Introduction

Here we will see maven profile and non-profile based plugin execution. Situation may occur when you need to execute plugin(s) for different profile(s) and plugin(s) which do not need any profile for your application.

For example, let’s say you want to copy some configuration files into some specific location for local environment where your application will be deployed and run locally. We also need to replace the tokens in XML configuration files for application when it runs locally.

But for other environments your application may not require such files to be copied into a specific location, rather the application requires those files in a zip format.

You may also need to activate a profile by default.

Therefore we will see in this tutorial how to configure the maven profile and non-profile based plugin execution.

Prerequisites

Maven 3.6.1, Java 8 or 12, Eclipse 4.12

Maven filtering replace multiple tokens during build

Build File – pom.xml

Now we will create a pom.xml file with the following content.

In the below file we have declared one profile local which is executed only for local environment when executed using command mvn clean install -P local.

The other <build/> tag which is no-profile based and executed always.

If may also activate a particular profile by setting true to <activateByDefault/> tag. This is shown using default profile.

You can find the assembly.xml file and configuration XML file from the link as I mentioned in the Prerequisites section above.

<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-profile-nonprofile-plugins</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
	</dependencies>
	
	<profiles>
		<profile>
			<id>local</id>
			<properties>
				<search.home>/web/SearchIndex</search.home>
			</properties>
			<build>
				<plugins>
					<plugin>
						<artifactId>maven-resources-plugin</artifactId>
						<executions>
							<execution>
								<id>copy-resources</id>
								<phase>validate</phase>
								<goals>
									<goal>copy-resources</goal>
								</goals>
								<configuration>
									<outputDirectory>${search.home}/home</outputDirectory>
									<resources>
										<resource>
											<directory>home</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>
		</profile>
		<profile>
			<id>default</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<build>
			  .. executed with no profile
			</build>
		</profile>
	</profiles>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>12</source>
					<target>12</target>
				</configuration>
			</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>

If you do not want to execute for local profile then simply execute mvn clean install command.

That’s all.

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

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