How to get the value of the <version> property in pom.xml?

Asked

Viewed 1,761 times

2

I need to get the value of the current version of the application in various parts of the code. A priori I thought of creating a class containing the version:

public class Version {
    public static String getVersion(){
        return "0.1.0";
    }
}

But there must be a better way to do this (I believe).

I saw that Java has a versioning, but I don’t understand if it’s for the same purpose I’m looking for.

Well, since I’m using Maven and there’s the property <version> in the configuration file, I thought that at the time of maintenance it would be easier to update only this property to use throughout the project.

The question is: Is it possible to obtain this property? If so, how to do?
If not, you could give me alternatives?

1 answer

1


The Maven Resources Plugin allows you to perform Maven variable overrides on files (Resources) of the project.

This plugin is part of the standard Maven execution plan in the phases process-resources and process-test-resources, so what you need to do is instruct the plugin to replace the variables during these phases.

Following the example of the documentation, you simply need to add the following configuration:

<project>
  ...
  <name>My Resources Plugin Practice Project</name>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

Then any file in the specified directory will be processed by Maven. You could then create a txt or properties containing the version:

versao=${project.version}

This way, Maven is in charge of placing the correct version when packaging the project.

If there are other files in the project it may be best to prevent them from being unnecessarily or improperly processed. You can specify multiple tags <resource> containing tags <include> and <exclude> to specify which files you want to process and which should remain intact.

Example:

<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>
  • Thanks Luiz, I will do some tests. That Package Java also serves for versioning?

  • @Renan The Java versioning specification is interesting and contains important information if you are implementing a library or module that is used with various components and dependencies of different versions. For example, you may have a jar with the API interfaces in the version 1.0 and an API implementation jar in version 1.0.10, therefore the manifesto jar would contain different versions of specification and implementation. However, for simpler cases this information is irrelevant.

  • The Maven Archiver Plugin can generate this information for you. So you could read this information while running by uploading the file META-INF/MANIFEST.MF of jar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.