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

时间:2015-10-23 02:30:06

标签: 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=<...>/target/jacoco.exec

但是,当maven尝试运行JaCoCo时,.exec还没有创建:

[INFO] Skipping JaCoCo execution due to missing execution data file:<...>/target/jacoco.exec

在maven跳过JaCoCo执行后,jacoco.exec最终会被创建。因此,如果我在没有清理的情况下重新运行构建,我仍然可以生成AHP报告。

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

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

  • jacoco-行家-插件
  • vertx-行家-插件
  • 行家资源-插件
  • 行家依赖性-插件
  • 行家-万无一失-插件
  • 行家故障安全-插件
  • 行家-万无一失报告-插件
  • 行家组装-插件

我确实在构建输出中看到一条可疑消息:

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ <module> ---
[INFO] Changes detected - recompiling the module!

我不确定这是否相关,但它在跳过消息之前出现了两次,并且没有出现在JaCoCo正常工作的模块中。

有什么想法吗?

*编辑 - 这里是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>

我不确定该插件管理部分究竟在做什么,但是评论它并不能解决任何问题。我也尝试将JaCoCo插件配置放在surefire / failsafe配置之上,以便对于共享相同目标的插件有重要意义,但这也没有帮助。

*编辑2 - 看起来这个问题确实存在问题。评论它们以某种方式修复了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>

任何人都知道为什么?

6 个答案:

答案 0 :(得分:4)

只是对已经给出的答案的补充。 可能会发生在你的maven-surefire-plugin配置中你已经使用argLine配置来覆盖类似所用内存的东西。如果这样做,jacoco-maven-plugin设置的argline将不会被使用而无法生成jacoco报告。 在这种情况下,将属性名称分配给jacoco-maven-plugin配置,然后在maven-surefire-plugin 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>

[...]

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <printSummary>false</printSummary>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
                <forkCount>3</forkCount>
                <reuseForks>true</reuseForks>
                <argLine>${surefireArgLine} -Xmx1024m -noverify</argLine>                  
            </configuration>
        </plugin>

答案 1 :(得分:3)

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

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

  2. 确定插件执行的阶段。例如对于maven-failsafe-plugin,这可能是integration-test

  3. 更改JaCoCo插件的配置,以便稍后在build lifecycle中执行report目标。例如。如果在jacoco.exec阶段生成了integration-test文件,则可以在report阶段执行post-integration-test目标。

答案 2 :(得分:1)

我没有找到任何解决方案,但我做了以下事情:

mvn clean jacoco:prepare-agent install 创建文件jacoco.exec,但执行mvn sonar:sonar not create file。

解决方案:

第1步: mvn clean jacoco:prepare-agent install

第2步: 从/ src / main / resources 复制文件jacoco.exec 第3步:

在pom.xml上添加下一个插件

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

第4步: mvn clean install

第5步:

mvn声纳:声纳

READY

添加我的插件插件

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

答案 3 :(得分:1)

对我来说,exec文件没有生成的原因之一是当我放置prepare-agent时,在pluginManagement的插件部分中准备包目标

当我尝试相同的外部插件管理时,我成功运行相同的jacoco-exec文件是使用mvn clean install命令生成的

我的pomfile如下:

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

答案 4 :(得分:1)

在我的情况下,解决方案就是@massimo所提到的,我覆盖了<surefireArgLine>标签内的内存值,删除标签后它开始生成报告并显示代码覆盖范围。

答案 5 :(得分:0)

我还遇到了相同的问题,其中将jacoco插件添加到了父pom,并且jacoco.exec文件正在为其中一个子模块生成。我必须删除maven surefire插件的argLine参数,然后生成了它。