【问题标题】:Mutiple maven plugin executions with different system property values具有不同系统属性值的多个 Maven 插件执行
【发布时间】:2019-07-20 16:14:36
【问题描述】:

我正在尝试使用名为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 在插件级别声明时,我可以正确访问它。怎么了?

【问题讨论】:

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


    【解决方案1】:

    在 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>
    

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 2021-03-24
      • 1970-01-01
      • 2011-08-07
      • 2016-03-15
      • 2013-09-12
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      相关资源
      最近更新 更多