【问题标题】:How to configure JaCoCo maven plugin from command line如何从命令行配置 JaCoCo maven 插件
【发布时间】:2016-08-13 01:44:33
【问题描述】:

我正在尝试从命令行配置 JaCoCo maven 插件,而不是使用 pom.xml。到目前为止,我已经设法使用命令执行prepare-agent

mvn -X -Djacoco.destFile=./coverage/jacoco.exec clean org.jacoco:jacoco-maven-plugin:prepare-agent install

带输出:

[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) destFile = /src/coverage/jacoco.exec
...

创建./coverage/jacoco.exec 文件,现在我正在尝试运行report 阶段,但我无法为此阶段设置属性。我正在运行命令:

mvn -X -Djacoco.dataFile=./coverage/jacoco.exec -Djacoco.outputDirectory=./jacoco_ut org.jacoco:jacoco-maven-plugin:report

mvn -X -DdataFile=./coverage/jacoco.exec -DoutputDirectory=./jacoco_ut org.jacoco:jacoco-maven-plugin:report

jacoco:report 中没有user propertyjacoco:prepare-agent 中。

我的输出如下:

[DEBUG] Configuring mojo 'org.jacoco:jacoco-maven-plugin:0.7.6.201602180812:report' with basic configurator -->
[DEBUG]   (f) dataFile = /src/target/jacoco.exec
[DEBUG]   (f) outputDirectory = /src/target/site/jacoco
[DEBUG]   (f) outputEncoding = UTF-8
[DEBUG]   (f) project = MavenProject: project:3.2.0-SNAPSHOT @ /src/pom.xml
[DEBUG]   (f) skip = false
[DEBUG]   (f) sourceEncoding = UTF-8
[DEBUG] -- end configuration --

使用默认值。

【问题讨论】:

    标签: java maven plugins jacoco


    【解决方案1】:

    更新到 0.7.8

    GitHub issue 322 已从version 0.7.8jacoco-maven-plugin 解决。从这个版本开始,您可以使用用户属性jacoco.dataFile,因此问题中的命令将按原样运行。

    要在命令行上强制版本,您应该:

    mvn -Djacoco.destFile=./coverage/jacoco.exec clean org.jacoco:jacoco-maven-plugin:0.7.8:prepare-agent install
    

    您还可以在 POM 中配置 jacoco-maven-plugin 并明确指定此版本。

    或保留默认值

    在版本 0.7.8 之前,dataFile 属性没有 user property,因此您将无法这样做。您在调用时正确地覆盖了默认值

    mvn -Djacoco.destFile=./coverage/jacoco.exec clean org.jacoco:jacoco-maven-plugin:prepare-agent install
    

    因为jacoco.destFile 是与prepare-agent 目标的destFile 属性关联的用户属性的名称。

    但是,report 目标的相应 dataFile 属性没有用户属性。所以你最好的选择是保持默认。

    【讨论】:

      【解决方案2】:

      当你不能使用属性时,你可以使用环境变量。

      当我得到 Jacoco Diff Coverage 时 jacoco:report includes cannot use property.

      所以我使用 ${inc_files}

      壳牌:

      echo "+++git diff+++"
      inc_files=`git diff --name-only master | grep -oP 'com/languoguang/xxclouds/quark.*.java' | xargs | sed 's/.java/.class/g' | xargs | sed -e 's/ /,/g'`;
      echo ${inc_files};
      echo "---git diff---";
      
      echo "+++maven test jacoco+++";
      mvn -B -f pom.xml -Dinc_files=${inc_files} clean test -Dmaven.test.failure.ignore=true;
      echo "---maven test jacoco---";
      

      pom.xml:

      <execution>
          <configuration>
              <title>quark-frameworks-coverage-inc</title>
              <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate/jacoco-aggregate-inc/</outputDirectory>
              <includes>${inc_files}</includes>
          </configuration>
          <id>report-aggregate-inc</id>
          <phase>test</phase>
          <goals>
              <goal>report-aggregate</goal>
          </goals>
      </execution>
      

      我希望这会有用。

      【讨论】:

        【解决方案3】:

        我们使用下面的代码:

        mvn clean devtestcov:atest -Dactive.devtest -Dmaven.test.failure.ignore=true -Djacoco-agent.destfile=target/jacoco.exec
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-28
          • 1970-01-01
          • 2015-09-04
          • 2014-10-01
          • 2011-11-16
          • 2016-07-18
          • 2011-06-07
          相关资源
          最近更新 更多