11

src / main/javaとsrc/test / java以外のディレクトリのコードを使用できるように、mavenコンパイラプラグインを別のフェーズで別のsourceDirectoriesとdestinationDirectoriesで実行したいと思います。

ソリューションは次のようになると思いました。リンク先のフェーズは統合前テストでした。ただし、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>

このプラグインを使用して、デフォルトの動作に影響を与えることなく、さまざまなフェーズでさまざまなディレクトリをコンパイルする方法はありますか?

4

2 に答える 2

9

ソースディレクトリは、<build>要素内のcompiler-pluginの外部に設定されているため、これは機能しません。

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>

add-test-sourceゴールをバインドして、 testCompileゴールの直前に実行すると、統合テストが含まれます。これらをtarget/test-classesに出力して、surefireプラグインがそれらを検出できるようにすることに注意してください。

標準のテストソースの削除を処理するために、統合テスト用の場所を追加する前に、モデルを変更して既存のtestSourceの場所を削除する小さなプラグインを作成しました。

于 2009-08-14T13:46:16.190 に答える
5

さらに調査した結果、Maven 2ではこれが私が望む方法では実際には不可能であることが明らかになりました。統合テストを導入するには、何らかの形のハックが必要です。(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>

ここで、統合テストを統合テストフェーズで実行するには、excludesとincludesを使用して、以下のように実行するタイミングを操作する必要があります。これにより、必要なカスタムパラメータが可能になります(私の場合、エージェントは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>
于 2009-08-16T10:59:22.450 に答える