EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Maven Profile

Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML
Home Software Development Software Development Tutorials Maven Tutorial Maven Profile

Maven-Profile

Introduction to Maven Profile

Maven profile is nothing but a set of configuration files which was used or set in the maven profile which were overridden by default values. By using the maven build profile we can also customize the build of different types of environments like dev vs prod. We are specifying the profile in the pom.xml file by using active profile elements. The active profile elements are triggered in multiple ways in maven.

What is Maven Profile?

It contains great length for the build is portable, in other things, it will allow us to build the configuration inside into the POM which was allowed to avoid the reference of file system and it will make learning more heavily on the local repository for storing the metadata for making the same possible. Basically, most times portability is not possible in multiple conditions, we need to configure the plugin by using the path of the file system. We can modify the profile at the time of build and it is also used to give the target environment.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How do I Activate the Maven Profile?

We need to explicitly specify the –p command into the line flag. We can follow this flag by using a comma delimited list. The profile specified option is activated in addition to any profiles which was activated by using configuration files of configuration or section of the active profile in the settings.xml file.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

The below example shows steps to activate by using settings via the section of active profiles. This section will take the list of elements from the active profile as follows. For activating we need to follow the below steps are as follows.

  • In the first step, we are creating the project in spring initializer. At the time of creating the project, we have given the project name maven_profile and chose the language java.

Group name – com.example

Artifact – maven_profile

Name – maven_profile

Packaging – jar

Java version – 8

Activate Maven Profile 1

  • After creating the project template, now we are opening the project into the spring tool suite as follows.

Activate Maven Profile 2

  • In the below example, we are activating the profile when we have not defined any system property profile as follows.

Code:

<profiles>
<profile>
<activation>
<property>
<name>!debug</name>
</property>
</activation>
</profile>
</profiles>

Activate Maven Profile 3

  • In the below example, we are activating the profile when the system property is not defined or it will define the value which was not true.

Code:

<profiles>
<profile>
<activation>
<property>
<name>maven_profile</name>
<value>!true</value>
</property>
</activation>
</profile>
</profiles>

Activate Maven Profile 4

  • To run the profile which was added in the pom.xml file we need to run the project as follows.

Activate Maven Profile 5

Scope and Skills

While defining the scope of the variable we are taking the below example to find the profile scope as follows. In the below example, we are activating the simple profile to find the scope of the profile.

Code:

<profiles>
<profile>
<activation>
<property>
<name>maven_profile</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>

Scope of profile

In the below example we placed the profile in our pom.xml files, we are declaring the profile only for our specified project. But here we can add the profiles in three locations as follows. If suppose we need to add the profile to the specified project we are adding the same into the pom.xml file. Below is the scope of locations as follows.

  • To add the project specific profile we need to edit the pom.xml file.
  • To add the user specific profile we need to edit the settings.xml file.
  • To add the global specific profile we need to edit the settings.xml file.

The below example shows the scope of user specific and global specific profiles as follows.

Code:

<profiles>
<profile>
<activation>
<property>
<name>maven_profile</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>

Scope of user & global specific profile

Maven Profile Missing File

We can activate the maven profile by using the missing file. In the below example, the activation element is including the OS details. It is triggered when a specified file is missing from the directory. The below example shows how we can activate the profile by using the missing file as follows.

Code:

<profiles>
<profile>
<id>maven_profile</id>
<activation>
<file>
<missing>target/maven</missing>
</file>
</activation>
</profile>
</profiles>

Missing File 1

In the below example we are activating the maven profile missing file by using settings.xml file as follows.

Code:

<profiles>
<profile>
<id>maven_profile_settings</id>
<activation>
<file>
<missing>target/settings</missing>
</file>
</activation>
</profile>
</profiles>

Missing File 2

Maven Profile Settings

To activate the maven profile by using the settings.xml file we need to create the same in a user profile. In the below example we are creating the settings.xml file as follows.

Create settings.xml file

After creating the file now, we are activating the maven profile same as the pom.xml file into the settings.xml file as follows.

Code:

<profiles>
<profile>
<id>maven</id>
<activation>
<file>
<missing>maven_profile/settings</missing>
</file>
</activation>
</profile>
</profiles>

Activate maven Profile

After creating the code of activation now in the below example we are running the maven project as follows.

Run Project

Examples

The below example shows the basic maven profile. Here we are defining the maven id as follows.

Code:

<profiles>
<profile>
<id>maven</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>

Example 1-1

Example 1-2

The below example shows profile for passing different values of properties for the development and production environment. We are creating the properties file.

db.driverClassName = ${db.driverClassName}
db.url=${db.url}
db.username = ${db.username}
db.password = ${db.password}

Example 1-3

After creating the properties file now in the below step we are creating the two profiles by using values of different properties as follows.

Code:

<profile>
<id>dev</id>
<activation>
…..
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
……
</properties>
</profile>
</profiles>

Example 1-4

Now in the below example, we are printing the output of a properties file as follows. We are creating maven_app class as follows.

Code:

public class maven_app {
-----
}

Example 1-5

Now below we are testing and running the project by using a maven build as follows. We can also use the maven command for the same.

Example 1-6

Key Takeaways

  • Maven profile is nothing but an element subset which was allowing us to customize the builds for the specified environment, this is portable in different types of environments.
  • Environment of the build is known as the specific environment which was set on dev and prod instances.

FAQ

Given below are the FAQs mentioned:

Q1. Why we are using maven profile in the application?

Answer: It is used to set the different types of environmental values in a single xml file. We can set dev and prod parameter values in a single file.

Q2. Which files we are using to activate the maven profile in java?

Answer: We are using pom.xml and settings.xml files to activate the maven profile in java. We need to create the settings.xml file in the user’s home directory.

Q3. What is maven build a profile in java application?

Answer: The build profile is nothing but multiple configuration values which was used to set default values.

Conclusion

While using the maven profile we can modify the profile at the time of build and it is also used to give the target environment. Maven profile is nothing but set of a configuration files which was used or set in the maven profile which were overridden by default values.

Recommended Articles

This is a guide to Maven Profile. Here we discuss the introduction and how to activate the maven profile by using the missing file along with an example. You may also have a look at the following articles to learn more –

  1. Maven POM File
  2. Maven Interview Questions
  3. Maven Commands
  4. JUnit 5 Maven Dependency
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Maven Training (4 Courses, 6 Projects) Learn More