Jacoco maven 插件阻塞控制台异常-java.lang.IllegalStateException: class 已经检测
Jacoco maven plugin clogs up console with Exceptions-java.lang.IllegalStateException: class is already instrumented
我正在使用 Jacoco 插件进行声纳代码覆盖,并使用 power mock mockito 组合编写 JUnit 测试用例,而当我 运行 mvn clean install 但控制台显示非常非常长的堆栈跟踪链,对于项目中使用的许多 类 不断出现,非常烦人,
异常跟踪类似于以下 -
java.lang.instrument.IllegalClassFormatException: Error while instrumenting class..
Caused by: java.io.IOException: Error while instrumenting class.
Caused by: java.lang.IllegalStateException: Class <class-name> is already instrumented.
我通过以下 link 寻求解决方案,但找不到出路:-
https://github.com/jacoco/jacoco/issues/32
link 说 "The error indicates that there are two JaCoCo agents configured for the same process, Looks like activation of surefire-report-plugin leads to a doubled agent, which is not an exceptional case, but an error in configuration of JVM for tests" 在我的例子中,我猜它甚至不是双重代理,因为我的 argLine 已设置为只有一个 jacoco 代理。
在我的例子中,argLine 设置为以下 -
javaagent:C:\Users\user\.m2\repository\org\jacoco\org.jacoco.agent\0.7.6.201602180812\org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=C:Users\user\git\package\target\jacoco.exec
我的 pom.xml jacoco-sonar 配置条目如下 -
<properties>
<sonar.sources>src/main/java</sonar.sources>
<sonar.tests>src/test/java</sonar.tests>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.language>java</sonar.language>
<sonar.binaries>${project.basedir}/target/classes</sonar.binaries>
<sonar.inclusions>
**/com/abc/service/impl/ABCServiceImpl.java,
**/com/abc/dao/impl/ABCDAOImpl.java
</sonar.inclusions>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<skipTests>false</skipTests>
<argLine>-Xmx1024m -XX:MaxPermSize=256m ${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意:如果我删除了 'default-instrument' 和 'default-restore-instrumented-classes' 的执行元素,那么异常跟踪不会出现,但我的覆盖率 = 0.0%声纳仪表板,使用这两个执行元素为我提供了正确的代码覆盖率,但在控制台上有冗长的堆栈跟踪。任何帮助表示赞赏。
我使用 mockito 而不是 powermock,但我认为与声纳的使用没有任何区别。我使用 Sonar Maven Scanner 进行分析,它非常有效。
我的 pom.xml 包含以下属性
<properties>
<project.source.version>1.8</project.source.version>
<project.target.version>${project.source.version}</project.target.version>
<sonar.scanner.version>3.3.0.603</sonar.scanner.version>
<jacoco.maven.version>0.7.9</jacoco.maven.version>
<sonar.projectName>MyProject</sonar.projectName>
<sonar.projectKey>my-project-key</sonar.projectKey>
<sonar.language>java</sonar.language>
</properties>
并且插件配置如下
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<inherited>true</inherited>
<configuration>
<source>${project.source.version}</source>
<target>${project.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.scanner.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.maven.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
你可以运行分析
mvn clean install sonar:sonar
这会生成一个位于 target/jacoco.exec 的默认 exec 文件,您的本地声纳服务器将使用该文件,并且还包含覆盖范围。
更新答案
使用 powermock 进行了测试。我使用了 powermock jacoco-offline example 并对其进行了一些调整。
那么pom.xml就是
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>org.powermock</groupId>
<artifactId>jacoco-offline</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>JaCoCo Offline with PowerMock</name>
<description>
Example how to get code coverage with PowerMock
</description>
<properties>
<jacoco.version>0.7.9</jacoco.version>
<sonar.scanner.version>3.3.0.603</sonar.scanner.version>
<!-- Used to locate the profile specific configuration file. -->
<build.profile.id>dev</build.profile.id>
<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Only unit tests are run by default. -->
<skip.unit.tests>false</skip.unit.tests>
<powermock.version>1.7.0</powermock.version>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<mockito1.version>1.10.19</mockito1.version>
<mockito2.version>2.8.9</mockito2.version>
<easymock.version>3.4</easymock.version>
<assertj-core.version>3.8.0</assertj-core.version>
<junit.version>4.12</junit.version>
<spring.version>3.0.5.RELEASE</spring.version>
<commons-lang3.version>3.4</commons-lang3.version>
<sonar.jacoco.reportPaths>${project.basedir}/target/coverage.exec</sonar.jacoco.reportPaths>
</properties>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<classifier>runtime</classifier>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito-common</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-support</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito1.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.scanner.version}</version>
</plugin>
</plugins>
</build>
基本就这些了,你是对的,我也错过了报道。在 运行 mvn clean install
之后,您可以在目标文件夹中看到 coverage.exec。默认情况下,声纳正在寻找 jacoco.exec 文件。
您只需添加
即可调整此行为
<sonar.jacoco.reportPaths>${project.basedir}/target/coverage.exec</sonar.jacoco.reportPaths>
到 pom.xml 中的属性部分。在 运行ning mvn clean install sonar:sonar
之后,覆盖范围再次出现在我的本地 sonarqube 中。希望这对你有更多帮助...
根据我对这项研究的看法:
也可以将离线检测 类 与 JaCoCo Java 代理一起使用。在这种情况下,配置取自代理选项。必须以排除预检测 类 的方式配置代理,例如使用“excludes=*”。否则,如果代理在您的情况下再次使用此类 类,它将导致控制台上出现错误消息。
代理 jacocoagent.jar 是 JaCoCo 发行版的一部分,包括所有必需的依赖项。可以使用以下 JVM 选项激活 Java 代理:
-javaagent:[你的路径/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]
对于 JaCoCo 代理选项,请考虑以下内容 link:
http://www.jacoco.org/jacoco/trunk/doc/agent.html
如果您正在使用 pom.xml 和 jacoco 插件进行构建,那么您需要在构建的“准备代理”阶段更新配置,以便排除预检测的 类,如下所示。
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<excludes>
<exclude>*</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我也遇到过同样的问题,但 Gradle。对我来说,问题出在以下说明
debug {
testCoverageEnabled true
}
这是我根据一个教程添加的。它还检测 类(正如 Jacoco 所做的那样),因此抛出异常。
删除这条指令解决了我的问题。
我正在使用 Jacoco 插件进行声纳代码覆盖,并使用 power mock mockito 组合编写 JUnit 测试用例,而当我 运行 mvn clean install 但控制台显示非常非常长的堆栈跟踪链,对于项目中使用的许多 类 不断出现,非常烦人, 异常跟踪类似于以下 -
java.lang.instrument.IllegalClassFormatException: Error while instrumenting class..
Caused by: java.io.IOException: Error while instrumenting class.
Caused by: java.lang.IllegalStateException: Class <class-name> is already instrumented.
我通过以下 link 寻求解决方案,但找不到出路:-
https://github.com/jacoco/jacoco/issues/32
link 说 "The error indicates that there are two JaCoCo agents configured for the same process, Looks like activation of surefire-report-plugin leads to a doubled agent, which is not an exceptional case, but an error in configuration of JVM for tests" 在我的例子中,我猜它甚至不是双重代理,因为我的 argLine 已设置为只有一个 jacoco 代理。
在我的例子中,argLine 设置为以下 -
javaagent:C:\Users\user\.m2\repository\org\jacoco\org.jacoco.agent\0.7.6.201602180812\org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=C:Users\user\git\package\target\jacoco.exec
我的 pom.xml jacoco-sonar 配置条目如下 -
<properties>
<sonar.sources>src/main/java</sonar.sources>
<sonar.tests>src/test/java</sonar.tests>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.language>java</sonar.language>
<sonar.binaries>${project.basedir}/target/classes</sonar.binaries>
<sonar.inclusions>
**/com/abc/service/impl/ABCServiceImpl.java,
**/com/abc/dao/impl/ABCDAOImpl.java
</sonar.inclusions>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<skipTests>false</skipTests>
<argLine>-Xmx1024m -XX:MaxPermSize=256m ${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意:如果我删除了 'default-instrument' 和 'default-restore-instrumented-classes' 的执行元素,那么异常跟踪不会出现,但我的覆盖率 = 0.0%声纳仪表板,使用这两个执行元素为我提供了正确的代码覆盖率,但在控制台上有冗长的堆栈跟踪。任何帮助表示赞赏。
我使用 mockito 而不是 powermock,但我认为与声纳的使用没有任何区别。我使用 Sonar Maven Scanner 进行分析,它非常有效。
我的 pom.xml 包含以下属性
<properties>
<project.source.version>1.8</project.source.version>
<project.target.version>${project.source.version}</project.target.version>
<sonar.scanner.version>3.3.0.603</sonar.scanner.version>
<jacoco.maven.version>0.7.9</jacoco.maven.version>
<sonar.projectName>MyProject</sonar.projectName>
<sonar.projectKey>my-project-key</sonar.projectKey>
<sonar.language>java</sonar.language>
</properties>
并且插件配置如下
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<inherited>true</inherited>
<configuration>
<source>${project.source.version}</source>
<target>${project.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.scanner.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.maven.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
你可以运行分析
mvn clean install sonar:sonar
这会生成一个位于 target/jacoco.exec 的默认 exec 文件,您的本地声纳服务器将使用该文件,并且还包含覆盖范围。
更新答案
使用 powermock 进行了测试。我使用了 powermock jacoco-offline example 并对其进行了一些调整。
那么pom.xml就是
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>org.powermock</groupId>
<artifactId>jacoco-offline</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>JaCoCo Offline with PowerMock</name>
<description>
Example how to get code coverage with PowerMock
</description>
<properties>
<jacoco.version>0.7.9</jacoco.version>
<sonar.scanner.version>3.3.0.603</sonar.scanner.version>
<!-- Used to locate the profile specific configuration file. -->
<build.profile.id>dev</build.profile.id>
<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Only unit tests are run by default. -->
<skip.unit.tests>false</skip.unit.tests>
<powermock.version>1.7.0</powermock.version>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<mockito1.version>1.10.19</mockito1.version>
<mockito2.version>2.8.9</mockito2.version>
<easymock.version>3.4</easymock.version>
<assertj-core.version>3.8.0</assertj-core.version>
<junit.version>4.12</junit.version>
<spring.version>3.0.5.RELEASE</spring.version>
<commons-lang3.version>3.4</commons-lang3.version>
<sonar.jacoco.reportPaths>${project.basedir}/target/coverage.exec</sonar.jacoco.reportPaths>
</properties>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<classifier>runtime</classifier>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito-common</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-support</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito1.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.scanner.version}</version>
</plugin>
</plugins>
</build>
基本就这些了,你是对的,我也错过了报道。在 运行 mvn clean install
之后,您可以在目标文件夹中看到 coverage.exec。默认情况下,声纳正在寻找 jacoco.exec 文件。
您只需添加
即可调整此行为<sonar.jacoco.reportPaths>${project.basedir}/target/coverage.exec</sonar.jacoco.reportPaths>
到 pom.xml 中的属性部分。在 运行ning mvn clean install sonar:sonar
之后,覆盖范围再次出现在我的本地 sonarqube 中。希望这对你有更多帮助...
根据我对这项研究的看法: 也可以将离线检测 类 与 JaCoCo Java 代理一起使用。在这种情况下,配置取自代理选项。必须以排除预检测 类 的方式配置代理,例如使用“excludes=*”。否则,如果代理在您的情况下再次使用此类 类,它将导致控制台上出现错误消息。
代理 jacocoagent.jar 是 JaCoCo 发行版的一部分,包括所有必需的依赖项。可以使用以下 JVM 选项激活 Java 代理:
-javaagent:[你的路径/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]
对于 JaCoCo 代理选项,请考虑以下内容 link: http://www.jacoco.org/jacoco/trunk/doc/agent.html
如果您正在使用 pom.xml 和 jacoco 插件进行构建,那么您需要在构建的“准备代理”阶段更新配置,以便排除预检测的 类,如下所示。
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<excludes>
<exclude>*</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我也遇到过同样的问题,但 Gradle。对我来说,问题出在以下说明
debug {
testCoverageEnabled true
}
这是我根据一个教程添加的。它还检测 类(正如 Jacoco 所做的那样),因此抛出异常。
删除这条指令解决了我的问题。