【问题标题】:Maven surefire plugin is not using argLine property provided by JacocoMaven surefire 插件未使用 Jacoco 提供的 argLine 属性
【发布时间】:2018-03-28 08:29:24
【问题描述】:

这个问题更侧重于理解 maven 生命周期,而不是解决实际问题。

我们有一个包含多个 Maven 模块的项目。 Jacoco 和 Surefire 插件都在父 pom.xml 中配置如下:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.0</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/jacoco.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
            <argLine>${surefireArgLine}</argLine>
    </configuration>
</plugin>

这个配置效果很好,执行通用目标时会在目标目录下创建 jacoco.exec 文件,例如:

mvn clean install

mvn test

但是如果我们执行以下命令,不会创建 jacoco.exec 文件:

mvn clean install -DskipTests
#other actions here...
mvn jacoco:prepare-agent surefire:test

使用 -X 选项分析日志,surefire 插件表明它将按预期使用 argLine 属性:

<argLine>${surefireArgLine}</argLine>

目标 jococo:prepare-agent 正确生成该变量的值:

argLine set to -javaagent:s:\\m2_repo\\org\\jacoco\\org.jacoco.agent\\0.8.0\\org.jacoco.agent-0.8.0-runtime.jar=destfile=S:\\sources\\sofia2-s4c\\config\\services\\target\\jacoco.exec

但是当 surefire:test 被执行时它不使用 argLine 属性:

[DEBUG]   (s) additionalClasspathElements = []
argLine SHOULD BE HERE!!!!
[DEBUG]   (s) basedir = S:\sources\sofia2-s4c\config\services

我们已经解决了这个执行:

mvn clean install -DskipTests
#other actions here...
mvn test

由于 mvn test 检测到编译的类没有变化,这是有效的。但我想了解为什么第一种方法不起作用。

【问题讨论】:

    标签: maven maven-surefire-plugin jacoco-maven-plugin


    【解决方案1】:

    问题在于surefire插件配置的&lt;argLine&gt;

    来自https://github.com/jacoco/jacoco/issues/964的解决方案:

    • 定义用户属性&lt;argLine&gt;...&lt;/argLine&gt;。肯定会捡起来的。无需使用${surefireArgLine}
    • 在surefire插件配置中保留&lt;argLine&gt;,但写成&lt;argLine&gt;@{argLine} ${surefireArgLine}&lt;/argLine&gt;

    另见https://stackoverflow.com/a/23605812/510583

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      相关资源
      最近更新 更多