我尝试使用名为 testVar
的系统属性的不同值多次执行下面的插件。我的 pom.xml
中有以下插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<skip>false</skip>
<forkCount>1</forkCount>
<threadCount>3</threadCount>
</configuration>
<executions>
<execution>
<id>before-run</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<systemPropertyVariables>
<testVar>aaa</testVar>
</systemPropertyVariables>
</configuration>
</execution>
<execution>
<id>main-run</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<systemPropertyVariables>
<testVar>bbb</testVar>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
运行 System.getProperty("testVar")
时,我收到 null
。但是,当在插件级别声明时,我可以正确访问 testVar
。怎么了?
最佳答案
maven-surefire-plugin的配置中有多个execution
标签,即目标test
在默认阶段test<执行多次
。实际上,您的插件配置会导致 3 次测试异常:
- default-test(由surefire自动触发,未设置自定义系统属性)
- 运行前(在 POM 系统属性集中首先定义)
- main-run(如 POM 中第二个定义,系统属性集)
mvn 测试
使用 Maven 3.5.4:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.app.ExampleTest
getProperty:null
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-surefire-plugin:2.14.1:test (before-run) @ app ---
[INFO] ...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.app.ExampleTest
getProperty:aaa
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-surefire-plugin:2.14.1:test (main-run) @ app ---
[INFO] ...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.app.ExampleTest
getProperty:bbb
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
考虑覆盖 default-test
执行,以便正确应用您的配置。示例:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<executions>
<execution>
<id>before-run</id>
...
</execution>
<execution>
<id>default-test</id>
...
</execution>
</executions>
关于java - 具有不同系统属性值的多个 Maven 插件执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897033/