当前位置: 首页 > 知识库问答 >
问题:

如何让JaCoCo与Maven和JUnit 4.11合作?

卫诚
2023-03-14

我很难让JaCoCo和Maven合作。我一直遇到任何一个。

Skipping JaCoCo execution due to missing execution data file.

或者

The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.8.2:check are missing or invalid

我似乎也无法让JaCoCo只运行mvn clean test,相反,我必须运行 mvn-clean test-JaCoCo:reportor

我尝试了多种编辑POM文件的方法,例如添加destFile和dataFile的配置,以及此处的POM设置:https://howtodoinjava.com/junit5/jacoco-test-coverage/还有这里https://www.lambdatest.com/blog/reporting-code-coverage-using-maven-and-jacoco-plugin/。任何帮助都将不胜感激。下面是我的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.NAME.PROJECTNAME</groupId>
  <artifactId>PROJECTNAME</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>PROJECTNAME</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>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>4.4.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>17.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://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.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.2</version>
          <configuration>
            <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
            <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
          </configuration>
          <executions>
            <execution>
              <goals>
                <goal>prepare-agent</goal>
              </goals>
            </execution>
            <!-- attached to Maven test phase -->
            <execution>
              <id>report</id>
              <phase>test</phase>
              <goals>
                <goal>report</goal>
              </goals>
            </execution>
          </executions>
        </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>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <groupId>org.openjfx</groupId>
          <artifactId>javafx-maven-plugin</artifactId>
          <version>0.0.8</version>
          <configuration>
              <mainClass>HelloFX</mainClass>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

共有1个答案

邢勇
2023-03-14

这是因为您正在使用

根据上的Maven文档

pluginManagement:是一个可以在插件旁边看到的元素。插件管理以非常相似的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,而是打算配置从这个构建继承的项目构建。然而,这仅配置在子元素或当前POM中的plugins元素中实际引用的插件。孩子们完全有权利覆盖插件管理定义。

简而言之,您将使用

我删除了<代码>

-> mvn clean verify

...
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (default) @ PROJECTNAME ---
[WARNING] The artifact xml-apis:xml-apis:jar:2.0.2 has been relocated to xml-apis:xml-apis:jar:1.0.b2
[INFO] argLine set to -javaagent:/home/codejournal/.m2/repository/org/jacoco/org.jacoco.agent/0.8.2/org.jacoco.agent-0.8.2-runtime.jar=destfile=/tmp/maven-jacoco/target/coverage-reports/jacoco-unit.exec
...
...
[INFO] --- jacoco-maven-plugin:0.8.2:report (report) @ PROJECTNAME ---
[INFO] Loading execution data file /tmp/maven-jacoco/target/coverage-reports/jacoco-unit.exec
[INFO] Analyzed bundle 'PROJECTNAME' with 1 classes
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ PROJECTNAME ---
[INFO] Building jar: /tmp/maven-jacoco/target/PROJECTNAME-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.141 s
[INFO] Finished at: 2022-04-24T02:18:30+05:30
[INFO] ------------------------------------------------------------------------

-> cat target/site/jacoco/jacoco.csv 

GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
PROJECTNAME,io.codejournal.maven.jacoco,Hello,7,0,0,0,3,0,2,0,2,0

 类似资料:
  • 如何让Maven插件与Sonar Scanner配合使用? 我正在RHEL 7.5上运行以下程序: 纱线版本:Hadoop 3.1.0 mvn——版本:ApacheMaven 3.5.3 SonarQube 7.1 节点-v:v9。5 npm-v:5.6.0 我带着pom去这个目录。xml文件:/opt/sonaqube/web/META-INF/maven/org。声纳来源。声纳网络 我运行以下

  • 以下是我的POM中的一些相关片段: 版本:Maven 3.0.4、Tomcat Maven Plugin2.1、Jacoco 0.6.2.201302030002、Java7

  • 执行maven时发生以下错误(mvn clean install) 无法在项目测试上执行目标org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:prepare-agent(默认值)ng:目标org.jacoco:jacoco-maven-plugin:0.7.2.201409121644:prepare-agent失败:无法在插件“org.jacoc

  • 我试图通过Sonar ut ant JaCoCo runTests示例了解JaCoCo和Sonar是如何协同工作的:https://github.com/SonarSource/sonar-examples/blob/master/projects/code-coverage/ut/ant/ut-ant-jacoco-runTests/build.xml 我在本地机器上安装声纳,默认配置。然后,我

  • 有办法让这成为可能吗? 感谢您的关注!

  • 我有一个使用maven(scala-maven-plugin)构建的scala项目(有几个java文件)。我们为代码覆盖率插入了jacoco(jacoco-maven-plugin),这会生成良好的scala代码覆盖率。我们可以在/target中的典型位置看到html/csv报告,scala覆盖范围很好。 但是,我们无法使用sonar来处理scala文件。插件运行并发送java报道,所以我知道它从