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:

PhasesDescriptions
pre-cleanexecute processes needed prior to the actual project cleaning
cleanremove all files generated by the previous build
post-cleanexecute 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:

PhasesDescriptions
validatevalidate the project is correct and all necessary information is available
initializeinitialize build state, e.g. set properties or create directories
generate-sourcesgenerate any source code for inclusion in compilation
process-sourcesprocess the source code, for example to filter any values
generate-resourcesgenerate resources for inclusion in the package
process-resourcescopy and process the resources into the destination directory, ready for packaging
compilecompile the source code of the project
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes
generate-test-sourcesgenerate any test source code for inclusion in compilation
process-test-sourcesprocess the test source code, for example to filter any values
generate-test-resourcescreate resources for testing
process-test-resourcescopy and process the resources into the test destination directory
test-compilecompile the test source code into the test destination directory
process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes (Maven 2.0.5 and above)
testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed
prepare-packageperform 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)
packagetake the compiled code and package it in its distributable format, such as a JAR
pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment
integration-testprocess and deploy the package if necessary into an environment where integration tests can be run
post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment
verifyrun any checks to verify the package is valid and meets quality criteria
installinstall the package into the local repository, for use as a dependency in other projects locally
deploycopy 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.

PhasesDescriptions
pre-siteexecute processes needed prior to the actual project site generation
sitegenerate the project’s site documentation
post-siteexecute processes needed to finalize the site generation, and to prepare for site deployment
site-deploydeploy 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/rarmaven-pluginpom
generate-resources
process-resourcesprocess-resources
compilecompile
process-test-resourcesprocess-test-resources
test-compiletest-compile
testtest
packagepackagepackage
installinstallinstall
deploydeploydeploy
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

By grokonez | November 19, 2016.

Last updated on December 10, 2017.



Related Posts


Got Something To Say:

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

*