Maven Introduction – Part 4: Maven Build LifeCycle

Maven build lifeCycle is the central concept of Maven. The sequence of phases which define the order of building and distributing artifact/project is defined with a build lifeCycle.

There are three standard lifeCycles: default, clean and site.
default handles project deployment.
clean handles project cleaning.
site handles the creation of project’s site documentation.

This tutorial will introduce these Maven build lifeCycles, relation between concepts of phases and plugin goals.

I. Maven LifeCycles
1. Clean LifeCycle

This is detail about the phases included in Clean LifeCycle:

Phases Descriptions
pre-clean execute processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean execute processes needed to finalize the project cleaning

The clean phase plays a central role. It usually deletes the target directory that Maven created during the build process.

2. Default LifeCycle

This LifeCycle contains many interesting build phases which deal with source generation, compilation, resource handling, tests, integration tests, deployment.
This is detail about the phases included in Default LifeCycle:

Phases Descriptions
validate validate the project is correct and all necessary information is available
initialize initialize build state, e.g. set properties or create directories
generate-sources generate any source code for inclusion in compilation
process-sources process the source code, for example to filter any values
generate-resources generate resources for inclusion in the package
process-resources copy and process the resources into the destination directory, ready for packaging
compile compile the source code of the project
process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes
generate-test-sources generate any test source code for inclusion in compilation
process-test-sources process the test source code, for example to filter any values
generate-test-resources create resources for testing
process-test-resources copy and process the resources into the test destination directory
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes (Maven 2.0.5 and above)
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment
integration-test process and deploy the package if necessary into an environment where integration tests can be run
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment
verify run any checks to verify the package is valid and meets quality criteria
install install the package into the local repository, for use as a dependency in other projects locally
deploy copy the final package to the remote repository for sharing with other developers and projects
3. Site LifeCycle

This is generally used to create detailed documentation for reports, deploy site… of any java project.

Phases Descriptions
pre-site execute processes needed prior to the actual project site generation
site generate the project’s site documentation
post-site execute processes needed to finalize the site generation, and to prepare for site deployment
site-deploy deploy the generated site documentation to the specified web server
II. Some remarkable things

When running a Maven command, we should notice that:
– When a phase is called, only phases upto and including that phase will execute. That means we need to call the last build phase we wanna to be executed.
For example, the order of phases in a lifeCycle is PhaseA -> PhaseB -> PhaseC -> PhaseD -> PhaseE
The command which call PhaseD will execute PhaseA -> PhaseB -> PhaseC and PhaseD, not execute PhaseE.

– Depending upon which module packaging type to be chosen, only some phases are potentially activated for goal execution.
These are the phases in the default life cycle for different packaging types:

jar/war/ejb/ejb3/rar maven-plugin pom
process-resources process-resources
compile compile
process-test-resources process-test-resources
test-compile test-compile
test test
package package package
install install install
deploy deploy deploy
III. Phases and Plugin Goals

With some parts above, we have known that each kind of lifeCycle contains some phases. Now we go deeply the concept of phases, and also plugin goals by relation between them.

Firstly, a plugin goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifeCycle by direct invocation.

Now look back to Effective POM from fresh Maven Project in the part 1 of this Maven Series:

So, we see that lifeCycle consists of phases (each phase as a step), can invoke a set of configured goals provided by certain plugins. Maven is also like a plugin executor, each plugin may have one or more goals.
We can decide which goal is attached to which phase in default lifeCycle.

When invoking a phase, Maven passes all phases and executes all goals bound to any phases upto and including the given phase. If there is a phase with no goal bound to it, nothing is done. The phase is just passed.

We can’t insert additional phases into any Maven built-in lifeCycles because they already exist. We can only develop our own lifeCycle with its own phases, but simply using Maven is better.

Now go to an example. We run command install (after clean) for the fresh Maven Project above:
– Right-click to the Project
– Run as -> Maven Build… -> install

Check the console window:

Look at this:
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-introduction ---
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-introduction ---
They are for:

This is how Maven show its work neatly via string in build log:
>> plugin-artifactId:plugin-version:goal (execution-id) @ project-name

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *