Replace property values at the time of maven build

This post explains how to replace property values at the time of maven build.

Replace property value in resource files (non-java files) at the time of maven build

Create one resource file filter-test.txt in $PROJECT_HOME/src/main/resources with content as follow.

Hello ${resource.name}

Now add following lines in pom.xml file of your project.

 
<properties>
	<resource.name>RESOURCE</resource.name>
</properties>

<build>  
	<resources>
		<!-- Filter resource files -->
		<resource>
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>
	....
</build>

Build your project using following command.

mvn clean install

Check content of filter-test.txt file under $PROJECT_HOME/target/classes. It should be as follow.

Hello RESOURCE

Please note that at the time of build ${resource.name} in filter-test.txt file is replaced with “RESOURCE” which we have defined in pom.xml file as property.

Now you can overwrite property value defined in pom.xml file using command line argument at the time of build. Do build using following command.

mvn clean install -Dresource.name=Atul

Check content of the filter-test.txt file under $PROJECT_HOME/target/classes. It should be as follow.

Hello Atul

Replace property value in java files at the time of maven build

Create one FilterTest.java file under $PROJECT_HOME/src/main/java/com/elitejavacoder/test with content as follow.

package com.elitejavacoder.test;

public class FilterTest {

	private static String NAME = "${java.name}";

		public static void main(String[] args) {

		try {
			System.out.println("n Value of name : " + NAME);
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

Now add following lines in pom.xml file of your project.

<properties>
	<java.name>JAVA</java.name>
</properties>

<build>  
	<resources>
		<!-- Filter Java files -->
		<resource>
			<filtering>true</filtering>
			<directory>src/main/java</directory>
			<targetPath>../filtered-sources/java</targetPath>
			<includes>
				<include>**/*.java</include>
			</includes>
		</resource>
	</resources>

	<!-- change default source directory to filtered-sources/java -->
	<sourceDirectory>target/filtered-sources/java</sourceDirectory>
	....
</build>

Please note that in case of java files, we filter java files as resource files and copy them to filtered-sources/java. Then we change default maven source directory to point filtered java files directory using <sourceDirectory>.

Now do a build using following command.

mvn clean install

Run this class to check output. Please note that I have created executable jar so I will run this class using following command from $PROJECT_HOME/target. For information on how to create executable jar please refer this post.

java -jar replace-property-values-at-build-time-1.0.0-SNAPSHOT.jar

The output should be as follow.

Value of name : JAVA

Please note that at the time of maven build, ${java.name} in FilterTest.java is replaced with “JAVA” which we have defined in pom.xml file as property. You can check this in compiled class file as well using any decompiler.

Now you can overwrite this property value defined in pom.xml file using command line argument at the time of build. Do build using following command.

mvn clean install -Djava.name=Atul

Again run this class to check output using following command.

java -jar replace-property-values-at-build-time-1.0.0-SNAPSHOT.jar

The output should be as follow.

Value of name : Atul

I hope this post helps!

2 comments on “Replace property values at the time of maven build

  1. NobleUplift says:

    Thank you so much!!! I've been trying to get filtering on .java files working for a while, but I couldn't figure it out myself or find a tutorial on it until now.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>