Passing System Properties and Environment Variables to unit tests in Maven – Update

Unfortunately there was an bug in my previous post.

I had naively assumed that the syntax for environment variables was the same as for system properties but alas not.

The correct code is:


    package
        <plugins>
        <plugin>
               org.apache.maven.plugins
               maven-surefire-plugin
               2.4.2
               
                   
                       some value
                   
                   
                       <property>
                           acceptance.test.host
                           ${acceptance.test.host}
                       </property>
                   
                
           </plugin>
       </plugins>

Share

Passing System Properties and Environment Variables to unit tests in Maven

This is a bit of dark magic …

<pre>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<environmentVariables>
<SOME_ENV_VARIABLE>some value</SOME_ENV_VARIABLE>
</environmentVariables>
<systemProperties>
<property>
<name>someSystemProperty</name>
<value>${thisCanBePassedIn}</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

I’m afraid I had to update this post as initially I naively assumed that the syntax for environment variables was the same as for system properties. As you can see from above, you have to actually put a separate xml tag in there for the variable.

By putting the value in ${} you can then pass it in with -DsomeSystemProperty from the command line when you do mvn clean install

e.g. :

mvn -DsomeSystemProperty=foobar clean install
Share