Maven - (Property|Filter)
Table of Contents
1 - About
A property is used to supplied value of resource files at build time. This process is called filtering.
A property can be:
- one of the values defined in your pom.xml,
- a value defined in the user's settings.xml,
- an environment variable
- a property defined in an external properties file,
- or a system property.
2 - Articles Related
3 - How to
3.1 - Enable filtering
Filtering is dsiable by default. To enable it, you must modify the filtering element of the Pom.xml.
<project> ....... <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
3.2 - Define a property
3.2.1 - Pom.Xml
<project> .... <properties> <my.filter.value>hello</my.filter.value> </properties> </project>
3.2.2 - External properties file
In src/main/filters/
Example: filter.properties:
# filter.properties my.filter.value=hello!
The external property file must be defined in the POM.xml
<build> <filters> <filter>src/main/filters/filter.properties</filter> </filters> ....... </build>
3.2.3 - Configuration Property
To reference a property from the configuration files, the property name uses the following syntax:
${AliasRootElement.element}
where:
- AliasRootElement is of:
- settings for settings.xml
- env for environment variable
- Element is an element of the XML file
Some elements have default values and then don't need to be explicitly defined.
Example:
- ${pom.name} refers to the name of the project,
- ${pom.version} refers to the version of the project,
- ${pom.build.finalName} refers to the final name of the file created when the built project is packaged,
- ${settings.localRepository} refers to the path of the user's local repository).
- ${env.myEnvironmentVariableName} refers the the environment variable myEnvironmentVariableName
3.2.4 - System properties
Filtering resources can also get values from system properties built into Java like:
- java.version
- user.home
- …
3.2.5 - Command Line
Properties can also be defined on the command line using the standard Java -D parameter.
Example: To define the property command.line.prop with the value “hello again”
mvn process-resources "-Dcommand.line.prop=hello again"
3.2.6 - Environment variable
Process (OS) - Environment Variable
${env.variable_name}
3.2.7 - Built-in properties
3.3 - Use them
The property values will be supplied when the resource is filtered.
Example: application.properties in the src/main/resources directory
# application.properties application.name=${pom.name} application.version=${pom.version} java.version=${java.version} command.line.prop=${command.line.prop}
3.4 - Start filtering
mvn process-resources