Updated February 20, 2023
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.
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.
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
- After creating the project template, now we are opening the project into the spring tool suite as follows.
- 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>
- 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>
- To run the profile which was added in the pom.xml file we need to run the project as follows.
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>
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>
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>
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>
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.
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>
After creating the code of activation now in the below example we are running the maven project as follows.
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>
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}
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>
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 {
-----
}
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.
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 –