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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Ghassen Khalil AtiGita DigumartiNikhil BorpeMaven, sveliamo un POM di segreti, Ovvero... Parliamo delle impostazioni del processo di build | Cose Non Javiste Recent comment authors
newest oldest most voted
Gita Digumarti
Guest
Gita Digumarti

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
Guest
Ghassen Khalil Ati

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

Nikhil Borpe
Guest
Nikhil Borpe

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

Nikhil Borpe
Guest
Nikhil Borpe

what is the use of , and tag in maven?

trackback
Maven, sveliamo un POM di segreti, Ovvero... Parliamo delle impostazioni del processo di build | Cose Non Javiste

[…] (EN) How To Create User Defined Properties In Maven […]