运行maven编译两次

Ale*_*lex 4 java ant maven

我正在将ant项目迁移到maven,这个项目很不寻常:它在这些编译步骤之间使用了两个编译步骤和代码生成步骤.整个构建过程可以描述如下:

  1. 编译src目录中的所有内容
  2. 运行内部java工具,将java指向已编译的类和用于编译这些类的jar.此工具使用反射基于编译的类生成代码.
  3. 编译生成的类,最后组装一个jar.

我发现了一些建议创建自定义生命周期的链接,但我不知道从哪里开始.如果有人可以指出类似的项目配置真的很棒.

用maven实现这个目标的最简单方法是什么?我想我应该使用ant maven插件,但我仍然不明白如何使它编译源两次并在第一次编译步骤后将其指向生成的源.

Ale*_*lex 6

好吧,我终于想出了如何做到这一点.

基本上我在不同的构建阶段运行编译器,以便在generate-sources阶段进行编译,在process-sources阶段生成代码,最后编译器在'compile'阶段进行最终编译.

这是我的配置:

<build>
<plugins>
  <!-- Code generation, executed after the first compiler pass -->
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
      <execution>
        <id>generateCode</id>
        <phase>process-sources</phase>
        <goals>
          <goal>java</goal>
        </goals>
        <configuration>
          <classpathScope>test</classpathScope>
          <mainClass>my.code.Generator</mainClass>
          <arguments>
            <argument>-target</argument>
            <argument>${project.build.directory}/generated-sources/java</argument>
            <argument>-source</argument>
            <argument>my.code.generator.Configuration</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <id>add-source</id>
        <phase>process-sources</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>${project.build.directory}/generated-sources/java</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>


  <!-- Custom compilation mode -->
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
      <execution>
        <id>default-compile</id>
        <phase>generate-sources</phase>
      </execution>
      <execution>
        <id>build-generated-code</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
        <configuration>
          <generatedSourcesDirectory>${project.build.directory}/generated-sources/java</generatedSourcesDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

希望这对某人有所帮助.

更新您可能会注意到编译器不必要地编译最终编译器传递的所有源.要微调特定的编译器传递,您可能需要为项目使用"exclude"/"include"配置.