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

Read More

Maven Overview – Core Concepts

Maven Overview – Core Concepts
What is Maven?
Maven is a Build tool used primarily for Java Projects.

How Maven helps us?
Maven helps us with Builds, dependencies, reporting, documentation and Releases

What is POM?
Whenever we create a maven project, we will have POM.xml file. POM stands for Project Object Model. pom.xml contains information about project configurations, dependencies, plugins, goal, dependencies etc.

Maven Build Cycle:
The Project Object Model (POM) has build life cycle. The Build process in maven consists of Build Life cycles, Phases and Goals.
Build life cycle consists of Sequences of phases to be executed and each phase consists of sequence of goals to be executed

Maven Built-in Build Life Cycles:
Maven has 3 Built-in Build Life Cycles,

  • default
  • clean
  • site

Default: default life cycle performs compiling and packaging the product
clean: clean life cycle removes temporary files, .class files etc
site: generated documentation for the project

Phases of Build Life Cycle:
Now we know that, Build life cycle has sequence of phases to be executed. Maven has alot of sequence of phases. Here are some of the important Phases,

  • Validate: Validates the project and checks all the necessary informations are available
  • Compile: Compiles the source code
  • Test: Tests the source code using suitable unit testing framework
  • Package: Compiled code is packaged in a distributable format (ex: jar)
  • Install: Installs the package in local repository
  • Deploy: Copies the package to remote repository

Build Profiles:
Build profiles are used to build projects with different configurations. This helps us to avoid creating multiple POM.xml files with different configurations.
There are 3 types of Build Profiles,

  • Per user
  • Per project
  • Global

Maven Plugins:
There are two types of plugins in maven,

  • Build Plugin: Executed during build and should be declared within tag <build> tag
  • Reporting Plugin: Executed during Site generation and declared within tag <reporting> tag

Maven Repositories:

  • Local Repository
  • Central Repository
  • Remote Repository

Local Repository: 
It is a directory in our local system. When we run maven it will download the dependencies and will keep those dependencies in our system, So we can re-use the same downloaded dependencies for other projects too.

Central Repository:
Central Repository is provided by Maven Community. Maven looks into central repository when it cannot find the dependencies in the Local Repository

Remote Repository:
Remote Repository is the repository in the web server.

External Dependencies:
These dependencies are the one which are not available in the Local Repository or Central or Remote Repository But available in our Hard disk.

SNAPSHOT Dependencies:
These are the dependencies that are still under development. SNAPSHOT dependencies make sure that we have the latest version on every build.

Read More