1

大規模な Maven プロジェクトに統合テストを追加しようとしています。イベントの望ましい順序は次のとおりです。

  1. 既存のアーティファクトを消去します。
  2. 依存関係を解決し、プロジェクトをビルドします。
  3. Surefire プラグインを介して単体テストを実行します。
  4. Clover プラグインのフォーク ライフサイクル。
  5. --- Clover プラグインを使用してソースを計測します。
  6. --- Clover の project.build.directory と project.build.finalName を変更します。
  7. --- 新しいディレクトリに Clover インストルメント化プロジェクトをビルドします。
  8. --- Surefire プラグインを介して Clover の計測ユニット テストを実行します。
  9. --- Clover のインストルメント化された単体テストからコード カバレッジを確認します。
  10. --- project.build.directory と project.build.finalName を元の値にリセットします。
  11. --- フォークされたライフサイクルを終了します (Clover は「テスト」段階でのみフォークします)。
  12. プロジェクトを WAR ファイルとしてパッケージ化します。
  13. Tomcat7 プラグインを介してプロジェクト WAR ファイルをローカルでホストします。
  14. Surefire プラグインを介して Tomcat7 インスタンスに対して統合テストを実行します。

手順 9 を除いて、これはすべて期待どおりに機能します (したがって、質問です)。代わりに、ビルド ディレクトリは引き続き Clover バージョンに設定され、プロジェクト名には「-clover」が追加されます。「MyProject-clover.war」が Tomcat7 によってホストされている場合、期待どおりに機能しないこともわかりました (ブラウザーで 404 エラーが返されます)。

たとえそれが機能したとしても、統合テストは本番コードに触れないため、テスト対象のWARファイルにCloverインストルメンテーションは必要ありません/必要ありません(src/test/javaの下のすべてのSelenium UIのものです本番コード自体ではなく、ローカルでホストされているページと対話します)。

前述のとおり、これは数百の依存関係と数十のプラグインを含む大規模なプロジェクトです。以下は私の問題に関連していると思いますが、必要に応じてさらに掘り下げることができます (pom ファイル全体を投稿するのは不合理に思えます)。

Clover プラグインの pom 構成は次のとおりです。

