簡體   English   中英

如何使用jacoco獲取外部Java庫的代碼覆蓋率?

[英]How can I get code coverage of an external java library with jacoco?

如果我有一個使用庫(jar文件)的java項目,是否可以在這個jar中獲取類的代碼覆蓋率?

這背后的想法是,我想找出項目所依賴的外部庫的比例(假設是spring,hibernate,或者它可能是scala jar,如果它是scala項目,為什么不這樣)實際上是使用的。 我甚至想象我可以嘗試列出它們並在一個jar中重新綁定它們,這個jar 包含必要的.class文件(例如像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>

我已經嘗試更改include標記,但唯一的效果是限制默認值,其中只包含項目中的類文件。

提前致謝 !


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

您需要為報告目標指定要分析的庫的類。 不幸的是,我找不到相關文檔。 官方文件是......嗯...稀疏

如果您可以執行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>

即使這個問題很老,但它似乎仍然相關。 如果外部庫是Maven項目,您可以轉到庫源目錄並請求之前使用您的testsuite獲取的JaCoCo報告:

mvn org.jacoco:jacoco-maven-plugin:0.7.8:report -Djacoco.dataFile=/path/to/jacoco.exec

您在$ {project} / target / site / jacoco目錄中獲取報告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM