Unable to get <systemPropertyVariables> variable values ​​from pom

Hi, I am working on a java maven project in which I have to define some variables in the pom.xml file.

I defined the following variable 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 to access the my.value variable, I use the following Java code snippet.

  String testdata = System.getProperty("my.value"); System.out.println(testdata); 

But console output always shows me null , even when I set the value of a variable.

Can someone point out what is wrong here?

Thanks in advance.

EDIT: I also tried declaring systemPropertyVariables under the maven-failsafe-plugin , but 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 in the line above.

Edit: Sorry to post this answer earlier.

I run it as a JUnit test using the plugin code ... / plugin you specified, 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 extract from the pom.xml file, and the condition I wrote is that

if the value in the variable is empty from the beginning with $ {, then returns localhost: 8080 else returns url.

So, if you could point me to something wrong here

+8
java eclipse windows maven
source share
2 answers

Works for me with maven-3.2.3 on Windows using JDK 1.6.0_67

Created a project with maven-archetype-quickstart ...

Corresponding pom lines added ... combining the right 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> 

Matching Strings in AppTest.java

 /** * Rigourous Test :-) */ public void testApp() { System.out.println(System.getProperty("my.value")); System.out.println(System.getProperty("buildDirectory")); assertTrue( true ); } 

Corresponding output of mvn test

 ------------------------------------------------------- TESTS ------------------------------------------------------- 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 on github.

+2
source share

@Raghuram Thanks for the help.

I have a way around this problem.

In my java file, in the @Before annotation, I set the variable value as follows

  System.setProperty("my.value","this value"); 

And now it works fine.

Greetings.

Note: Guys, I apologize for sending another question as an answer .. :(

-2
source share

All Articles