Search code examples
javamavenpom.xmlmaven-surefire-plugin

Mutiple maven plugin executions with different system property values


I am trying to execute the plugin below more than once with a different value of a system property called testVar. I have the following plugin in my 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>

I am getting null when running System.getProperty("testVar"). However, I can access the testVar properly when it is declared at the plugin-level. What's wrong?


Solution

  • You have several execution tags in the configuration of the maven-surefire-plugin, i.e. the goal test is executed several times in the default phase test. Actually, your plugin configuration leads to 3 test exections:

    1. default-test (triggered automatically by surefire, no custom system property set)
    2. before-run (as defined first in your POM, system property set)
    3. main-run (as defined second in your POM, system property set)

    mvn test with 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] ------------------------------------------------------------------------
    

    Consider overriding the default-test execution in order to apply your configuration properly. Example:

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