我试图使用名为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>
我在运行null
时得到System.getProperty("testVar")
。但是,当我在插件级别声明时,我可以正确访问testVar
。怎么了?
你在maven-surefire-plugin的配置中有几个execution
标签,即目标test
在默认阶段test
中执行了几次。实际上,您的插件配置会导致3个测试执行:
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>
您正在以错误的方式使用系统属性。您可以在下面查看如何使用。
https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html
请看一个例子:
<systemPropertyVariables>
<!-- Appium's VM Variables -->
<target>${target}</target>
<mobile>${mobile}</mobile>
<deviceType>${deviceType}</deviceType>
</systemPropertyVariables>
无需多个systemPropertyVariables标记。我不知道可以在maven surefire插件中使用执行标记。
现在用来访问系统属性。
System.getProperty("target");
System.getProperty("mobile");
System.getProperty("deviceType");
如何在Maven命令中使用
mvn test -Dtarget=Native -Dmobile=Android -DdeviceType=RealDevice
希望它能解决你的问题。