如果我有一个使用库(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>
Run Code Online (Sandbox Code Playgroud)
我已经尝试更改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" /> …Run Code Online (Sandbox Code Playgroud)