Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get property value from pom.xml?

Tags:

java

maven

I have added a node in my pom.xml:

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

how could I get this 1.5 value 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

like image 272
curiousity Avatar asked Oct 16 '14 12:10

curiousity


People also ask

How do I access property in POM xml?

Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time.

How do you find the value of POM files?

The plugin is part of the Maven Super Pom and executed during the process-resources phase of the Jar Default Lifecyle. The only thing you have to do is to active filtering. How you make this property then available to your Java application is up to you - reading it from the classpath would work.

What is properties in POM xml?

Properties can be defined in a POM or in a Profile. The properties set in a POM or in a Maven Profile can be referenced just like any other property available throughout Maven. User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.


1 Answers

So you have a property like this.

<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 is for tests only.

  src/test/resources/project.properties

Add this line inside the new file. Please note that you should not prefix with "properties" (e.g. don't write "properties.getdownload-webapp.version").

  version=${getdownload-webapp.version}

Note that you can also add flags like this to the file.

  debug=false

If not already done, you have to enable Maven filtering for your project. It is the feature that will look for placeholders inside the files of your project to be replaced by values from the pom. In order to proceed, you need add these lines inside the <build> tag of your pom.xml file. This is how to do with src/main:

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

And here is how to do 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 want to the project.properties file and load each one using this method.

like image 110
awwsmm Avatar answered Sep 22 '22 06:09

awwsmm