How to create user defined properties in Maven

Custom properties or variables are useful to keep your Maven pom.xml file more easy to read and maintain.

File : pom.xml


<project>
...
  <properties>
     <my.plugin.version>1.5</my.plugin.version>
  </properties>
...
</project>

In above pom.xml, you can refer “my.plugin.version” via code ${my.plugin.version}.

Example 1

A classic use case is used to define a jar or plugin version.


	<properties>
		<spring.version>3.1.2.RELEASE</spring.version>
	</properties>
 
	<dependencies>
 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
	</dependencies>

If you want to upgrade Spring to 3.1.5, just change the “spring.version” to 3.1.5, and all the dependencies will be affected.

Example 2

Another common use case is used to define a long file path.


<properties>
	<project.theme.name>default</project.theme.name>
	<project.resources.build.folder>
		${project.build.directory}/temp-resources/${project.theme.name}/
	</project.resources.build.folder>
</properties>
	
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>2.5</version>
	<executions>
	  <execution>
	    <id>copy-resources</id>
	    <goals>
		<goal>copy-resources</goal>
	    </goals>
	    <configuration>
	    <outputDirectory>
                 ${project.resources.build.folder}
            </outputDirectory>
	    //...	
Note
Furthermore, Maven comes with many useful project properties like project.build.directory}, project.build.directory}, make sure you check this Maven Properties Guide

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Gita Digumarti
10 years ago

I am passing a command line argument with mvn command : mvn install “-Dmyproperty=mine”

1. can i capture the argument in pom.xml as :

${myproperty}

2. if i want to use the value of myproperty , in my java code. how can i refer it?

Ghassen Khalil Ati
10 years ago
Reply to  Gita Digumarti

Hi Gita, I have the same question !
Did you got any response or solutions ?

Nikhil Borpe
13 years ago

what is the use of ‘profile’,’reporting’,’plugin’ tags?

Nikhil Borpe
13 years ago

what is the use of , and tag in maven?