具有不同系统 属性 值的多个 Maven 插件执行
Mutiple maven plugin executions with different system property values
我正在尝试使用名为 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 次测试执行:
- 默认测试(由 surefire 自动触发,未设置自定义系统 属性)
- before-运行(首先在您的 POM 中定义,系统 属性 设置)
- main-运行(在您的 POM 中定义为第二个,系统 属性 设置)
mvn test
与 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>
我正在尝试使用名为 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 次测试执行:
- 默认测试(由 surefire 自动触发,未设置自定义系统 属性)
- before-运行(首先在您的 POM 中定义,系统 属性 设置)
- main-运行(在您的 POM 中定义为第二个,系统 属性 设置)
mvn test
与 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>