The Maven POM hosts a number of implicit and explicit properties which you can use to flexibly configure your build. Besides a set of pre-defined properties such as the POM’s properties (e. g. ${project.version}), the operation system’s environment variables (${env.*}), or Java system properties (e.g. ${java.home}) you can define your own set of properties helping you decouple your build configuration from some particular environment.
Now it would be nice if you could access these properties directly from your source code. A common use case where this is desirable is to show the current version number of your build in some info field on your UI. In this post I’ll show you how to easily accomplish this using Maven’s resource filtering feature.
More specifically, it is Maven’s Resources Plugin which is responsible for including build properties into your resources. The trick is to enable resource filtering in your POM and have Maven substitute all variable references in your resources with their respective values.
In your POM, you enable resource filtering by specifying the directory (and optionally individual resources) whose contents should be filtered. The following configuration will include main.properties into the filtering process while excluding all other resource files:
<build> ... <resources> <!-- include main.properties --> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>main.properties</include> </includes> </resource> <!-- exclude everything else --> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>main.properties</exclude> </excludes> </resource> ... </resources> ... </build>
Using the <exludes> and <includes> sections, you can further narrow down the set of files to be included into the filtering process.
The properties file main.properties can contain an entry referring to the version property of the POM or any other Maven property:
# main.properties fooapp.current.version = ${project.version}
During Maven’s process-resources life-cycle phase, which is executed before the compile phase by default, this variable is substituted with the current value from the POM resulting in something like
# main.properties fooapp.current.version = 1.0-SNAPSHOT
being copied to the build target. From this point, it is easy to load these filtered properties into your application to further process them there, for instance by displaying the current version number in your UI.
Einsortiert unter:Build, config and deploy Tagged: maven, POM Image may be NSFW.
Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.
