Maven – Profiles – Profile based Dependencies

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,

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:

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

Leave a Reply

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