【发布时间】: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