Getting variables from pom.xml java

I am using eclipse with maven to test mobile automation on a mobile web page.

I am defining the following in my pom.xml

<properties>
    <MY_VARIABLE>www.google.com/</MY_VARIABLE>
</properties>

      

but when i call this using

String testurl1 = System.getProperty("MY_VARIABLE");

      

it always returns null.

I have also tried the following way to define a variable

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <systemPropertyVariables>
        <MY_VARIABLE>www.google.com</MY_VARIABLE>
        </systemPropertyVariables>
    </configuration>
</plugin>

      

but still getting the value as null.

I could use some help Thanks.

+3


source to share


1 answer


Your config won't work in eclipse as there is no reliable m2e support to be sure. Maven surefire plugin extends the new process and provides it systemPropertyVariables

. Your config will work if you run tests from the command line, eg.

mvn surefire:test

      



To run it in both worlds (command line and eclipse) I do it like this ...

  • Create src/test/resources/maven.properties

  • Edit the file maven.properties

    and put the properties it needs, for example

    project.build.directory=${project.build.directory}
    MY_VARIABLE=${MY_VARIABLE}
    
          

  • Enable resource filtering for test resources

    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
        ...
    </build>
    
          

  • Load properties into your test and access them

     Properties mavenProps = new Properties();
     InputStream in = TestClass.class.getResourceAsStream("/maven.properties");
     mavenProps.load(in);
    
     String buildDir = mavenProps.getProperty("project.build.directory");
     String myVar = mavenProps.getProperty("MY_VARIABLE");
    
          

+3


source







All Articles