11

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

Ian
  • 1,221
  • 1
  • 18
  • 30
curiousity
  • 4,703
  • 8
  • 39
  • 59

4 Answers4

7

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.

TranceVibes
  • 126
  • 2
  • 8
awwsmm
  • 1,353
  • 1
  • 18
  • 28
  • hi i have a problem with this, I also have a webResource but if I add your code it gonna crash whith it – Robs Jan 19 '23 at 15:45
1

The mechanism chiefly in charge to transfer Maven properties to Java application is provided by the Maven Resource Plugin

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.

This will replace any placeholders, e.g. ${my.property} in any of the files in src/main/resources with the corresponding property from your pom, e.g. <property><my.property>test</my.property></property>

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

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
0

I assume you want to get it in the code to check something right? You can use filtering from maven that will inject the value in the source code, similar to the filtering option http://mojo.codehaus.org/templating-maven-plugin/

Nadir
  • 1,369
  • 1
  • 15
  • 28
  • Alternatively, maven resources plugin could filter resources by maven property – Kelvin Ng Oct 16 '14 at 12:27
  • I could not call filter, all logic is only in java method - perhaps there is a way to use resources - but how to read their values runtime? – curiousity Oct 16 '14 at 12:33
  • the templating plugin from the link above can modify java files for you. You can do something like String version = ${getdownload-webapp.version} and then use the version variable in your logic – Nadir Oct 16 '14 at 12:48
  • I need not only version but also some other string values. buy the way the plugin you suggested is in pre release – curiousity Oct 16 '14 at 13:30
-1
String version = project.getProperties().getProperty("getdownload-webapp.version");

Where project is of type MavenProject

Bradley Smith
  • 39
  • 2
  • 5