Apache Maven – Lifecycle and Basic Operations

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

1. 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

There are three built-in build lifecycles:

 Build Lifecycle Responsibilities
defaulthandles project deployment
cleanhandles project cleaning
sitehandles creation of project’s site documentation

1.1 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 PhaseResponsibilities
validatevalidates, if the project is correct and all necessary are available for next activity
compilecompiles the source code of the project
testtests the compiled source code
packagepackages the compiled source code, depending on the pom attribute <packaging> (default is JAR)
installinstalls the package into local maven repository (this could be used as dependency for other projects in our local environment)
deployDeploys/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

1.2 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 listed below
  • For example, “mvn clean
 Build PhaseResponsibilities
pre-cleanexecutes process required, prior to cleaning
cleanremoves older version of project build
post-cleanexecutes processes needed to finalize the project cleaning

1.3 site lifecycle

  • The site lifecycle used to create project’s site documentation
 Build Phase Responsibilities
pre-siteexecutes process required, prior to site generation
sitegenerates project’s site documentations
post-siteexecutes processes needed to finalize the site generation
site-deploydeploys to specified web server (site doc generated above)

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - Basic Operations
Apache Maven - Dependency Scopes