【问题标题】:Maven2 compiler custom execution source directory and target directoryMaven2编译器自定义执行源目录和目标目录
【发布时间】:2009-08-16 18:59:22
【问题描述】:

我想在不同的阶段使用不同的 sourceDirectories 和 destinationDirectories 运行 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>

有没有办法让这个插件在不影响其默认运行的情况下,分阶段编译不同目录?

【问题讨论】:

    标签: java maven-2 maven-plugin javac


    【解决方案1】:

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

    您可以使用 build-helper-maven-plugin 的 add-sourceadd-test-source 为您的集成测试指定额外的源目录,但这不会删除现有的源目录。 p>

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

    如果您将 add-test-source 目标绑定到在 testCompile 目标之前运行,您的集成测试将被包含在内。请注意,您希望将它们输出到目标/测试类,以便确保插件能够找到它们。

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

    【讨论】:

      【解决方案2】:

      经过更多研究,很明显这在 Maven 2 中实际上不可能以我想要的方式进行,因此需要某种形式的 hack 来引入集成测试。虽然您可以添加其他目录(如 Rich Seller 所建议的那样),但没有插件可以删除其他源或将目录与主编译分开编译。

      我发现添加集成测试的最佳解决方案是首先使用构建助手插件添加目录 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>
      

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

      【讨论】:

      • 唉,这也不能正常工作。似乎如果你使用这个 cobertura:cobertura 集成测试,那么 Maven 会运行覆盖率的单元测试,然后是测试本身,最后运行集成测试,并且由于某种原因忽略了排除但考虑了包含。跨度>
      猜你喜欢
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多