如何使用exec插件在两者之间运行maven编译两次?

Art*_*ent 4 maven-3 maven-compiler-plugin

我有一个简单的代码生成器G,它从我的项目中读取接口A并从中生成一个新的接口B. 因此我需要实现这个目标:

  1. 编译A.
  2. 执行G.
  3. 编译B.

步骤1和3由maven-compiler-plugin处理,而对于步骤2,我使用maven-exec-plugin.目前步骤1和2运行良好,但我无法弄清楚如何再次运行编译器插件来编译新生成的B版本.

这可能与maven有关,还是有另一种方法可以解决我的问题?

解:

基于khmarbaise的答案,我将此添加到我的pom.xml中,让第一次编译在generate-sources阶段运行,代码生成在process-sources阶段,这使得生成的类在编译阶段可用:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <executions>
                <execution>
                    <id>pre-compile</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.example.MyCodeGenerator</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

khm*_*ise 5

让我们在generate-sources中进行编译.只需将maven-compiler-plugin配置为在该特定生命周期阶段运行,并将生成的代码(编译代码)放在除默认值之外的其他位置.其次让你的执行在之后的阶段(进程源)运行,最后让其余的像往常一样.结果是你必须将maven-compiler-plugin绑定到generate-sources阶段,exec-plugin绑定到process-sources生命周期阶段.