如何在Java 7和spring-instrument上运行Jacoco? [英] How to run Jacoco with Java 7 and spring-instrument?

查看:101
本文介绍了如何在Java 7和spring-instrument上运行Jacoco?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于将cobertura与Java 7结合使用时遇到了一些问题-我正在尝试 Jacoco . 我的项目有一个父pom.xml和子项目. 在一个项目中,我使用spring来运行一些集成测试-所以我在这个项目的pom.xml中有这个插件:

Since I had some problems using cobertura with Java 7 - I'm trying Jacoco. My project has a parent pom.xml and sub projects. In one project I use spring to run some integration tests - so I have this plugin in this project's pom.xml:

<plugin>
    <version>2.12.4</version>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!-- -XX:-UseSplitVerifier is for java 7 -->
        <argLine>
            -XX:-UseSplitVerifier 
            -javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar
        </argLine>
    </configuration>
</plugin>

由于我使用Java 7,因此已在父级pom.xml中设置了此插件:

Since I use Java 7, I've set this plugin in the parent pom.xml:

<plugin>
    <version>2.5.1</version>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    <optimize>true</optimize>
    <debug>true</debug>
    <showDeprecation>true</showDeprecation>
    <showWarnings>true</showWarnings>
    <encoding>utf8</encoding>
    </configuration>
</plugin>

现在,当我使用mvn clean install时,所有项目在/target文件夹中都有此文件:jacoco.exec ;但是,使用spring-instrument的项目没有此文件. 我认为问题在于 Jacoco 希望使用spring-instrumentasm,但是它失败了(但我不确定我是对的).

Now, when I'm using mvn clean install all the projects have this file in the /target folder: jacoco.exec ; But this project that uses the spring-instrument does not have this file. I think the problem is that Jacoco wishes to use the asm of spring-instrument but it fails (but I'm not sure I'm right).

Jacoco 版本是0.6.3.201306030806.

为什么在这种情况下 Jacoco 无法检测? 我该如何克服?

Why does Jacoco fails to instrument in this case? How can I overcome this?

我想也许我可以配置 maven-compiler-plugin 将代码编译为1.6,然后我就不需要 maven-surefire-plugin 插件.有道理吗?

I thought maybe I can configure the maven-compiler-plugin to compile the code to 1.6 and then I wouldn't need the maven-surefire-plugin plugin. Does it make sense?

推荐答案

Surefire插件正在重写Jacoco Maven插件定义的argLine值.

The argLine value defined by Jacoco Maven Plugin is being rewritten by the Surefire Plugin.

在"jacoco-maven-plugin"配置中设置属性名称,如下所示:

Set a property name in your "jacoco-maven-plugin" configuration like this:

<propertyName>coverageAgent</propertyName>

,然后在您的surefire插件配置中编辑argLine,使其包含Jacoco的代理:

and then edit the argLine in your surefire plugin configuration so it includes Jacoco's agent:

<argLine>
    -XX:-UseSplitVerifier
    ${coverageAgent} 
    -javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar
</argLine>

请注意,Jacoco的代理人已放置在弹簧乐器之前.之所以应该这样做,是因为Jacoco在处理修改后的字节码(例如AspectJ LTW生产的字节码)时遇到了问题.

Notice that the Jacoco's agent is placed before the Spring's Instrument. That's the way it should be done because Jacoco has problems dealing with modified bytecode (e.g. the one produced by AspectJ LTW).

实际上,即使是第一代理人,Jacoco的报告仍然可能是错误的,但是问题通常仅限于少数情况(例如

Actually, even when being the first agent, Jacoco's reports can still be wrong, but the problem is usually limited to a small set of circumstances (e.g. http://sourceforge.net/p/eclemma/discussion/614869/thread/3d875388 ).

这篇关于如何在Java 7和spring-instrument上运行Jacoco?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