Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Build Profile in Maven and its Types

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

A software system is said to be portable if it can run on an operating system other than the one it was designed for without requiring major editing. The process of making computer software run in a new environment is known as porting. 

Build Profile in Maven and its Types

To make sure that builds are portable, Apache Maven uses top levels of management. Portability, however, is only sometimes feasible. Maven offers the Build Profiles to solve these cases. This article will discuss the build profile in Maven and its types. We will discuss how to trigger profiles. Further, we will discuss how to activate it.

What is Build Profile in Maven?

Maven's profile is nothing more than a set of attributes that enables builds to be adjusted for a specific environment. A build environment is a particular environment that is configured for production and development instances.

A Build Profile in Maven is a collection of configuration parameters. You can use these parameters to override or change the default Maven build parameters. Using build profiles, you may modify builds for various situations, such as production and development environments.

Profile Element

The maven build profiles are specified using the profiles element in the POM(Project Object Model) file. A profile element contains every build profile that was created. The profile element's attributes will override attributes with the same name further up in the POM in terms of values. 

Profile Element

There is an activation element inside every profile element. You can use the activation element to specify the circumstance that triggers the build profile to be applied.  

There are two methods to instruct Maven on which profile to be used.

  1. The settings.xml file can be used to instruct Maven. 
     
  2. Add -P <profile-name > command can be used.
     

The settings.xml file is used to set up a Maven installation. Similar to a pom.xml file, but either user- or globally-specific. Elements that describe the values necessary to configure Maven's execution in various ways are provided by the Maven settings.xml file. These parameters include, 

  1. The location of the local repository.
     
  2. Authentication details.
     
  3. Different servers for the remote repository etc.
     

Types of Build Profiles in Maven

There are three types of Build Profiles supported by Maven 3.0 and above.

  1. Per Project: It is defined in the POM itself(pom.xml).
     
  2. Per User: It is defined in the Maven settings(%USER HOME%/.m2/settings.xml).
     
  3. Global: It is defined in the Global Maven settings(${maven.home}/conf/settings.xml).
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Triggering Build Profiles in Maven

There are various ways to trigger a Build Profile in Maven:

  • Command-line interface.
     
  • By means of Maven settings.
     
  • Depending on environmental factors.
     
  • OS configurations.
     
  • Existing or missing files.

Build Profile Activation

In the Maven settings, profiles can be made active under the <activeProfiles> section. A list of <activeProfile> elements with a profile id within each profile is passed to this section.

The Syntax is as follows:

<settings>
  ...
  <activeProfiles>
    <activeProfile>Coding_Ninjas_profile-1</activeProfile>
  </activeProfiles>
  ...
</settings>

 

The profile contains an "activation" section describing these triggers. Currently, this detection is only capable of determining the JDK version prefix, whether a system property is present, and its value. Here are some illustrations.
 

When the JDK's version begins with "1.6," the settings listed below will activate the profile.

<profiles>
  <profile>
    <activation>
      <jdk>1.6</jdk>
    </activation>
    ...
  </profile>
</profiles>

 

When the system property "environment" is specified with the value "compile," the following example will cause the profile to be activated:

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>compile</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

Build Profile Deactivation

There are various ways to activate profiles, but occasionally, we need to disable one. We can disable a profile with the '!' or '-'.

mvn groupId:artifactId:goal -P !Coding_Ninjas_profile-1,!Coding_Ninjas_profile-2,!?Coding_Ninjas_profile-3

 

OR

mvn groupId:artifactId:goal -P -Coding_Ninjas_profile-1,-Coding_Ninjas_profile-2,-?Coding_Ninjas_profile-3


Profiles tagged as activeByDefault or profiles that their activation config would otherwise activate can be deactivated using this method.

Must Read Apache Server

Frequently Asked Questions

What is POM in Maven?

POM stands for Project Object Model. Maven's basic unit of work is called a POM. It is an XML file with details about the project's config that Maven will use to build the project. The POM includes details about the project and different configuration settings that Maven uses to build the project (s).

How many types of Build Profiles are there?

There are three types of Build profiles in Maven. Per project, Per profile and Global Build profile. There is one more build profile i.e., Profile Descriptor. A profile descriptor is defined in project basedir (profiles.xml) but is not supported in Maven 3.0 and above.

How many Maven Build LifeCycles are there?

There are three maven-build lifecycles. These are default, clean and site lifecycles. The default lifecycle handles the deployment of your project. The clean lifecycle handles your project's cleanup. The site lifecycle handles the setup of your project's website.

Conclusion

We discussed the Maven Project Object Model. We discussed the uses of POM in Maven and the config details of the POM. Further, we discussed the minimum requirements of POm along with an example. We also covered some additional elements of the pom file.

We hope this blog has helped you. We recommend you visit our articles on different topics of Maven, such as

🔥 What do you mean by Maven?

🔥 Maven Configuration Properties.

🔥 Introduction to Maven Commands.

ping command in linux

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Topics covered
1.
Introduction
2.
What is Build Profile in Maven?
2.1.
Profile Element
2.2.
Types of Build Profiles in Maven
3.
Triggering Build Profiles in Maven
3.1.
Build Profile Activation
3.2.
Build Profile Deactivation
4.
Frequently Asked Questions
4.1.
What is POM in Maven?
4.2.
How many types of Build Profiles are there?
4.3.
How many Maven Build LifeCycles are there?
5.
Conclusion