17

Tomcat7-maven-plugin埋め込みインスタンスでJaCoCoを使用してコード カバレッジを取得する方法はありますか?

jacoco-maven-plugin は WAR の POM で構成され、単体テストを計測しますが、jacoco エージェントを組み込みの Tomcat インスタンスにアタッチして、Tomcat に対して実行される統合テストを計測する方法がわかりません。Tomcat インスタンスが組み込まれていることを考えると、このアプローチが可能かどうかはわかりません。これを達成する他の方法はありますか?おそらく、Tomcat Maven プラグインの使用から Cargo の使用に切り替えてカバレッジを取得できますが、可能であれば Tomcat プラグインを使い続けることを好みます。

これが私のPOMからのいくつかの関連するスニペットです:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.2.201302030002</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.14</version>
    <executions>
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <systemProperties>
            <!-- as expected, this system property doesn't work since Tomcat is embedded, but this is the type of config I'm looking for -->
            <JAVA_OPTS>-javaagent:${project.build.directory}/${jacoco.jar}=destfile=${project.build.directory}/jacoco.exec,append=true</JAVA_OPTS>
        </systemProperties>
    </configuration>
    <executions>
        <execution>
            <id>tomcat-startup</id>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>tomcat-shutdown</id>
            <goals>
                <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
        </execution>
    </executions>
</plugin>

バージョン: Maven 3.0.4、Tomcat Maven Plugin 2.1、Jacoco 0.6.2.201302030002、Java 7

4

6 に答える 6

8

質問が投稿されてからしばらく経っていることは知っていますが、答えが実際に問題の根本に対処しているとは思いません。これらのプラグイン内でテストを実行している場合、コード カバレッジはフェイルセーフまたは確実に機能する可能性があります。ただし、jacoco を使用して tomcat を監視してカバレッジ レポートを取得するだけの場合、現在の情報ではそれが提供されません。tomcat7-maven-plugin では、JAVA_OPTS を指定するだけでは jacoco に -javaagent を挿入できないことがわかりました。cargo に切り替えるとこんな風にできました。

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.2.201409121644</version>
    <configuration>
        <destFile>${sonar.jacoco.reportPath}</destFile>
        <dataFile>${sonar.jacoco.reportPath}</dataFile>
        <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
        <classDumpDir>${project.reporting.outputDirectory}/jacoco-it/classes</classDumpDir>
        <skip>${skipITs}</skip>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-agent</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${sonar.jacoco.reportPath}</destFile>
                <propertyName>jacoco.agent.itArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>jacoco-report</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>dump</goal>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
    <plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.4.11</version>
    <configuration>
        <skip>${skipITs}</skip>
        <container>
            <containerId>tomcat7x</containerId>
            <zipUrlInstaller>
                <url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip
                </url>
                <downloadDir>${project.build.directory}/downloads</downloadDir>
                <extractDir>${project.build.directory}/extracts</extractDir>
            </zipUrlInstaller>
            <dependencies>
                <dependency>
                    <groupId>ojdbc</groupId>
                    <artifactId>ojdbc6</artifactId>
                </dependency>
            </dependencies>
        </container>
        <configuration>
            <home>${project.build.directory}/catalina-base</home>
            <properties>
                <cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 -Drunmode=TEST</cargo.jvmargs>
                <cargo.servlet.port>9090</cargo.servlet.port>
            </properties>
            <configfiles>
                <configfile>
                    <file>${basedir}/src/test/conf/context.xml</file>
                    <todir>conf/Catalina/localhost/</todir>
                    <tofile>context.xml.default</tofile>
                </configfile>
            </configfiles>
        </configuration>
    </configuration>
    <executions>
        <execution>
            <id>start-tomcat</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-tomcat</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

重要な部分は次のとおりです。

  1. <plugin><groupId>org.jacoco</groupId> ...<propertyName>jacoco.agent.itArgLine</propertyName>
  2. <cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 </cargo.jvmargs>

jacoco プラグインでレポート ターゲットを実行すると、カバレッジ レポートを含むディレクトリが ${projectbase}/target/site/jacoco-it/index.html に作成されます。これは soapui-maven-plugin で使用しますが、selenium-maven-plugin でも使用できます。

于 2015-01-27T18:48:11.523 に答える
0

クラスをオフラインで計測し、JaCoCo エージェントを Tomcat クラスパス (プラグインの依存関係) に配置し、ファイル jacoco-agent.properties を追加するだけで、Tomcat が組み込まれた JaCoCo エージェントをセットアップする際の問題を解決しました。

作業構成をブログに掲載しました。

http://burkond.blogspot.de/2014/05/selenium-in-sonar-code-coverage-metrics.html

于 2014-05-26T15:55:48.157 に答える
0

私はまったく同じ問題を抱えていましたが、私が見つけた唯一の解決策は、MAVEN_OPTS以前にMavenビルドに設定することでした(たとえば、コマンドラインまたはJenkinsジョブの構成で):

export MAVEN_OPTS=-javaagent:~/.m2/repository/org/jacoco/org.jacoco.agent/0.7.4.201502262128/org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=./target/jacoco.exec,append=true

これにより、jacoco エージェントが組み込みの tomcat インスタンスにアタッチされ、カバレッジ結果が指定された .xml ファイルにレポートされdestfileます。

まず、jacoco ランタイム JAR へのパスが正しいことが重要です。手動でダウンロードして参照するか、別の Maven コマンドを使用してローカルの .m2 リポジトリにダウンロードできます。

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.jacoco:org.jacoco.agent:0.7.4.201502262128:jar:runtime

次に、jacoco.execファイルへのパスが正しいことを確認します。私の場合、単体テストの結果を含む既存の jacoco.exec ファイルがターゲット フォルダーに既にあります。はappend=true、単体テストと統合テストが組み合わされていることを確認します。

于 2015-06-12T07:38:38.463 に答える