Maven 2.1.0 not passing on system properties to Java virtual machine
Asked Answered
A

3

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); 
Alluvium answered 5/5, 2009 at 9:13 Comment(0)
A
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"
Alluvium answered 5/5, 2009 at 9:14 Comment(3)
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"Iinde
Please note, that vor maven 3 you just use mvn -Dsystem.test.property=test test. Maven propagetes the property to the tests.Becalm
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.Warram
C
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>
Chancechancel answered 15/5, 2009 at 17:54 Comment(3)
Nowadays use <systemPropertyVariables> instead of <systemProperties>. See maven.apache.org/plugins/maven-surefire-plugin/examples/…Audient
I've tried this, and ${...} doesn't seem to be replaced with the property value passed to maven...Chimerical
@Chimerical - not sure what the issue is, could be a misspelling somewhere, but this worked quite well for me.Chancechancel
A
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.

Amoritta answered 15/7, 2010 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.