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:
|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:
|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.
|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
The command which call
PhaseD will execute
PhaseD, not execute
– 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:
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:
maven-clean-plugin 2.5 default-clean clean clean maven-resources-plugin 2.6 default-testResources process-test-resources testResources default-resources process-resources resources maven-compiler-plugin 3.1 default-compile compile compile default-testCompile test-compile testCompile
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:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Introduction 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-introduction --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-introduction --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-introduction --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-introduction --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-introduction --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-introduction --- [INFO] Building jar: ...MavenIntroduction\target\maven-introduction-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-introduction --- [INFO] Installing ...MavenIntroduction\target\maven-introduction-0.0.1-SNAPSHOT.jar to .m2\repository\com\javasampleapproach\mavenintro\maven-introduction\0.0.1-SNAPSHOT\maven-introduction-0.0.1-SNAPSHOT.jar [INFO] Installing ...MavenIntroduction\pom.xml to .m2\repository\com\javasampleapproach\mavenintro\maven-introduction\0.0.1-SNAPSHOT\maven-introduction-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
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:
maven-compiler-plugin 3.1 default-compile compile compile default-testCompile test-compile testCompile
This is how Maven show its work neatly via string in build log:
plugin-artifactId:plugin-version:goal (execution-id) @ project-name
Last updated on December 10, 2017.