具有不同系统属性值的多个maven插件执行

问题描述 投票:0回答:2

我试图使用名为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。怎么了?

java maven pom.xml maven-surefire-plugin
2个回答
0
投票

你在maven-surefire-plugin的配置中有几个execution标签,即目标test在默认阶段test中执行了几次。实际上,您的插件配置会导致3个测试执行:

  1. default-test(由surefire自动触发,没有自定义系统属性集)
  2. 运行前(在POM中首先定义,系统属性集)
  3. main-run(在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>

0
投票

您正在以错误的方式使用系统属性。您可以在下面查看如何使用。

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

希望它能解决你的问题。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.