Maven Profile And Non-profile Based Plugin Execution

Introduction

Here you 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. You 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 than the application requires those files in a zip format.

You may also need to activate a default profile.

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

Prerequisites

Maven 3.6.3 – 3.8.5, Java 8+

Maven filtering replace multiple tokens during build

Build File – pom.xml

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

In the below file I 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 a no-profile based and executed always.

You 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>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</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>
			</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. Hope you got an idea how to execute maven profile and non-profile based plugins from your pom file.

Source Code

Download

Leave a Reply

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