Hi I am working on a java maven project in which I have to define some variables in the pom.xml file.
I have defined a variable as follows in my pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<includes>
<include>**/*Test*.java</include>
<include>**/*Tests*.java</include>
<include>**/Test*.java</include>
</includes>
<systemPropertyVariables>
<my.value>NOTNULL</my.value>
</systemPropertyVariables>
</configuration>
</plugin>
To try accessing the my.value
variable, I am using the following piece of Java code.
String testdata = System.getProperty("my.value");
System.out.println(testdata);
But the console output always shows me null
even when I set the value of the variable.
Can anyone point out what is wrong here?
Thanks in advance.
EDIT : I have also tried declaring the systemPropertyVariables
under the maven-failsafe-plugin
but with no change.
NOTE: When I try to convert the testdata line of code as follows,
String testdata = System.getProperty("my.value").toString();
I get a NullPointer Exception at the above line.
Edit: Sorry for posting this as an answer earlier..
I am running it as JUnit test using the plugin ... /plugin code you provided but here is my console output..
21 Oct 2014 12:36:56,973 main INFO s.MyClass - Default Implicit timeout set in Driver to: 100
21 Oct 2014 12:36:56,973 main INFO s.MyClass - Default URL for server is set to: http://localhost:8080
---- null
The URL is what i am trying to retrieve from the pom.xml file and the condition i have written is that
if the value in the variable is empty of starts with ${ then return localhost:8080 else return the url.
So if you could point me to something wrong here
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.
To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.
Pinging forked JVM Simply the mechanism checks the Maven PID is still alive and it is not reused by OS in another application. If Maven process has died, the forked JVM is killed.
systemPropertyVariables. Maven Surefire plugin provides the configuration parameter systemPropertyVariables to set system properties. The properties defined here will be available in the unit tests.
Works for me with maven-3.2.3
on Windows
with JDK 1.6.0_67
Created a project with maven-archetype-quickstart
...
Added relevant pom lines... combining surefire example with specific lines in the question above.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<my.value>NOTNULL</my.value>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>
</configuration>
</plugin>
Relevant lines in AppTest.java
/**
* Rigourous Test :-)
*/
public void testApp()
{
System.out.println(System.getProperty("my.value"));
System.out.println(System.getProperty("buildDirectory"));
assertTrue( true );
}
Relevant output from mvn test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
NOTNULL
C:\Users\raghu\Documents\GitHub\mvn-examples\test-properties\target
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in c
om.mycompany.app.AppTest
Here is the project in github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With