java Maven2编译器自定义执行源目录和目标目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1277870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven2 compiler custom execution source directory and target directory
提问by Paul Keeble
I want to run the maven compiler plugin in a different phase and with different sourceDirectories and destinationDirectories such that code from directories other than src/main/java and src/test/java can be used.
我想在不同的阶段和不同的 sourceDirectories 和 destinationDirectories 运行 maven 编译器插件,以便可以使用来自 src/main/java 和 src/test/java 以外的目录的代码。
I thought the solution would look something like the below, where the phase I was linking it to was pre-integration-test. However the properties for testSourceDirectory and testOutputDirectory don't seem to be specified in this way as they are in the section of the POM.
我认为解决方案看起来像下面这样,我将它链接到的阶段是预集成测试。然而 testSourceDirectory 和 testOutputDirectory 的属性似乎没有以这种方式指定,因为它们在 POM 的部分中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile mytests</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<testSourceDirectory>${basedir}/src/inttest/java</testSourceDirectory>
<testOutputDirectory>${basedir}/target/inttest-classes</testOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Is there a way to get this plug-in to compile different directories in different phases without affecting its default operation?
有没有办法让这个插件在不影响其默认运行的情况下,在不同阶段编译不同目录?
采纳答案by Rich Seller
The source directories are set outside the compiler-plugin inside the <build> element, so this won't work.
源目录设置在 <build> 元素内的编译器插件之外,因此这不起作用。
You can use the build-helper-maven-plugin's add-sourceand add-test-sourceto specify additional source directories for your integration tests, but this will notremove the existing source dirs.
您可以使用 build-helper-maven-plugin 的add-source和add-test-source为您的集成测试指定额外的源目录,但这不会删除现有的源目录。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-it-source</id>
<phase>pre-integration-test</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/inttest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
If you bind the add-test-sourcegoal to run just before the testCompilegoal, your integration tests will be included. Note you want them to be output to target/test-classes so the surefire plugin will find them.
如果您将add-test-source目标绑定到testCompile目标之前运行,您的集成测试将被包括在内。请注意,您希望将它们输出到目标/测试类,以便surefire 插件能够找到它们。
To handle removal of the standard test sources, I wrote a small plugin to modify the model to remove existing testSource locations before adding the ones for integration tests.
为了处理标准测试源的删除,我编写了一个小插件来修改模型以在添加用于集成测试的测试源位置之前删除现有的测试源位置。
回答by Paul Keeble
After more research it is apparent this is not actually possible in Maven 2 in the way I want, a hack of some form is necessary to introduce integration tests. While you can add additional directories (as suggested by Rich Seller) there is no plugin to remove the other sources or to compile a directory separately from the main compilation.
经过更多研究,很明显这在 Maven 2 中实际上不可能以我想要的方式进行,需要某种形式的 hack 来引入集成测试。虽然您可以添加其他目录(如 Rich Seller 所建议的),但没有插件可以删除其他源或将目录与主编译分开编译。
The best solution I have found for adding integration tests is to first use the build helper plugin to add the directory inttest directory to be compiled as tests.
我找到的添加集成测试的最佳解决方案是首先使用构建助手插件添加目录 inttest 目录以编译为测试。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/inttest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Now in order to get the integration tests to execute on the integration-test phase you need to use excludes and includes to manipulate when they get run as below. This allow any custom parameters you might want (in my case an agent being added via argline).
现在,为了让集成测试在集成测试阶段执行,您需要使用排除和包含在它们运行时进行操作,如下所示。这允许您可能需要的任何自定义参数(在我的情况下,通过 argline 添加代理)。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/itest/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>inttests</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes><exclude>none</exclude></excludes>
<includes>
<include>**/itest/**/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

