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

标签 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>

运行 System.getProperty("testVar") 时,我收到 null。但是,当在插件级别声明时,我可以正确访问 testVar 。怎么了?

最佳答案

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

  1. default-test(由surefire自动触发,未设置自定义系统属性)
  2. 运行前(在 POM 系统属性集中首先定义)
  3. main-run(如 POM 中第二个定义,系统属性集)

mvn 测试 使用 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>

关于java - 具有不同系统属性值的多个 Maven 插件执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897033/

相关文章:

通过编辑pom在Maven项目中编码Java文件

java - sleep 定时器任务

java - 在哪里可以找到java中预定义方法/类的源代码?

java - 无法在STS中创建maven项目

java - IntelliJ IDEA + Maven 在 iml 文件中需要什么依赖项?

java - Selenium Grid - 有什么方法可以防止在多台机器上的节点之间共享剪贴板?

java - 具有属性的 Maven 版本

java - 用于第三方 servelet 的 Guice Singleton Servlet Binding 解决方法

java - 无法加载库: xuggle; version: 5; Using POM

maven - 如何在不同的 POM 之间共享 POM 片段