Maven Filtering Replace Multiple Tokens During Build

Introduction

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.

Prerequisites

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}"
		hostContext="${hostContext}">
		<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" />
	</cores>
</config>

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.

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>zip</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>target/config</directory>
			<useDefaultExcludes>true</useDefaultExcludes>
			<outputDirectory>./</outputDirectory>
		</fileSet>
	</fileSets>
</assembly>

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="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>maven-replace-token-xml</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
		<jetty.port>9988</jetty.port>
		<hostContext>localhost</hostContext>
		<search.home>/web/SearchIndex</search.home>
	</properties>

	<dependencies>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.1.0</version>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>validate</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<outputDirectory>target/config</outputDirectory>
							<resources>
								<resource>
									<directory>xml</directory>
									<filtering>true</filtering>
									<includes>
										<include>**/*.xml</include>
									</includes>
								</resource>
							</resources>
							<delimiters>
								<delimiter>${*}</delimiter>
								<delimiter>[[*]]</delimiter>
							</delimiters>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<finalName>configuration</finalName>
					<descriptors>
						<descriptor>${basedir}/assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>create-archive</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>

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"
		hostContext="localhost">
		<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" />
	</cores>
</config>

You will also see a zip file configuration.zip 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

Leave a Reply

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