Apache Maven: Lifecycle and Basic Operations

In this article, we will learn and understand about Maven’s build lifecycle

 

There are three built-in build lifecycles:

 Build Lifecycle  Responsibilities
default handles project deployment
clean handles project cleaning
site handles creation of project’s site documentation

 

Build lifecycle phases

Every build lifecycle is made up of phases and each build phases defines/represents different stage in the lifecycle

Also with every build phase, a goal is associated with it

We will list down various stages/phases of the build lifecycle

 

default lifecycle

There are around 23 build phases for this default lifecycle, but we will list down only key build phases which we will encounter frequently in our day-to-day build activities

 Build Phase Responsibilities
validate validates, if the project is correct and all necessary are available for next activity
compile compiles the source code of the project
test tests the compiled source code
package packages the compiled source code, depending on the pom attribute <packaging> (default is JAR)
install installs the package into local maven repository (this could be used as dependency for other projects in our local environment)
deploy Deploys/copies the package into remote repository (sharing with other users/developers)

Execution of any of the build phase will result in execution of above build phases too (similar to implicit invocation)

For example, running maven command “mvn install” will execute or run all build phases like validate, compile, test, package and including install

Note: For complete lists of default build lifecycle refer here

 

clean lifecycle

In Maven when you run to build the project for deployment, then there is quite possibility that older version of project build exists in the “target” folder

To overcome this situation, we have to clean the project before making build for project deployment

Various build phases of the clean lifecycle

 Build Phase Responsibilities
pre-clean executes process required, prior to cleaning
clean removes older version of project build
post-clean executes processes needed to finalize the project cleaning

For example, “mvn clean

 

site lifecycle

The site lifecycle used to create project’s site documentation

 Build Phase  Responsibilities
pre-site executes process required, prior to site generation
site generates project’s site documentations
post-site executes processes needed to finalize the site generation
site-deploy deploys to specified web server (site doc generated above)

References

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Apache Maven: Basic Operations
Apache Maven: Dependency Scopes