Java cannot read variables from pom

I have the following line in my maven.properties file.

   MY_VARIABLE = www.google.com

      

My pom file code looks like this:

    <build>

    <pluginManagement>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.16</version>

                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/*Test*.java</include>
                                <include>**/*Tests*.java</include>
                                <include>**/Test*.java</include>
                            </includes>
                            <files>
                                <file>src/test/resources/maven.properties</file>
                            </files>

                            <systemPropertyVariables>
                                <message>${MY_VARIABLE}</message>
                            </systemPropertyVariables>

                        </configuration>
                    </execution>
                </executions>

            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

      

Finally, I have a piece of code to retrieve the value in a message variable.

   public static String getMsg() 
   {
      final String msg = System.getProperty("message");
      if (StringUtils.isEmpty(msg) || url.startsWith("${")) 
      {
          return "Empty message";
      }
      return msg;
   }

      

So when I call getMsg () method, it always returns msg as Empty message.

Is this some mistake in the pom.xml declaration or is it some kind of problem with the getMsg () function used.

It would be nice if someone could shed some light on this.

Thanks in advance....

+1


source to share


3 answers


If I am not mistaken, you need to use the following syntax

<properties>
  <message>${MY_VARIABLE}</message>
</properties>

      



systemPropertyVariables are for the Surefire plugin, for unit testing

+1


source


I ran into this problem a while ago. I don't know if this is a bug / limitation. The solution I found was to set a variable as a virtual machine argument. This is how it currently works for me:

Update the pom as follows:



                   <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.17</version>
                        <configuration>
                            <argLine>-DMY_VARIABLE=${MY_VARIABLE}</argLine>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

      

Then you can call maven to run your integration tests like this: mvn integration-test -DMY_VARIABLE = www.google.com

0


source


Thanks guys for the help.

I found a way to do this.

Before starting tests, in my java file I am using

 System.setProperty("message" , "welcome message");

      

And that will finally print out the value instead of null.

Greetings

0


source







All Articles