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

时间:2021-12-24 23:58:19

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.

我希望在不同的阶段运行maven编译器插件,并使用不同的源目录和destinationdirectory,以便可以使用来自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?

是否有办法让这个插件在不影响其默认操作的情况下,在不同的阶段编译不同的目录?

2 个解决方案

#1


9  

The source directories are set outside the compiler-plugin inside the <build> element, so this won't work.

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

You can use the build-helper-maven-plugin's add-source and add-test-source to specify additional source directories for your integration tests, but this will not remove 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-source goal to run just before the testCompile goal, 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.

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

#2


5  

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中并不是我想要的方式,引入集成测试需要一些形式的技巧。虽然可以添加额外的目录(如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.

对于添加集成测试,我发现的最佳解决方案是首先使用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>

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>

#1


9  

The source directories are set outside the compiler-plugin inside the <build> element, so this won't work.

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

You can use the build-helper-maven-plugin's add-source and add-test-source to specify additional source directories for your integration tests, but this will not remove 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-source goal to run just before the testCompile goal, 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.

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

#2


5  

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中并不是我想要的方式,引入集成测试需要一些形式的技巧。虽然可以添加额外的目录(如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.

对于添加集成测试,我发现的最佳解决方案是首先使用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>

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>