我正在将 ant 项目迁移到 Maven,这个项目非常不寻常:它使用两个编译步骤和这些编译步骤之间的代码生成步骤。整个构建过程可以描述如下:
- 编译 src 目录中的所有内容
- 运行内部java工具,将java指向已编译的类和用于编译这些类的jar。该工具使用反射根据已编译的类生成代码。
- 编译生成的类并最终组装一个 jar。
我发现了一些建议创建自定义生命周期的链接,但我不知道从哪里开始。
如果有人可以指出类似的项目配置,那就太好了。
使用 Maven 实现这一目标的最简单方法是什么?
我想我应该使用 ant maven 插件,但我仍然不明白如何让它编译源两次并在第一个编译步骤后将其指向生成的源。
好吧,我终于知道该怎么做了。
基本上我在不同的构建阶段运行编译器,以便编译发生在生成源阶段,代码在进程源阶段生成,最后编译器在“编译”阶段进行最终编译。
这是我的配置:
<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>
希望这对某人有帮助。
UPDATE您可能会注意到编译器不必要地在最终编译器传递时编译所有源代码。要微调您的特定编译器传递,您可能需要为您的项目使用“排除”/“包含”配置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)