13

ライブラリ (jar ファイル) を使用する Java プロジェクトがある場合、この jar 内のクラスのコード カバレッジを取得することは可能ですか?

この背後にあるアイデアは、プロジェクトが依存している外部ライブラリ (たとえば、Spring、Hibernate、または scala プロジェクトの場合は scala jar など) が実際に使用されている割合を調べたいということです。それらをリストして、必要な .class ファイルのみを含む単一の jar に再​​バンドルして(たとえば、apache felix のようなプラグインを使用して)、可能な限り小さい jar を取得できると想像することさえできます。これを本当にやりたいかどうかはわかりませんが、多くの理由でおそらく悪い考えであることは承知していますが、実験として考えています。

方法がわかりません。jacoco は、プロジェクト内のクラス ファイルのカバレッジのみを直接報告します。多分私は何か間違ったことをしている、私はこのようなmavenプラグインを使用しています:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.5.6.201201232323</version>
            <configuration>
                <destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</destfile>
                <datafile>${basedir}/target/coverage-reports/jacoco-unit.exec</datafile>
                <includes>
                    <include>**</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <id>jacoco-initialize</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-site</id>
                    <phase>package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

インクルード タグを変更しようとしましたが、唯一の効果は、プロジェクト内に直接クラス ファイルのみを含むデフォルトを制限することです。

前もって感謝します !


oersの回答後に編集:

antとantrun-pluginでそれを行う方法を見つけましたが、非常に複雑ですが、antrunプラグインのバージョンでは多くの問題がありました(基本的なタスクであっても、最近のバージョンを機能させることができません)。 Mavenに固執します。アリの代わりに je jacoco maven プラグインで同等のことを行う方法を誰かが知っていれば、私は興味があります!

ant による部分的な解決策: 実際、jacoco.exec ファイルには、外部 jar のクラスへの参照が既に含まれていました。したがって、これらのjarを考慮するように指示する必要があるのはレポートタスクであり、私が考えた実行時フェーズではありません. これが私が使用したmaven構成です(http://intellectualcramps.wordpress.com/2012/03/22/jacoco-tycho-and-coverage-reports/でヘルプを見つけました):

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <!--<version>1.7</version>-->
            <dependencies>
                <dependency>
                    <groupId>org.jacoco</groupId>
                    <artifactId>org.jacoco.ant</artifactId>
                    <version>0.5.6.201201232323</version>
                </dependency>
                <dependency>
                    <groupId>ant-contrib</groupId>
                    <artifactId>ant-contrib</artifactId>
                    <version>20020829</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>jacoco-report</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>

                            <taskdef name="jacoco-report"
                                   classname="org.jacoco.ant.ReportTask"
                                   classpathref="maven.plugin.classpath" />
                            <taskdef classpathref="maven.runtime.classpath"
                     resource="net/sf/antcontrib/antcontrib.properties" />
                            <available
               file="${project.basedir}/target/jacoco.exec"
               property="jacoco.exec.file.exists" />
                            <echo message="${project.basedir}/target/jacoco.exec" />
                            <if>
                                <equals arg1="${jacoco.exec.file.exists}"
                      arg2="true" />
                                <then>
                                    <echo message="Executing jacoco report" />

                                    <trycatch>
                                        <try>
                                            <jacoco-report>
                                                <executiondata>
                                                    <file
                                 file="${project.basedir}/target/jacoco.exec" />
                                                </executiondata>

                                                <structure name="Minerva">
                                                    <classfiles>
                                                        <fileset
                                     dir="target/classes" />

                                                        <fileset dir="C:/Data/dev/m2Repository/com/groupama/framework/crypt/fwk-cryptage/1.0/">
                                                            <include name="**/*.jar"/>
                                                        </fileset>

                                                    </classfiles>

                                                    <sourcefiles
                                    encoding="UTF-8">
                                                        <fileset
                                     dir="src/main/java" />
                                                    </sourcefiles>
                                                </structure>
                                                <html destdir="${project.basedir}/target/jacoco/report" />
                                                <xml destfile="${project.basedir}/target/jacoco/report/jacoco.xml"/>
                                            </jacoco-report>
                                        </try>
                                        <catch>
                                            <echo>skipping</echo>
                                        </catch>
                                    </trycatch>
                                </then>
                                <else>
                                    <echo message="No jacoco.exec file found." />
                                </else>
                            </if>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
4

2 に答える 2

2

レポートの目標に、分析するライブラリのクラスを指定する必要があります。残念ながら、そのドキュメントは見つかりません。公式ドキュメントは...うーん...まばらです

antを実行できる場合は、レポートタスクを確認することをお勧めします。

<jacoco:report>

    <executiondata>
        <file file="jacoco.exec"/>
    </executiondata>

    <structure name="Example Project">
        <classfiles>
            <fileset dir="classes"/> <!-- HERE THE CLASSES FROM YOUR LIB -->
        </classfiles>
        <sourcefiles encoding="UTF-8">
            <fileset dir="src"/> <!-- HERE THE SORUCESFROM YOUR LIB -->
        </sourcefiles>
    </structure>

    <html destdir="report"/>

</jacoco:report>
于 2012-04-19T07:14:07.317 に答える