我的Jacoco报告目标不受测试阶段的约束

时间:2016-03-29 06:32:07

标签: maven jacoco

jacoco-ut.exec文件是在target / coverage-reports / jacoco-ut.exec中创建的,但没有可见的站点/目录。

以下是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配置后,问题得以解决,报告在站点/目录中生成。

完整插件现在看起来像:

  <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

1 个答案:

答案 0 :(得分:0)

使用此配置,您在使用mvn clean install生成网站时不会遇到任何问题。

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

JaCoco配置

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

        <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>

看看<argLine>${jacoco.it.argLine}</argLine>。此占位符将替换为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>

再次,看看<argLine>${jacoco.argLine}</argLine>。它将包含将JaCoco与单元测试集成所需的配置变量。