Java Maven2编译器自定义执行源目录和目标目录

Java Maven2编译器自定义执行源目录和目标目录,java,maven-2,maven-plugin,javac,Java,Maven 2,Maven Plugin,Javac,我想在不同的阶段使用不同的SourceDirectory和DestinationDirectory运行maven编译器插件,这样就可以使用src/main/java和src/test/java以外的目录中的代码 我认为解决方案会像下面这样,我将它链接到的阶段是预集成测试。但是,testSourceDirectory和testOutputDirectory的属性似乎没有像POM部分中那样以这种方式指定 <plugin> <groupId>org.apache.maven

我想在不同的阶段使用不同的SourceDirectory和DestinationDirectory运行maven编译器插件,这样就可以使用src/main/java和src/test/java以外的目录中的代码

我认为解决方案会像下面这样,我将它链接到的阶段是预集成测试。但是,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>

org.apache.maven.plugins
maven编译器插件
编译我的测试
测试编译
预集成测试
${basedir}/src/inttest/java
${basedir}/target/inttest类

有没有办法让此插件在不同阶段编译不同的目录而不影响其默认操作?

源目录设置在元素内部的编译器插件外部,因此这不起作用

您可以使用build helper maven插件和为集成测试指定其他源目录,但这不会删除现有的源目录

  <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>

org.codehaus.mojo
构建助手maven插件
1.3
添加它的来源
预集成测试
添加源
${basedir}/src/inttest/java
如果将添加测试源目标绑定为在测试编译目标之前运行,则将包括集成测试。注意,您希望将它们输出到目标/测试类,以便surefire插件能够找到它们


为了处理标准测试源的删除,我编写了一个小插件来修改模型,以便在添加用于集成测试的测试源位置之前删除现有的测试源位置。

经过更多的研究,很明显这在Maven 2中实际上不可能以我想要的方式实现,引入集成测试需要某种形式的破解。虽然您可以添加额外的目录(如Rich Seller所建议的),但没有插件可以删除其他源代码或单独编译主编译目录

我找到的添加集成测试的最佳解决方案是首先使用buildhelper插件添加要编译为测试的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>

org.codehaus.mojo
构建助手maven插件
添加测试源
生成源
添加测试源
src/inttest/java
现在,为了让集成测试在集成测试阶段执行,您需要使用excludes和include在运行时进行操作,如下所示。这允许您可能需要的任何自定义参数(在我的示例中,是通过argline添加的代理)


org.apache.maven.plugins
maven surefire插件
**/itest/**
内部测试
测试
集成测试
没有一个
**/itest/***/Test.java

唉,这也不能正常工作。在中,如果您使用这个cobertura:cobertura集成测试,那么Maven将运行具有覆盖率的单元测试,然后测试本身,最后运行集成测试,出于某种原因,将忽略排除,但考虑包含。
<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>