Maven filtering replace multiple tokens during build


In this post we 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. We will use here maven-assembly-plugin to build the zip file under target directory.


Eclipse 4.12, Maven 3.6.1, Java 12(or 8)

Creating Project

Create a maven based standalone project with the following details:

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

Sample XML File

Let’s say we 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 we 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 we will replace those place holders with required values during maven build.

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

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

Creating Assembly Config

We 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 we 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 we 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.

We specify the goal as copy-resources because we will copy the XML file after replacement into the output directory target/config.

We specify the resource directory xml from where filtering will be applied to the XML file.

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

Finally we 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

download source code

Thanks for reading.

Leave a Reply

Your email address will not be published.