There are six types of properties that you can use within a Maven application POM file:
It is always recommended that you use properties, instead of hardcoding values in application POM files. Let''s look at a few examples.
Let's take the application POM file inside the Apache Axis2 distribution module, available at http://svn.apache.org/repos/asf/axis/axis2/java/core/trunk/modules/distribution/pom.xml. This defines all the artifacts created in the Axis2 project that need to be included in the final distribution. All the artifacts share the same groupId
tag as well as the version
tag of the distribution
module. This is a common scenario in most of the multi-module Maven projects.
Most of the modules (if not all) share the same groupId
tag and the version
tag:
<dependencies> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-java2wsdl</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-kernel</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb</artifactId> <version>${project.version}</version> </dependency> </dependencies>
In the previous configuration, instead of duplicating the version
element, Axis2 uses the project property ${project.version}
. When Maven finds this project property, it reads the value from the project POM version
element. If the project POM file does not have a version
element, then Maven will try to read it from the immediate parent POM file. The benefit here is, when you upgrade your project version
some day, you only need to upgrade the version
element of the distribution
POM file (or its parent).
The previous configuration is not perfect; it can be further improved in the following manner:
<dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>axis2-java2wsdl</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>axis2-kernel</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>axis2-adb</artifactId> <version>${project.version}</version> </dependency> </dependencies>
Here, we also replace the hardcoded value of groupId
in all the dependencies with the project property ${project.groupid}
. When Maven finds this project property, it reads the value from the project POM groupId
element. If the project POM file does not have a groupId
element, then Maven will try to read it from the immediate parent POM file.
The following lists out some of the Maven built-in properties and project properties:
project.version
: This refers to the value of the version
element of the project POM fileproject.groupId
: This refers to the value of the groupId
element of the project POM fileproject.artifactId
: This refers to the value of the artifactId
element of the project POM fileproject.name
: This refers to the value of the name
element of the project POM fileproject.description
: This refers to the value of the description
element of the project POM fileproject.baseUri
: This refers to the path of the project's base directoryThe following is an example that shows the usage of this project property. Here, we have a system
dependency that needs to be referred from a filesystem path:
<dependency> <groupId>org.apache.axis2.wso2</groupId> <artifactId>axis2</artifactId> <version>1.6.0.wso2v2</version> <scope>system</scope> <systemPath>${project.basedir}/lib/axis2-1.6.jar</systemPath> </dependency>
In addition to the project properties, you can also read properties from the USER_HOME/.m2/settings.xml
file. For example, if you want to read the path to the local Maven repository, you can use the ${settings.localRepository}
property. In the same way, with the same pattern, you can read any of the configuration elements that are defined in the settings.xml
file.
The environment variables defined in the system can be read using the env
prefix, within an application POM file. The ${env.M2_HOME}
property will return the path to the Maven home, while ${env.java_home}
returns the path to the Java home directory. These properties will be quite useful within certain Maven plugins.
Maven also lets you define your own set of custom properties. Custom properties are mostly used when defining dependency versions.
You should not scatter custom properties all over the place. The ideal place to define them is the parent POM file in a multi-module Maven project, which will then be inherited by all the other child modules.
If you look at the parent POM file of the WSO2 Carbon project, you will find a large set of custom properties, which are defined in https://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/parent/pom.xml. The following lists out some of them:
<properties> <rampart.version>1.6.1-wso2v10</rampart.version> <rampart.mar.version>1.6.1-wso2v10</rampart.mar.version> <rampart.osgi.version>1.6.1.wso2v10</rampart.osgi.version> </properties>
When you add a dependency to the Rampart JAR, you do not need to specify the version there. Just refer it by the ${rampart.version}
property name. Also, keep in mind that all the custom defined properties are inherited and can be overridden in any child POM file:
<dependency>
<groupId>org.apache.rampart.wso2</groupId>
<artifactId>rampart-core</artifactId>
<version>${rampart.version}</version>
</dependency>
3.143.0.85