Maven Build Phases with Example

Maven Build Phases with Example:
As we have already discussed some of the most commonly and frequently used Maven build phases (Here). Now let us see an example of executing Maven Build Phases in a java project.

We are going to see how to execute,

  • 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

Project Structure:
structure

Sample.java

Now let us run the phases in command prompt, Open the command prompt

mvn clean validate
mvn-clean-validate

mvn clean compile:
mvn-clean-compile

mvn clean install:
mvn-clean-install

mvn clean package:
mvn-clean-package

If we have test cases, we can run mvn clean test and mvn clean deploy will install it in remote repository

 

Read More

Maven – Configuring Dependencies

Maven – Configuring Dependencies:
When we create a maven project, By default pom.xml will be generated.

pom.xml

Model Version:
Model Version is the version of POM model

Artifact Id:
Contains the name of the project which we create

Group Id:
GroupId is an unique id for the project.

Version:
Version is the version number of the project. A project might have multiple releases, So the version number indicates the version release of the project

Packaging:
Type of project packaging.

Now let us include a dependency inside the project, We can get the dependencies here – Maven Repository

Let us include Spring context dependency and check for the dependency download.
Let us include this dependency inside POM.xml

Before we include this dependency, The dependency should be enclosed between <dependencies> tag.

When we right click the project and select “build with dependencies”, the jars are downloaded with the help of these dependencies.

output

 

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

Maven Installation Step by Step

What is Maven?
Maven is a project Management tool which provides a complete build life cycle framework.
Maven helps us to manage Builds, dependencies, Documentation, Reporting etc.

In this article let us see how to Install Maven Step by Step.

STEP 1:
JAVA_HOME variable to be set in Windows Environment Variable. Right click on My Computer –> Properties –> Advanced System Settings –> Environment Variables
javahome

STEP 2:
Download Maven (here) and unzip the contents.

2

 

STEP 3:
We need to create 2 system variables, M2_HOME and MAVEN_HOME and set the path where we have unzipped the maven.

3

STEP 4:
Edit the path variable in environment variables and include %M2_HOME%\bin
4

STEP 5:
Verify the maven installation

5

Read More