【问题标题】:maven jacoco: not generating code coverage reportmaven jacoco:不生成代码覆盖率报告
【发布时间】:2014-08-20 01:02:59
【问题描述】:

我正在尝试为我的项目的代码覆盖率设置 jacoco

我的项目基于Java 1.8

这是我项目的pom.xml 中的情况

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.5.10.201208310627</version>
        <configuration>
            <output>file</output>
            <append>true</append>
        </configuration>
        <executions>
            <execution>
                <id>jacoco-initialize</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>jacoco-site</id>
                <phase>verify</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>  

然后我运行mvn test 并看到以下内容

$ mvn test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.5.10.201208310627:prepare-agent (jacoco-initialize) @ pennyapp ---
[INFO] argLine set to -javaagent:/Users/harit/.m2/repository/org/jacoco/org.jacoco.agent/0.5.10.201208310627/org.jacoco.agent-0.5.10.201208310627-runtime.jar=destfile=/Users/harit/code/idea/pennyapp/target/jacoco.exec,append=true,output=file
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pennyapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pennyapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/harit/code/idea/pennyapp/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pennyapp ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ pennyapp ---
[INFO] Surefire report directory: /Users/harit/code/idea/pennyapp/shippable/testresults
[INFO] Using configured provider org.apache.maven.surefire.junit4.JUnit4Provider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
objc[13225]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Running HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec - in HelloTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.523 s
[INFO] Finished at: 2014-08-19T17:56:33-07:00
[INFO] Final Memory: 10M/119M
[INFO] ------------------------------------------------------------------------

然后我运行mvn jacoco:report 我看到了

$ mvn jacoco:report
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building pennyapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.5.10.201208310627:report (default-cli) @ pennyapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.176 s
[INFO] Finished at: 2014-08-19T17:56:51-07:00
[INFO] Final Memory: 11M/112M
[INFO] ------------------------------------------------------------------------

那我看target/site/jacoco/index.html看到下面的

问题
- 配置中有什么不正确的地方?
- 如何生成报告?

谢谢

【问题讨论】:

  • 您使用过时版本的 JaCoCo 插件有什么特别的原因吗?对于 Java 8 支持,您必须至少使用 0.7.0 版本(请参阅changelog)。
  • 运行旧版本的jacoco 是个问题。之后就修复了

标签: java maven code-coverage jacoco


【解决方案1】:

您使用过时版本的 JaCoCo 插件有什么特别的原因吗?对于 Java 8 支持,您必须至少使用 0.7.0 版本(请参阅changelog)。

在您的配置中,报告目标绑定到验证阶段,因此运行 mvn test 不会生成任何报告,因为它不运行验证阶段 (test phase comes before verify)。您必须使用mvn verify 来执行测试并生成报告。

JaCoCo 项目提供了示例 Maven 配置。你可以试试“this POM file for a JAR project runs JUnit tests under code coverage and creates a coverage report”。

