首页 > 解决方案 > 如何在maven中做多个生成源代码的主类

问题描述

当我在一个模块中运行 mvn clean install 时,如何在 maven pom.xml 中执行此操作?

  1. 编译主要源代码
  2. 在编译的源代码中调用主类以生成另一个源代码
  3. 第 1 组和第 2 组并再次执行编译
  4. 在打包阶段,原始源代码和生成的源代码应该在 jar 中

标签: javamaven

解决方案


我尝试并有效的解决方案之一。基本上你在生成额外的java代码后强制再次调用maven编译器插件

<plugins>
           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <id>generate-code</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <classpathScope>compile</classpathScope>
                            <mainClass>MyClassGenerator</mainClass>
                            <arguments>
                                <argument>${generated.code.dir}</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>addtoSourceFolder</id>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <phase>process-classes</phase>
                        <configuration>
                            <sources>
                                <source>${generated.code.dir}</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <executions>
                    <execution>
                        <id>compile-generated-code</id>
                        <configuration>
                            <includes>**/*.java</includes>
                        </configuration>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

推荐阅读