Maven Filtering Replace Multiple Tokens During Build


In this post I will discuss about replacing the multiple tokens in XML file using maven-resources-plugin. Situation may occur where you need to replace some place holders in the XML file during build of your application and need to create a zip file under the target directory, then you may use this example as a reference. Such XML files or zip file may be required for configuring the application or project. I will use here maven-assembly-plugin to build the zip file under target directory.


Maven 3.6.1 – 3.8.5, Java 8+

Project Setup

Create a maven based standalone project in your favorite IDE or tool with the following details:

Group Id: com.roytuts
Artifact Id: maven-replace-token-xml

Sample XML File

Let’s say, you have the below sample XML file – test.xml under xml folder under the project directory.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config sharedLib="library">
	<cores adminPath="/admin" hostPort="${jetty.port}"
		<core name="activeCustomers" instanceDir="activeCustomers"
			dataDir="[[search.home]]/activeCustomers" />

		<core name="carriers" instanceDir="carriers"
			dataDir="[[search.home]]/carriers" />

		<core name="cities" instanceDir="cities"
			dataDir="[[search.home]]/cities" />

		<core name="customerAddresses" instanceDir="customerAddresses"
			dataDir="[[search.home]]/customerAddresses" />

		<core config="config.xml" name="servicerequest_questions_en"
			instanceDir="servicerequest_questions_en" schema="schema.xml"
			dataDir="[[search.home]]/servicerequest_questions" />

In the above file, you see there are two types of place holders – ${..} and [[...]].

In your XML file you can have only one or more place holders like these.

So, I will replace those place holders with required values during maven build.

At the same time, I will also copy the XML file after replacement into a new file under target/config directory.

Finally, I will build the zip directory using maven-assembly-plugin.

Creating Assembly Config

I will create an assembly.xml file under the project root directory with the below content.

You may find more on assembly configuration here.


Updating Build File – pom.xml

Update the default generated pom.xml file to include the required plugins.

In the below pom.xml file, I have defined properties to be replaced for the place holders in XML file.

This project does not have any other resources so it does not include any dependency.

<project xmlns=""






In the above file, I have included maven-compiler-plugin for our Java resources, though it is not required for this project as there is no Java resource file.

The important plugin is maven-resources-plugin here that does the required job for filtering or replacements and copy the resource into new file under target/config folder.

I have specified the goal as copy-resources, because I will copy the XML file after replacement into the output directory target/config.

I have specified the resource directory xml from where filtering will be applied to the XML file.

Notice, I have also added two delimiters as the place holders are specified by those two delimiters in the XML file.

Finally, I have added maven-assembly-plugin to create zip file for the new XML file.

You may find more on assembly configuration and creating zip file using maven-assembly-plugin here.

Building the Project

If you execute mvn install in command line tool or from Eclipse Run As -> Maven install, then you will see the following changes on the original file under target/config directory.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config sharedLib="library">
	<cores adminPath="/admin" hostPort="9988"
		<core name="activeCustomers" instanceDir="activeCustomers"
			dataDir="/web/SearchIndex/activeCustomers" />

		<core name="carriers" instanceDir="carriers"
			dataDir="/web/SearchIndex/carriers" />

		<core name="cities" instanceDir="cities"
			dataDir="/web/SearchIndex/cities" />

		<core name="customerAddresses" instanceDir="customerAddresses"
			dataDir="/web/SearchIndex/customerAddresses" />

		<core config="config.xml" name="servicerequest_questions_en"
			instanceDir="servicerequest_questions_en" schema="schema.xml"
			dataDir="/web/SearchIndex/servicerequest_questions" />

You will also see a zip file has been created under target folder. If you open the zip file you will see that test.xml file is there inside this zip file.

Source Code


Leave a Reply

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