【讨论】:

    【解决方案2】:

    JaCoco Maven 插件正在覆盖 Surefire argLine,如果您还需要覆盖 argLine,请务必保留 argLine 变量:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.19.1</version>
      <configuration>
        <argLine>-Xmx1024M ${argLine}</argLine> 
      </configuration>
    </plugin>
    

    请注意,您可以更改此属性名称,如 jacoco plugin documentation 中所述。

    【讨论】:

      【解决方案3】:

      这对我有用:

      mvn clean install
      mvn site
      

      即使没有达到最低代码覆盖率并且mvn clean install 失败,mvn site 构建成功并在以下位置创建了覆盖率报告:

      .../target/site/jacoco/index.html
      

      【讨论】:

        【解决方案4】:

        以下作品对我来说没有任何问题

        <plugin>
           <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.4</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>                   
                </execution>
            </executions>
        </plugin>
        

        注意:阶段是“测试”,以便在执行测试后生成报告

        【讨论】:

        • 谢谢。添加在 mvn clean install 上生成的 test 站点路径后
        • 对我来说,test 是关键,现在运行“mvn test”时会分类站点文件夹
        【解决方案5】:

        我有同样的问题。 问题是我在&lt;plugins&gt; 中添加了 jacoco 插件,该插件列在&lt;pluginManagment&gt; 标签内。

          <build>
            <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
              <plugins>
               ...
                <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.4</version>
                <executions>
                  <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                      <goal>prepare-agent</goal>
                    </goals>
                  </execution>
                  <execution>
                    <id>jacoco-report</id>
                    <phase>test</phase>
                    <goals>
                      <goal>report</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
        ...
               
              </plugins>
            </pluginManagement>
        

        &lt;plugins&gt; 添加为&lt;build&gt; 的直接子级后,一切正常。

        <build>
        ....
         <plugins>
              <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.4</version>
                <executions>
                  <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                      <goal>prepare-agent</goal>
                    </goals>
                  </execution>
                  <execution>
                    <id>jacoco-report</id>
                    <phase>test</phase>
                    <goals>
                      <goal>report</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
        ...
        <build>
        

        【讨论】:

        • 这很奇怪,但救了我很多。
        • 很高兴听到@laksys。
        【解决方案6】:

        有类似的问题,我不知道根本原因,但避免在构建中解决问题

        <pluginManagement>
        

        按照对我有用的 pom

            <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                <modelVersion>4.0.0</modelVersion>
                <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.4.3</version>
                    <relativePath /> <!-- lookup parent from repository -->
                </parent>
                <groupId>com.example</groupId>
                <artifactId>SampleSpring</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <packaging>war</packaging>
                <name>SampleSpring</name>
                <description>Demo project for SampleSpring</description>
                <properties>
                    <java.version>1.8</java.version>
                    <maven.compiler.source>1.8</maven.compiler.source>
                    <maven.compiler.target>1.8</maven.compiler.target>
                    <start-class>com.example.spring.SampleSpringApplication</start-class>
        
                    <jacoco.version>0.8.3</jacoco.version>
                    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
                    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
                    <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
                    <sonar.language>java</sonar.language>
        
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                    </dependency>
        
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                        <scope>provided</scope>
                    </dependency>
        
                    <!-- Testing -->
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-test</artifactId>
                        <scope>test</scope>
                            <exclusions>
                                <exclusion>
                                    <groupId>org.junit.vintage</groupId>
                                    <artifactId>junit-vintage-engine</artifactId>
                                </exclusion>
                            </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-test</artifactId>
                        <scope>test</scope>
                    </dependency>
                    <!-- Testing -->
        
        
                </dependencies>
        
                <build>
                    <plugins>
        
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                        </plugin>
        
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>3.0.0-M1</version>
                        </plugin>
        
                        <!--jacoco plugin -->
                        <plugin>
                            <groupId>org.jacoco</groupId>
                            <artifactId>jacoco-maven-plugin</artifactId>
                            <version>0.8.5</version>
                            <configuration>
                                <output>file</output>
                                <append>true</append>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>jacoco-initialize</id>
                                    <goals>
                                        <goal>prepare-agent</goal>
                                    </goals>
                                </execution>
                                <execution>
                                    <id>jacoco-site</id>
                                    <phase>verify</phase>
                                    <goals>
                                        <goal>report</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <!--jacoco plugin end -->
        
                        <!--Sonarqube plugin -->
                        <plugin>
                            <groupId>org.sonarsource.scanner.maven</groupId>
                            <artifactId>sonar-maven-plugin</artifactId>
                            <version>3.6.0.1398</version>
                        </plugin>
                        <!--Sonarqube plugin end -->
        
                    </plugins>
                </build>
        
            </project>
        

        【讨论】:

          【解决方案7】:

          在尝试其他解决方法之前,首先检查 “切换‘跳过测试’模式”在 IntelliJ IDEA 的 Maven 工具栏上是否处于活动状态。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-09-09
            • 2022-06-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多