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

时间:2019-02-27 02:15:47

标签: java maven pom.xml maven-surefire-plugin

我尝试使用一个名为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时,我可以正确访问它。怎么了?

2 个答案:

答案 0 :(得分: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 标签。我不知道 execution 标签可以在maven surefire插件中使用。

现在用于访问系统属性。

System.getProperty("target");
System.getProperty("mobile");
System.getProperty("deviceType");

如何在Maven命令中使用

mvn test -Dtarget=Native -Dmobile=Android -DdeviceType=RealDevice

希望它能解决您的问题。

答案 1 :(得分:0)

在maven-surefire-plugin的配置中,您有几个export const login = (username,password) => { return dispatch=>{ const basicAuth = 'Basic ' + btoa(username + ':' + password); let myHeaders = new Headers(); myHeaders.append('Authorization', basicAuth); myHeaders.append('Content-Type', 'application/json'); fetch(`${baseUrl}api/user/login`, { withCredentials: true, headers: myHeaders }) .then(function (response) { return response.json(); }) .then(function (json) { dispatch(setLoginInfo(json)) }) .catch(err =>{ console.log(err) dispatch(loginFailed()) }); } } 标签,即目标execution在默认阶段test中执行了几次。实际上,您的插件配置会导致 3 测试执行:

  1. 默认测试(通过surefire自动触发,未设置自定义系统属性)
  2. 运行前(在POM中首先定义,系统属性集)
  3. 主运行(在POM中定义,系统属性集第二个)

test与Maven 3.5.4:

mvn test

请考虑覆盖------------------------------------------------------- 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