运行Maven编译两次 [英] Run maven compilation twice
问题描述
我在蚂蚁迁移项目的Maven过程,这个项目是相当不寻常的:它使用两个编译步骤和这些编译步骤之间code生成步骤。整个构建过程可以描述如下:
I'm in process of migrating ant project to maven and this project is quite unusual: it uses two compilation steps and code generation step between those compilation steps. The whole build process can be described as follows:
- 编译src目录下的一切
- 运行内部的Java工具,指向Java来用于编译这些类的编译类和罐子。该工具通过使用反射生成的基础上编译的类code。
- 编译生成的类,最后组装罐子。
我发现,建议创建一个自定义的生命周期的几个环节,但我不知道从哪里开始。
如果有人可以指向类似的项目配置,这将是真正伟大的。
I found a few links that suggest to create a custom lifecycle but I've no idea where to start. If someone can point to similar project configuration that would be really great.
什么是与Maven实现这一目标的最简单的方法?
我想我应该用蚂蚁Maven插件,但我还是不明白如何使它编译来源两次,它指向第一次编译步骤后生成的来源。
What would be the simplest way of achieving this with maven? I guess I should use ant maven plugin but I still don't understand how to make it compile sources twice and point it to the generated sources after first compilation step.
推荐答案
好吧,我终于想通了如何做到这一点。
OK, I finally figured out how to do that.
基本上我对不同的构建阶段运行编译器,编译发生在生成来源阶段,工艺污染源阶段产生code,最后编译器可以在编译阶段最后汇编。
Basically I run compiler on different build phases so that compile happens on generate-sources phase, code is generated on process-sources phase and finally compiler makes final compilation on 'compile' phase.
下面是我的配置:
<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>
希望这将是有益的人。
Hope this will be helpful for someone.
更新您可能会注意到,编译器编译不必要对最终的编译器通过所有来源。要微调您的特定编译过程,你可能要使用'排除'/'包括配置您的项目。
UPDATE You may probably notice that compiler unnecessarily compiles all the sources on final compiler pass. To fine tune your particular compiler pass you may want to use 'exclude'/'include' configuration for your project.
这篇关于运行Maven编译两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!