Using maven build profiles to generate different artifacts
Tags: maven, programming, java, springboot
Published: 2023-12-20 05:30:22

> Want to use the same maven Pom file to create two or more different types of artifacts (jar/war etc.) based on some condition?

You can do it by Modifying pom files manually based on your requirement every time you build but this becomes tedious if you have to do it continuously. 
Also, it will be a puzzle for developers who start working on the project when you are no longer there to guide.

We could use maven build profiles instead.

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.

Among other uses of build profiles, you can use this approach to:

- Create builds using different parameters values
- Create builds with different artifact types

---

Below I am going to show you a way with which you can use the maven profiles for the purpose of creating 
- A Jar library file to be used as a dependency by another project.
OR
- A War file to be deployed to the server directly.


## Changes Required to the POM file for creating profiles:
1. There will be some fields that will require values as parameters instead of hardcoded values:
    - artifactId - artifact id of the artifact created as end result
    - packaging - to define the type of packaging like war or jar
  - name - name of the artifact to be created
  - description - description of the artifact
2. Now these values defined as parameters above will be passed actual values based on the build profile.
3. The first profile is for generating war package for the springboot project having id "generate-war"
We have set activateByDefault as true for this, we want the default behaviour to be creation of war file.
4. The second is for generating library package to be used a library in other projects.
5. You can add the dependencies based on your requirements.

```
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.16</version>
		<relativePath/> 
	</parent>
	<groupId>com.abc.test</groupId>
	<artifactId>${packaging.artifactId}</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>${packaging.type}</packaging>
	<name>${packaging.name}</name>
	<description>${packaging.description}</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--Other dependencies-->
	</dependencies>

	<profiles>
		<profile>
			<id>generate-war</id>
			<properties>
				<packaging.artifactId>test-api</packaging.artifactId>
				<packaging.type>war</packaging.type>
				<packaging.name>test-api</packaging.name>
				<packaging.description>TEST API project</packaging.description>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<build>
				<plugins>
					<plugin>
						<groupId>org.springframework.boot</groupId>
						<artifactId>spring-boot-maven-plugin</artifactId>
					</plugin>
					<!--You can any other plugins required for specific profile here-->
				</plugins>
			</build>
		</profile>
		<profile>
			<id>generate-library</id>
			<properties>
				<packaging.artifactId>test-api-lib</packaging.artifactId>
				<packaging.type>jar</packaging.type>
				<packaging.name>test-api-lib</packaging.name>
				<packaging.description>TEST API library</packaging.description>
			</properties>
			<build>
				<finalName>test-api-lib</finalName>
			</build>
		</profile>
	</profiles>

	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
			   <groupId>org.apache.maven.plugins</groupId>
			   <artifactId>maven-install-plugin</artifactId>
			</plugin>
	    </plugins>
	</build>
</project>

```

To build using these profiles we use the -P parameter with the profile id as below:

```
mvn install -P generate-library
```

```
mvn install -P generate-war
```

Also if you are using intellij Idea then you can use the plugin as well to select the profile.

![Maven Build with Profiles in Intellij IDEA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st0zta5iahy35q74cmsx.png)

Hope this helps you if you ever face such a scenario!