简体   繁体   中英

How to configure JaCoCo maven plugin from command line

I'm trying to configure JaCoCo maven plugin from command line instead of using pom.xml . I have managed to execute prepare-agent so far with command:

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

with output:

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

which creates ./coverage/jacoco.exec file and now I'm trying to run report stage but I am not able to set properties for this stage. I'm running command:

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

or

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

as in jacoco:report there is no user property as in jacoco:prepare-agent .

i have output like:

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

with default values.

Update to 0.7.8

The GitHub issue 322 has been resolved as of version 0.7.8 of the jacoco-maven-plugin . Starting from this version, you can use the user property jacoco.dataFile , so the commands in the question would work as-is.

To force the version on the command line, you should have:

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

You can also configure the jacoco-maven-plugin inside your POM and specify this version explicitly.

Or keep the defaults

Before version 0.7.8, there is no user property for the dataFile attribute, so you won't be able to do that. You are correctly overriding the default value when you're invoking

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

because jacoco.destFile is the name of the user property associated with the destFile attribute of the prepare-agent goal.

However, there are no user property for the corresponding dataFile attribute of the report goal. So your best bet would be to keep the default.

When You can not use property, you can use environment variable.

When I get Jacoco Diff Coverage jacoco:report includes cannot use property.

So I use ${inc_files}

Shell:

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>

I hope this will be useful.

我们使用以下代码:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM