[英]How can I get code coverage of an external java library with jacoco?
If I have a java project that uses a library (a jar file), is it possible to get the code coverage of classes inside this jar ? 如果我有一个使用库(jar文件)的java项目,是否可以在这个jar中获取类的代码覆盖率?
The idea behind this is that I would like to find out what proportion of the external libraries the project relies on (let's say spring, hibernate, or it could be the scala jars if it was a scala project, why not) are actually used. 这背后的想法是,我想找出项目所依赖的外部库的比例(假设是spring,hibernate,或者它可能是scala jar,如果它是scala项目,为什么不这样)实际上是使用的。 I even imagine that I could try to list them and rebundle them in a single jar that would contain only necessary .class files (with a plugin like apache felix, for example) to get the smallest possible jar.
我甚至想象我可以尝试列出它们并在一个jar中重新绑定它们,这个jar 只包含必要的.class文件(例如像apache felix这样的插件)以获得尽可能小的jar。 I'm not sure I really want to do this, I'm aware it is probably a bad idea for a number of reasons, but I think of it as an experimentation.
我不确定我是否真的想要这样做,我知道这可能是一个坏主意,原因有很多,但我认为这是一个实验。
I can't find how to do it, jacoco reports only coverage for the class files directly inside my project. 我找不到怎么做,jacoco直接在我的项目中报告类文件的覆盖范围。 Maybe I'm doing something wrong, I'm using the maven plugin like this :
也许我做错了什么,我正在使用像这样的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>
I've tried changing the include tag, but the only effect is restricting the default which includes only the class files directly inside my project. 我已经尝试更改include标记,但唯一的效果是限制默认值,其中只包含项目中的类文件。
Thanks in advance ! 提前致谢 !
Edit after oers' answer : 在oers的回答后编辑:
I found out how to do it with ant and antrun-plugin, though it is very complicated, I had much trouble with antrun plugin versions (unable to make a recent version work, even for a basic task) and I'd really like to stick to Maven. 我发现如何使用ant和antrun-plugin来做这件事,虽然它非常复杂,我在使用antrun插件版本时遇到了很多麻烦(无法使最新版本工作,即使是基本任务)我真的很想坚持Maven。 If someone knows how to do the equivalent with je jacoco maven plugin instead of ant, I'm interested !
如果有人知道如何使用je jacoco maven插件代替蚂蚁,我很感兴趣!
Partial solution with ant : actually the jacoco.exec file already contained references to the classes of my external jars ; 使用ant的部分解决方案:实际上jacoco.exec文件已经包含对我的外部jar类的引用; therefore it is the report task that should be told to take account of these jar, and not the runtime phase as I thought.
因此,报告任务应该被告知考虑到这些jar,而不是我想的运行时阶段。 Here is the maven configuration I used (I found help on http://intellectualcramps.wordpress.com/2012/03/22/jacoco-tycho-and-coverage-reports/ ) :
这是我使用的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>
You need to give the report goal the classes of the library you want to analyze. 您需要为报告目标指定要分析的库的类。 Unfortunatly I can't find a documentation on that.
不幸的是,我找不到相关文档。 The official docu is ... hm ... sparse
官方文件是......嗯...稀疏
IF you can execute ant, I'd suggest looking at the report task . 如果您可以执行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>
Even though this question is old, it still seems relevant. 即使这个问题很老,但它似乎仍然相关。 If the external library is a Maven project, you can go to the library source directory and request JaCoCo report obtained previously with your testsuite from within there:
如果外部库是Maven项目,您可以转到库源目录并请求之前使用您的testsuite获取的JaCoCo报告:
mvn org.jacoco:jacoco-maven-plugin:0.7.8:report -Djacoco.dataFile=/path/to/jacoco.exec
You obtain the report in ${project}/target/site/jacoco directory. 您在$ {project} / target / site / jacoco目录中获取报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.