【问题标题】:My Jacoco report goal is not binding to the test phase我的 Jacoco 报告目标不绑定到测试阶段
【发布时间】:2016-07-16 13:54:46
【问题描述】:

在 target/coverage-reports/jacoco-ut.exec 中创建了 jacoco-ut.exec 文件,但看不到 site/ 目录。

这里是 Jacoco 配置:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <skipTests>false</skipTests>
      <argLine>${surefireArgLine}</argLine>
      <excludes>
        <exclude>**/integration/*.java</exclude>
      </excludes>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <skip>false</skip>
      <output>file</output>
      <append>true</append>
    </configuration>
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

这是控制台输出:

[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (pre-unit-test)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <address>${jacoco.address}</address>
  <append>true</append>
  <classDumpDir>${jacoco.classDumpDir}</classDumpDir>
  <destFile default-value="${project.build.directory}/jacoco.exec">/home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec</destFile>
  <dumpOnExit>${jacoco.dumpOnExit}</dumpOnExit>
  <exclClassLoaders>${jacoco.exclClassLoaders}</exclClassLoaders>
  <inclBootstrapClasses>${jacoco.inclBootstrapClasses}</inclBootstrapClasses>
  <inclNoLocationClasses>${jacoco.inclNoLocationClasses}</inclNoLocationClasses>
  <jmx>${jacoco.jmx}</jmx>
  <output>file</output>
  <pluginArtifactMap>${plugin.artifactMap}</pluginArtifactMap>
  <port>${jacoco.port}</port>
  <project>${project}</project>
  <propertyName>surefireArgLine</propertyName>
  <sessionId>${jacoco.sessionId}</sessionId>
  <skip default-value="false">false</skip>
</configuration>
[DEBUG] -----------------------------------------------------------------------

[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:report (post-unit-test)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <dataFile default-value="${project.build.directory}/jacoco.exec">/home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec</dataFile>
  <outputDirectory default-value="${project.reporting.outputDirectory}/jacoco">/home/stephane/dev/java/projects/x/x/x/target/site/jacoco-ut</outputDirectory>
  <outputEncoding default-value="UTF-8">${project.reporting.outputEncoding}</outputEncoding>
  <project>${project}</project>
  <skip default-value="false">false</skip>
  <sourceEncoding default-value="UTF-8">${project.build.sourceEncoding}</sourceEncoding>
</configuration>
[DEBUG] =======================================================================

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (pre-unit-test) @ x ---
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=0, ConflictMarker.markTime=1, ConflictMarker.nodeCount=158, ConflictIdSorter.graphTime=0, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=47, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=4, ConflictResolver.conflictItemCount=108, DefaultDependencyCollector.collectTime=191, DefaultDependencyCollector.transformTime=5}
[DEBUG] org.jacoco:jacoco-maven-plugin:jar:0.7.6.201602180812:

[DEBUG]   Excluded: org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1
[DEBUG]   Excluded: classworlds:classworlds:jar:1.1-alpha-2
[DEBUG]   Excluded: org.apache.maven.wagon:wagon-provider-api:jar:1.0-alpha-6
[DEBUG] Configuring mojo org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:prepare-agent from plugin realm ClassRealm[plugin>org.jacoco:jacoco-maven-plugin:0.7.6.201602180812, parent: sun.misc.Launcher$AppClassLoader@70dea4e]
[DEBUG] Configuring mojo 'org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:prepare-agent' with basic configurator -->
[DEBUG]   (f) append = true
[DEBUG]   (f) destFile = /home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec
[DEBUG]   (f) output = file
[DEBUG]   (f) pluginArtifactMap = {org.jacoco:jacoco-maven-plugin=org.jacoco:jacoco-maven-plugin:maven-plugin:0.7.6.201602180812:, backport-util-concurrent:backport-util-concurrent=backport-util-concurrent:backport-util-concurrent:jar:3.1:compile, org.codehaus.plexus:plexus-interpolation=org.codehaus.plexus:plexus-interpolation:jar:1.11:compile, junit:junit=junit:junit:jar:4.8.2:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:1.5.6:compile, org.apache.maven.shared:file-management=org.apache.maven.shared:file-management:jar:1.2.1:compile, org.apache.maven.shared:maven-shared-io=org.apache.maven.shared:maven-shared-io:jar:1.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.2.1:compile, org.apache.maven.doxia:doxia-sink-api=org.apache.maven.doxia:doxia-sink-api:jar:1.1:compile, org.apache.maven.doxia:doxia-logging-api=org.apache.maven.doxia:doxia-logging-api:jar:1.1:compile, org.apache.maven.reporting:maven-reporting-impl=org.apache.maven.reporting:maven-reporting-impl:jar:2.1:compile, org.apache.maven.doxia:doxia-core=org.apache.maven.doxia:doxia-core:jar:1.1.2:compile, xerces:xercesImpl=xerces:xercesImpl:jar:2.8.1:compile, commons-lang:commons-lang=commons-lang:commons-lang:jar:2.4:compile, commons-httpclient:commons-httpclient=commons-httpclient:commons-httpclient:jar:3.1:compile, commons-codec:commons-codec=commons-codec:commons-codec:jar:1.2:compile, org.apache.maven.doxia:doxia-site-renderer=org.apache.maven.doxia:doxia-site-renderer:jar:1.1.2:compile, org.apache.maven.doxia:doxia-decoration-model=org.apache.maven.doxia:doxia-decoration-model:jar:1.1.2:compile, org.apache.maven.doxia:doxia-module-xhtml=org.apache.maven.doxia:doxia-module-xhtml:jar:1.1.2:compile, org.apache.maven.doxia:doxia-module-fml=org.apache.maven.doxia:doxia-module-fml:jar:1.1.2:compile, org.codehaus.plexus:plexus-i18n=org.codehaus.plexus:plexus-i18n:jar:1.0-beta-7:compile, org.codehaus.plexus:plexus-velocity=org.codehaus.plexus:plexus-velocity:jar:1.1.7:compile, org.apache.velocity:velocity=org.apache.velocity:velocity:jar:1.5:compile, commons-collections:commons-collections=commons-collections:commons-collections:jar:3.2:compile, commons-validator:commons-validator=commons-validator:commons-validator:jar:1.2.0:compile, commons-beanutils:commons-beanutils=commons-beanutils:commons-beanutils:jar:1.7.0:compile, commons-digester:commons-digester=commons-digester:commons-digester:jar:1.6:compile, commons-logging:commons-logging=commons-logging:commons-logging:jar:1.0.4:compile, oro:oro=oro:oro:jar:2.0.8:compile, xml-apis:xml-apis=xml-apis:xml-apis:jar:1.0.b2:compile, org.jacoco:org.jacoco.agent=org.jacoco:org.jacoco.agent:jar:runtime:0.7.6.201602180812:compile, org.jacoco:org.jacoco.core=org.jacoco:org.jacoco.core:jar:0.7.6.201602180812:compile, org.ow2.asm:asm-debug-all=org.ow2.asm:asm-debug-all:jar:5.0.4:compile, org.jacoco:org.jacoco.report=org.jacoco:org.jacoco.report:jar:0.7.6.201602180812:compile}
[DEBUG]   (f) project = MavenProject: com.x:3.2.1 @ /home/stephane/dev/java/projects/x/x/x/pom.xml
[DEBUG]   (f) propertyName = surefireArgLine
[DEBUG]   (f) skip = false
[DEBUG] -- end configuration --
[INFO] surefireArgLine set to -javaagent:/home/stephane/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/home/stephane/dev/java/projects/x/x/x/target/coverage-reports/jacoco-ut.exec,append=true,output=file

仅此而已。

更新:我运行了以下命令:mvn jacoco:report -X 它说了一些有趣的事情:

[DEBUG]   (f) dataFile = /home/stephane/dev/java/projects/x/x/x/target/jacoco.exec
[DEBUG]   (f) outputDirectory = /home/stephane/dev/java/projects/x/x/x/target/site/jacoco
[DEBUG]   (f) outputEncoding = UTF-8
[DEBUG]   (f) project = MavenProject: com.x:3.2.1 @ /home/stephane/dev/java/projects/x/x/x/pom.xml
[DEBUG]   (f) skip = false
[DEBUG]   (f) sourceEncoding = UTF-8
[DEBUG] -- end configuration --
[INFO] Skipping JaCoCo execution due to missing execution data file:/home/stephane/dev/java/projects/x/x/x/target/jacoco.exec

当我让它创建一个 jacoco-ut.exec 文件时,它正在查找一个 jacoco.exec 文件。

自从文件创建以来,这有点令人惊讶:

$ ll target/coverage-reports/jacoco-ut.exec 
-rw-rw-r-- 1 stephane 2.9M Mar 29 09:47 target/coverage-reports/jacoco-ut.exec

我想我需要在报告配置中的某处指定该文件名。

更新:在 Jacoco 配置中移动上面的 destFile 和 dataFile 配置后,问题得到解决,并且在 site/ 目录中生成报告。

完整的插件现在看起来像:

  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
      <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
      <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
      <skip>${skipTests}</skip>
      <output>file</output>
      <append>true</append>
    </configuration>
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

虽然插件是在 plugins 元素中定义的,而不是在 pluginManagement 元素中定义的,但要获取报告我仍然需要执行命令:

mvn jacoco:report

我希望在运行命令时生成报告:

mvn clean install

【问题讨论】:

    标签: maven jacoco


    【解决方案1】:

    使用此配置,生成带有mvn clean install 的站点应该不会有任何问题。

    在站点文件夹中会有两个不同的文件夹:jacoco 包含单元测试报告和 jacoco-it 包含集成测试报告。

    JaCoco 配置

    配置 Jacoco 以公开配置变量以集成单元测试 (jacoco.argLine) 和集成测试 (jacoco.it.argLine) 并生成包含集成测试报告的文件夹 (jacoco-it):

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.4.201502262128</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <excludes>
                                <exclude>**/*Test</exclude>
                            </excludes>
                            <includes>
                                <include>com.vectorsf.*</include>
                            </includes>
                            <propertyName>jacoco.argLine</propertyName>
                        </configuration>
                    </execution>
    
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
    
                    <execution>
                        <id>prepare-agent-it</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${project.build.directory}/jacoco-integration.exec</destFile>
                            <excludes>
                                <exclude>**/*IT</exclude>
                            </excludes>
                            <includes>
                                <include>com.vectorsf.*</include>
                            </includes>
                            <propertyName>jacoco.it.argLine</propertyName>
                        </configuration>
                    </execution>
    
                    <execution>
                        <id>report-it</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                            <dataFile>${project.build.directory}/jacoco-integration.exec</dataFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    故障安全配置

            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <redirectTestOutputToFile>true</redirectTestOutputToFile>
                            <includes>
                                <include>**/*IT.java</include>
                            </includes>
                            <excludes>
                                <exclude>**/Application.java</exclude>
                            </excludes>
                            <argLine>${jacoco.it.argLine}</argLine>
                            <encoding>UTF-8</encoding>
                            <runOrder>alphabetical</runOrder>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    看看&lt;argLine&gt;${jacoco.it.argLine}&lt;/argLine&gt;。这个占位符将被 JaCoco 插件指定的所有参数替换。它将包含将 JaCoco 与集成测试集成所需的配置变量。

    Surefire 配置

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                    <excludes>
                        <exclude>**/*IT.java</exclude>
                        <exclude>**/Application.java</exclude>
                    </excludes>
                    <argLine>
                        ${jacoco.argLine}
                    </argLine>
                    <!-- Force alphabetical order to have a reproducible build -->
                    <runOrder>alphabetical</runOrder>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.15</version>
                    </dependency>
                </dependencies>
            </plugin>
    

    再次查看&lt;argLine&gt;${jacoco.argLine}&lt;/argLine&gt;。它将包含将 JaCoco 与单元测试集成所需的配置变量。

    【讨论】:

    • 感谢您分享您的配置。但我的问题不是我无法运行 Jacoco 插件,而是我无法在安装阶段运行它。我们的配置之间唯一不同的是您使用准备包阶段来实现报告目标。所以我尝试了它并用准备包阶段替换了我的测试阶段。但它并没有改变任何东西,问题仍然存在。
    • 尝试使用mvn verify触发jacoco执行
    • 调用验证阶段没有区别。如问题所述,我已经在运行安装阶段,该阶段在验证阶段之后。如果需要,请参阅maven.apache.org/guides/introduction/… 的阶段。
    • 是的,你是对的。使用 mvn install 就足够了。有点奇怪,正确配置了 jacoco,您应该会看到所有报告。使用我共享的配置,我没有任何问题。每次执行 mvn clean install 时,我都可以看到 jacoco 报告。我也试过你的 Jacoco 版本,它在我的项目中运行良好。您能否检查一下在 mvn 执行期间是否可以看到此跟踪:[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (report-it) @ yourproject ---
    • 嗨,很抱歉回来晚了。我终于注意到了一件事。使用 -DskipTests=false 选项运行命令时,我看不到您上面的 INFO 级别输出。但是,如果我不使用该选项并且跳过了测试,那么我会看到:`[INFO] 跳过了测试。 [INFO] [INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (jacoco-unit-test-report) @MyApp ---
    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2019-10-23
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多