Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get <systemPropertyVariables> variable values from pom

Tags:

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

like image 423
Ram Avatar asked Oct 21 '14 04:10

Ram


People also ask

What is POM XML in jar file?

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.

How do I view environment variables in POM XML?

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.

What is forked process in maven?

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.

What is systemPropertyVariables in pom xml?

systemPropertyVariables. Maven Surefire plugin provides the configuration parameter systemPropertyVariables to set system properties. The properties defined here will be available in the unit tests.


1 Answers

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.

like image 173
Raghuram Avatar answered Sep 21 '22 06:09

Raghuram