Maven Build Life Cycle

In this article, we have explained about the build life cycle of maven, which is also sometimes referred as Maven Lifecycle. The article also describes about maven goals and phases.


The build life cycle in Maven refers to the set of processes that must be followed in order to complete the objectives and tasks of each Maven project.

The build life cycle is primarily the focus of Maven 2.0, which explicitly states that these phases must be well-defined in order to produce the intended results when the build life cycle has been successfully completed.

Maven has 3 built-in build life cycles, which are illustrated below:

  • Clean: In this phase, the project is cleaned in preparation for a new build and deployment.
  • Site: this phase generates the project’s java documentation.
  • Default: this phase handles the whole deployment of the project.

Build Life Cycle of clean phase

As was already said, the project is cleaned up during this step to prepare it for a new compile and deployment. Mvn post-clean is the command used to accomplish the same. When this command is used, maven internally executes the following commands to complete the tasks listed below:

  1. mvn pre-clean
  2. mvn clean
  3. mvn post-clean

Maven’s clean is a goal that, when executed, removes all the built files from the output directory (target folder).

There is one thing to note that Maven runs the phases up to and until the invoked phase whenever a command for any life cycle is issued. For instance, when’mvn clean’ is called, Maven just runs the clean phase. However, no compile, deployment, or site phase is started.

Build Life Cycle of Site (site)

Maven has a phase that performs other tasks in addition to cleaning, compiling the source code, and creating an application that can be deployed. One of the crucial features offered by Maven, which generates the thorough documentation for any Java project, is this step.

The following phases are exclusive to this project documentation:

  1. pre-site
  2. site
  3. post-site
  4. site-deploy

‘mvn site’ is the maven command used to produce javadocs for a specific project. Basically, maven calls the “Doxia” document generation plugin and other report generating plugins when this command is issued.

Build Lifecycle (Default)

The Maven build lifecycle’s default phases are listed below. The maven commands will be used to initiate these phases.

PhaseDescription
validateValidates and guarantees that the project is good and ideal in light of the availability of the necessary information for the construction.
generate-sourcesProducing source code for any application and including it in the compilation process.
process-sourcesthe source code is processed to see if any filters need to be applied.
generate-resourcesany source code creation that incorporates the package.
process-resourcesTransferring the resources to the desired location and preparing the package.
compileCompilation of the project source code.
process-classesEnhancing the bytecode for the class files produced by the compiler.
generate-test-sourcesResources in the test destination directory are copied and processed.

Maven Build Goals

A build phase consists of a list of objectives. Maven goals stand for a certain task that is involved in creating and overseeing a project. A maven goal may occasionally not be linked to a build phase. By using the command line, we may accomplish these objectives. The syntax for carrying out a goal is:$ mvn plugin-prefix:goal
$ mvn plugin-group-id:plugin-artifact-id[:plugin-version]:goal

Here is an example of how to use the command line to perform the dependency tree objective. There are no build phases that include it.$ mvn dependency:tree

Maven Goal and Maven Phases:

A goal is a function carried out by a plugin, like compiler:build and deploy:install-file. The goals are tied to the stages of the life cycle during which they are carried out. Goals and phases both have direct command-line execution options.

Below is a list of the various stages of the maven build life cycle which are generally used:

  • validate
  • clean
  • compile
  • test
  • package
  • integration-test
  • verify
  • install
  • deploy
  • site

1 thought on “Maven Build Life Cycle”

  1. Wow, marvelous weblog format!

    you made blogging look easy. The entire look of your web site is
    fantastic, as well as the content material!

    Reply

Leave a Comment