38

We use the command line to pass on system properties to the Java virtual machine when running our Hudson builds on a Linux box. It used to work quite well in 2.0.9 by since we upgraded to 2.1.0 it has stopped working altogether. The system properties just never make it to the Java virtual machine.

I have created a small test project and indeed it does not work at all.

This should work just fine with Maven 2.0.9:

mvn2.0.9 -Dsystem.test.property=test test 

But this will fail:

mvn2.1 -Dsystem.test.property=test test 

The Java code simply does this

assertTrue( System.getProperty("system.test.property") != null); 
acdcjunior
  • 114,460
  • 30
  • 289
  • 276
raisercostin
  • 7,909
  • 3
  • 58
  • 68

3 Answers3

54

I don't think this is a problem in either Maven or Surefire plug-in. Else the surefire is behaving differently. It looks like now, when Surefire forks the JVM, will not take all the system properties from the parent JVM.

That's why you should pass whatever system properties you want for the tests, using argLine. So, both these should work

mvn2.1 -Dsystem.test.property=test test -DforkMode=never 

or

mvn2.1 test -DargLine="-Dsystem.test.property=test"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
raisercostin
  • 7,909
  • 3
  • 58
  • 68
  • surprisingly for Locale.getDefault() these worked mvn test -DargLine="-Duser.language=de -Duser.region=DE" and not the mvn test -DargLine="-Dsystem.user.language=de -Dsystem.user.region=DE" – Bibek Shrestha Mar 21 '12 at 14:27
  • 4
    Please note, that vor maven 3 you just use `mvn -Dsystem.test.property=test test`. Maven propagetes the property to the tests. – BetaRide Nov 13 '14 at 10:22
  • 2
    Really helpful answer. I was stuck from 2 days and it work for me, thanks. One more question as per question, if maven version is 2.1.0 then command line argument like "mvn test -Dtesting=testing" not working, but my maven version is 3.0.5 and jvm version is 8. Why it's not working in my case. It's working fine if I pass argument as you guided in answer. Please explain. – kailash gaur Aug 19 '15 at 11:28
13

I've experienced this with the Surefire plug-in. The Surefire plug-in is run under a different JVM instance that is launched by Maven. The command line params are configurable under the surefile-plugin configuration in your pom.xml. Here is a sample of our configuration.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
            <!--
                    By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
                    "**/Test*.java" - includes all of its subdirectory and all java filenames that start with "Test". "**/*Test.java" -
                    includes all of its subdirectory and all java filenames that end with "Test". "**/*TestCase.java" - includes all of
                    its subdirectory and all java filenames that end with "TestCase".
                -->
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <systemProperties>
                    <property>
                        <name>app.env</name>
                        <value>dev</value>
                    </property>
                     <property>
                        <name>oracle.net.tns_admin</name>
                        <value>${oracle.net.tns_admin}</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mike Pone
  • 17,050
  • 12
  • 49
  • 63
  • 4
    Nowadays use instead of . See http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html –  May 11 '10 at 06:10
  • I've tried this, and ${...} doesn't seem to be replaced with the property value passed to maven... – thecoop Jul 23 '13 at 10:08
  • @thecoop - not sure what the issue is, could be a misspelling somewhere, but this worked quite well for me. – Mike Pone Jul 23 '13 at 15:11
2

Be careful to not mix up configuration file with command line arguments. Configuration file (pom.xml) is overriding all the cmd arguments. So don't configure surefire plugin within pom.xml if you want to pass it throught command line like raisercostin explained.