Oni1
Oni1

Reputation: 1505

Reference to the tags in pom.xml

How or generally is it possible to reference to the tags from the pom.xml in java? You can make a reference in the pom with e.g.: <outputDirectory>${project.build.directory}.

But i want to do something like that:

...
System.out.println(${project.version});
...

Is this possible?

Upvotes: 2

Views: 1356

Answers (1)

fluminis
fluminis

Reputation: 4039

At build time you can filter some resource files so that at runtime you read the file and get the values you are looking for:

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

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

Or if it is more complexe you can use the assembly plugin:

https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html

For example: src/main/resources/info.properties

VERSION=${project.version}

And at runtime:

    Properties props = new Properties();
    props.load(this.getClass().getResourceAsStream("info.properties"));

    System.out.println(props.get("VERSION"));

Upvotes: 3

Related Questions