Maven – Profiles – Profile based Dependencies:
One of the major advantage of maven is creating profile based dependencies. Instead of creating separate POM.xml, we can create a profile and configure dependencies for that specific profile. This helps us to declare all the dependencies in one pom.xml.

There are various ways of creating Profiles in Maven, declaring in POM.xml or declaring in settings.xml etc. Here we are going to see how to create multiple profiles in pom.xml
We can create multiple profiles by declaring id inside <profile> </profile> tag. This <profile> tag should be enclosed between <profiles></profiles> tag.

Creating profile,

<profiles>
        <profile>
         <id>User1</id>
         <dependencies>
              <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
            </dependency>
         </dependencies>   
    </profile>
</profiles>

In this above code, We have created a profile with name User1 and declared spring dependency for that specific user.

Now let us create 2 profiles and configure dependencies for both the profiles and let us see the output.

Example:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MavenTest</groupId>
    <artifactId>Profile</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <profiles>
        <profile>
         <id>User1</id>
         <dependencies>
              <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
            </dependency>
         </dependencies>
          
    </profile>
    
    <profile>
         <id>User2</id>
         <dependencies>
             <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
         </dependencies>
          
    </profile>
    </profiles>
    
</project>

In the above pom.xml, We have created 2 users, User1 and User2. User1 has dependency included for emails and User2 for Spring context. After the project is run with Build with Dependencies, When we select a specific profile, Dependencies for that specific profile only to be shown.

First let us see how to select the specific profile using Netbeans.

Right click the project and when we select Set Configuration, We will be able to see the profiles created for that specific project.

output1

 

We have created User1 and User2 profiles. Now when we select the User1, email dependency downloads to be shown and when we select User2 Spring-context dependency donwloads to be shown.

User1 output:
user1

User2 Output:
user2

By Sri

Leave a Reply

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