<plugin>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>maven-clover2-plugin</artifactId>
    <version>3.1.11</version>
    <configuration>
        <generateHtml>true</generateHtml>
        <generateXml>true</generateXml>
        <licenseLocation>${basedir}/src/test/resources/clover.license</licenseLocation>
        <targetPercentage>92%</targetPercentage>
      <excludes>
        <exclude>**/mock/*.java</exclude>
        <exclude>**/com/mycompany/somestuff/*.java</exclude>
      </excludes>
    </configuration>
    <executions>
        <execution>
            <id>generate-clover-report</id>
            <phase>test</phase>
            <goals>
                <goal>instrument</goal>
                <goal>clover</goal>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

WAR プラグインの pom 構成は次のとおりです。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
            <manifestEntries>
                <!--Implementation-Build>${buildNumber}_${timestamp}</Implementation-Build -->
                <Build-Time>${timestamp}</Build-Time>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

Tomcat7 プラグインの pom 構成は次のとおりです。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <ajpPort>8009</ajpPort>
        <backgroundProcessorDelay>2</backgroundProcessorDelay>
        <configurationDir>${project.build.directory}/tomcat7_plugin</configurationDir>
        <contextFile>${CATALINA_HOME}/conf/context.xml</contextFile>
        <contextReloadable>false</contextReloadable>
        <fork>true</fork>
        <hostName>localhost</hostName>
        <httpsPort>8443</httpsPort>
        <ignorePackaging>false</ignorePackaging>
        <jarScanAllDirectories>false</jarScanAllDirectories>
        <path>/contentmain</path>
        <port>8080</port>
        <serverXml>${CATALINA_HOME}/conf/server.xml</serverXml>
        <tomcatUsers>${CATALINA_HOME}/conf/tomcat-users.xml</tomcatUsers>
        <tomcatWebXml>${CATALINA_HOME}/conf/web.xml</tomcatWebXml>
        <useNaming>true</useNaming>
        <useTestClasspath>true</useTestClasspath>
        <update>true</update>
        <warDirectory>${project.build.directory}/${project.build.finalName}</warDirectory>
    </configuration>
    <executions>
        <execution>
            <id>start-tomcat</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <configuration>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>stop-tomcat</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>shutdown</goal>
            </goals>
        </execution>
    </executions>
</plugin>

「mvn clean install -Dmaven.clover.skip」を実行したときの現在の(望ましい)動作は次のとおりです。

[INFO] --- maven-clover2-plugin:3.1.11:check (generate-clover-report) @ MyProject ---
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:unpack (unpack) @ MyProject ---
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes css/*.css and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes scripts/*/*.* and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes images/*.* and excludes:null
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ MyProject ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MyProject] in [mydir/MyProject/target/MyProject]
[INFO] Processing war project
[INFO] Copying webapp resources [mydir/MyProject/src/main/webapp]
[INFO] Webapp assembled in [2019 msecs]
[INFO] Building war: /mydir/MyProject/target/MyProject.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (default) @ MyProject >>>
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:copy (default) @ MyProject ---
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.1:create-timestamp (default) @ MyProject ---
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (default) @ MyProject <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ MyProject ---
[INFO] Building jar: /mydir/MyProject/target/MyProject-sources.jar
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run-war-only (start-tomcat) @ MyProject ---
[INFO] Running war on http://localhost:8080/contentmain

「mvn clean install」を実行したときの現在の(望ましくない)動作は次のとおりです(maven-war-pluginによってリストされたパスとmaven-source-pluginからの警告メッセージに注意してください:

[INFO] --- maven-clover2-plugin:3.1.11:check (generate-clover-report) @ MyProject ---
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:unpack (unpack) @ MyProject ---
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes css/*.css and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes scripts/*/*.* and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes images/*.* and excludes:null
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ MyProject ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MyProject] in [mydir/MyProject/target/clover/MyProject-clover]
[INFO] Processing war project
[INFO] Copying webapp resources [mydir/MyProject/src/main/webapp]
[INFO] Webapp assembled in [1770 msecs]
[INFO] Building war: /mydir/MyProject/target/clover/MyProject-clover.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (default) @ MyProject >>>
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:copy (default) @ MyProject ---
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.1:create-timestamp (default) @ MyProject ---
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (default) @ MyProject <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ MyProject ---
[WARNING] NOT adding sources to artifacts with classifier as Maven only supports one classifier per artifact. Current artifact [com.mycompany:MyProject:war:clover:ParentProject-SNAPSHOT] has a [clover] classifier.
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run-war-only (start-tomcat) @ MyProject ---
[INFO] Running war on http://localhost:8080/contentmain

のテスト段階で Clover が maven-clover2-plugin:check ゴールの実行を終了した後、 {project.build.directory} と {project.build.finalName} の値が元の値にリセットされるようにするにはどうすればよいですか?分岐したライフサイクル?

CloverWARプラグイン、およびTomcat7のオンライン マニュアルを参照してみました。Clover によって変更されたビルド変数を元に戻すために使用できる設定については言及されていません。私はいつでも pom ファイルにパスをハードコードすることができますが、より脆弱でない解決策を好みます。

4

2 に答える 2

0

[情報] --- maven-source-plugin:2.2.1:jar (デフォルト) @ MyProject --- [警告] Maven はアーティファクトごとに 1 つの分類子のみをサポートするため、分類子を使用してソースをアーティファクトに追加しないでください。現在のアーティファクト [com.mycompany:MyProject:war:clover:ParentProject-SNAPSHOT] には [clover] 分類子があります。

これは Maven の制限です。複数の分類子を持つアーティファクトはサポートされていません。分岐したライフ サイクルのアーティファクトには既に「クローバー」分類子があるため、同時に「ソース」分類子を持つことはできません。そのため、この警告が表示されます。maven-source-plugin を呼び出す必要がある場合は、 clover2:instrumentの代わりにclover2:setupの使用を検討してください。

4. Fork lifecycle for Clover plugin.
--- End forked lifecycle (Clover only forks through 'test' phase).

ビルドをフォークする Clover ゴールが 2 つあります。

  • clover2:instrument - 「インストール」フェーズまでビルドをフォークします

  • clover2:instrument-test - 「テスト」フェーズまでビルドをフォークします

後者に興味があるかもしれません。また、 useCloverClassifier=false オプションの使用に興味があるかもしれません。これにより、フォークされたビルドで「クローバー」分類子の使用が無効になります。

「MyProject-clover.war」が Tomcat7 によってホストされている場合、期待どおりに機能しないこともわかりました (ブラウザーで 404 エラーが返されます)。

私の最初の推測は、実行時に clover.jar (com.atlassian.clover:clover) が見つからないということです。clover.jar を Tomcat の /lib ディレクトリにコピーするか、WAR にバンドルする必要があります。https://confluence.atlassian.com/display/CLOVER/Using+Clover+for+web+applicationsを参照してください

于 2014-10-07T20:50:23.987 に答える
0

clover2:setup ターゲットが使用されない限り、Clover は常にこれらの変数を変更します。ただし、Clover のライフサイクルをフォークしたい場合 (つまり、clover2:instrument または clover2:instrument-test)、これらの変数は常に変更されます。

clover:instrument-test を使用してライフサイクルをフォークし続けたいので、回避策を考え出しました。project.build.finalName 変数と project.build.directory 変数を使用する代わりに、Clover が混乱する前に Maven の実行時にコピーおよび保存される独自の変数を使用しています。

<properties>
    <!-- Saving these variables now before Clover alters them -->
    <original.build.finalName>${project.build.finalName}</original.build.finalName>
    <original.build.directory>${project.build.directory}</original.build.directory>
</properties>

次に、後続のすべてのプラグインに、Clover が変更したプロジェクト変数の代わりにこれらの変数を使用するように指示します。

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <cacheFile>${original.build.directory}/war/work/webapp-cache.xml</cacheFile>
            <outputDirectory>${original.build.directory}</outputDirectory>
            <warName>${original.build.finalName}</warName>
            <webappDirectory>${original.build.directory}/${original.build.finalName}</webappDirectory>
            <workDirectory>${original.build.directory}</workDirectory>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <configurationDir>${original.build.directory}/tomcat7_plugin</configurationDir>
            <warDirectory>${original.build.directory}/${original.build.finalName}</warDirectory>
        </configuration>
    </plugin>
</plugins>

別の解決策として、後続の Maven フェーズで追加のプラグインを作成または利用して、Clover の実行が終了した後に project.build 変数を元に戻すことが含まれる場合があります。これらの解決策のいずれかは、すべてのプラグインでパスをハードコーディングするよりもおそらく優れています。

于 2014-09-23T18:49:37.950 に答える