How to get property value from pom.xml?

I added node to my pom.xml:

<properties>
        <getdownload-webapp.version>1.5</getdownload-webapp.version>
</properties>

      

how can i get this value 1.5 in code?

String version = System.getProperty("getdownload-webapp.version"); // output version = null

      

This code gave me null while running (

ps: there is no settings.xml in this project

+3


source to share


4 answers


So you have got such property.

<properties>
    <getdownload-webapp.version>1.5</getdownload-webapp.version>
</properties>

      


Create a file as follows in your Maven project.

  src/main/resources/project.properties

      

Or as follows, if it's just for testing.

  src/test/resources/project.properties

      

Add this line to a new file. Note that you must not use the "properties" prefix (for example, do not write "properties.getdownload-webapp.version").

  version=${getdownload-webapp.version}

      


Note that you can also add such flags to the file.



  debug=false

      


If not already done, you must enable Maven filtering for your project. This is a function that will look for placeholders inside your project files, which will be replaced with values ​​from the pom. To continue, you need to add these lines inside <build>

your pom.xml file. Here's how to do it using src / main:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    ...

      

And here's how to do it for src / test:

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

      


Finally, in your source code ( MyClassName.java

) add a block like

  Properties props = new Properties();
  props.load(MyClassName.class.getClassLoader().getResourceAsStream("project.properties"));
  String version = props.getProperty("version");

      

You can add as many variables as you like to the file project.properties

and load each one using this method.

+1


source


I am assuming you want to get it in code to test something correctly? You can use filtering from maven which will inject the value into the source code, similar to the filtering parameter http://mojo.codehaus.org/templating-maven-plugin/



0


source


The mechanism mainly responsible for passing Maven properties to a Java application is provided by the Maven resource plugin.

The plugin is part of the Maven Super Pom and runs during the process-resources

Jar Default lifecycle phase . The only thing you need to do is active filtering .

This will replace any placeholders, for example, ${my.property}

in any of the files in, src/main/resources

with the corresponding property from your pom, for example<property><my.property>test</my.property></property>

How you make this property available to your Java application is up to you - reading it from the classpath will work.

0


source


String version = project.getProperties().getProperty("getdownload-webapp.version");

      

Where the project is of type MavenProject

-1


source







All Articles