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

Pau*_*ble 11 java maven-2 javac maven-plugin

我想在不同的阶段运行maven编译器插件,并使用不同的sourceDirectories和destinationDirectories,以便可以使用来自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>
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*ler 9

源目录在<build>元素内的compiler-plugin外部设置,因此这不起作用.

您可以使用build-helper-maven-plugin的add-sourceadd-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>
Run Code Online (Sandbox Code Playgroud)

如果将add-test-source目标绑定到testCompile目标之前运行,则将包含集成测试.请注意,您希望将它们输出到目标/测试类,以便surefire插件可以找到它们.

为了处理标准测试源的删除,我编写了一个小插件来修改模型以删除现有的testSource位置,然后再添加用于集成测试的位置.


Pau*_*ble 5

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

我发现添加集成测试的最佳解决方案是首先使用build helper插件添加目录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>
Run Code Online (Sandbox Code Playgroud)

现在,为了使集成测试在集成测试阶段执行,您需要使用排除和包含来操作它们如何运行,如下所示.这允许您可能需要的任何自定义参数(在我的情况下,通过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>
Run Code Online (Sandbox Code Playgroud)