Maven JaCoCo在跳过JaCoCo执行后才生成JaCoCo.exec

Maven JaCoCo在跳过JaCoCo执行后才生成JaCoCo.exec,maven,jacoco,Maven,Jacoco,在我的一个模块中,我无法通过JaCoCo生成AHP报告。当构建开始时,我看到JaCoCo正确地设置了argLine: [INFO] jacoco.agent.argLine set to -javaagent:<...>/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=<...>/ta

在我的一个模块中,我无法通过JaCoCo生成AHP报告。当构建开始时,我看到JaCoCo正确地设置了argLine:

[INFO] jacoco.agent.argLine set to -javaagent:<...>/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=<...>/target/jacoco.exec
[INFO]jacoco.agent.argLine设置为-javaagent:/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=/target/jacoco.exec
但是,在maven尝试运行JaCoCo时,.exec尚未创建:

[INFO] Skipping JaCoCo execution due to missing execution data file:<...>/target/jacoco.exec
[INFO]由于缺少执行数据文件而跳过JaCoCo执行:/target/JaCoCo.exec
在maven跳过jacoco执行之后,jacoco.exec最终会被创建。因此,如果我在不清理的情况下重新运行构建,我仍然可以生成AHP报告

我在其他各种问题中看到,我需要小心使用Maven Surefire和JaCoCo。然而,我并没有在我的Surefire插件或任何插件中明确使用argLine。我开始怀疑其他插件是否像JaCoCo那样自动劫持argLine参数

以下是使用的所有插件的列表:

  • jacocomaven插件
  • VertexMaven插件
  • maven资源插件
  • maven依赖插件
  • maven surefire插件
  • maven故障保护插件
  • maven surefire报告插件
  • maven汇编插件
我确实在构建输出中看到一条可疑消息:

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ <module> ---
[INFO] Changes detected - recompiling the module!
[INFO]——maven编译器插件:3.0:compile(默认编译)@---
[信息]检测到更改-重新编译模块!
我不确定这是否相关,但它会在跳过消息之前出现两次,并且不会出现在JaCoCo正常工作的模块中

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <!-- <includes>
                    <include>**/unit/**/*Test*.java</include>
                </includes> -->
            </configuration>
        </plugin>
有什么想法吗

*编辑-这是jacoco配置

    <plugins>
        <...>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.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>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>
                                        jacoco-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [0.7.2.201409121644,)
                                    </versionRange>
                                    <goals>
                                        <goal>prepare-agent</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

org.jacoco
jacocomaven插件
${jacoco.version}
配制剂
报告
准备包装
报告
org.eclipse.m2e
生命周期映射
1.0.0
org.jacoco
jacocomaven插件
[0.7.2.201409121644,)
配制剂
我不确定插件管理部分到底在做什么,但评论它并不能解决任何问题。我还尝试将JaCoCo插件配置置于surefire/failsafe配置之上,以防顺序对共享相同目标的插件有影响,但这也没有帮助

*编辑2-看起来问题是surefire的包含。注释掉它们可以修复JaCoCo的.exec生成,JaCoCo工作正常

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <!-- <includes>
                    <include>**/unit/**/*Test*.java</include>
                </includes> -->
            </configuration>
        </plugin>

org.apache.maven.plugins
maven surefire插件
${maven.surefire.plugin.version}

有人知道为什么吗?

要解决这个问题,我将使用以下三个步骤:

  • 确定哪个插件执行正在生成
    jacoco.exec
    文件。为此,可以在启用调试日志的情况下运行Maven(
    mvn-X
    ),并在输出中查找
    jacoco.agent.argLine
    属性名称或其值。还可以查看有效的POM(
    mvn帮助:有效的POM
    )如果相关配置来自父POM。请注意,我猜测这是执行
    maven failsafe插件

  • 确定插件执行的阶段。例如,对于
    maven故障保护插件
    ,这可能是
    集成测试

  • 更改JaCoCo插件的配置,以便稍后在中执行
    报告
    目标。例如,如果在
    集成测试
    阶段生成
    JaCoCo.exec
    文件,则可以在
    集成后测试
    阶段执行
    报告
    目标


  • 只是对已经给出的答案的补充。 在maven surefire插件配置中,您可能已经使用argLine配置来覆盖所用内存之类的内容。如果您这样做,则不会使用jacoco maven插件设置的argLine来生成jacoco报告。 在本例中,为jacocomaven插件配置分配一个属性名,然后在maven-surefire插件argLine参数中引用它

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <!-- prepare agent for measuring unit tests -->
                    <execution>
                        <id>prepare-unit-tests</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <append>true</append>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <!-- Sets the VM argument line used when unit tests are run. -->
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
    
    org.jacoco
    jacocomaven插件
    0.7.9
    准备单元测试
    配制剂
    真的
    ${sonar.jacoco.reportPath}
    surefireArgLine
    
    [……]

    
    org.apache.maven.plugins
    maven surefire插件
    2.19.1
    
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>
                                    ${basedir}/target/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>
                                            ${basedir}/src/main/resources/ </directory>
                                        <includes>
                                            <include>jacoco.exec</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <encoding>cp1252</encoding>
                    </configuration>
                </plugin>
    
    <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>cp1252</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>
                                    ${basedir}/target/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>
                                            ${basedir}/src/main/resources/ </directory>
                                        <includes>
                                            <include>jacoco.exec</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>copy-folder</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <resources>
                                    <resource>
                                        <filtering>false</filtering>
                                        <directory>${project.basedir}/src/main/resources</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <encoding>cp1252</encoding>
                    </configuration>
                </plugin>
                <!-- Plugin Sonar -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.1</version>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.6.201602180812</version>
    
                    <configuration>
                        <destFile>${project.build.directory}/jacoco.exec</destFile>
                    </configuration>
    
                    <executions>
                        <execution>
                            <id>jacoco-initialize</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <destFile>${basedir}/target/jacoco.exec</destFile>
                                <dataFile>${basedir}/target/jacoco.exec</dataFile>
                            </configuration>
                        </execution>
                        <execution>
                            <id>jacoco-site</id>
                            <phase>package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    
    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>mmt</groupId>
      <artifactId>jacoco</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    
      <name>jacoco</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <argLine>-Xms256m -Xmx1524m -XX:MaxPermSize=256m -Duser.language=en</argLine>
     </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.7.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.20.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
        <plugins>
    
    
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.7.201606060606</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>
      </build>
    </project>