Maven build lifecycle
The Maven build lifecycle defines the process of building and releasing a project.
A typical Maven build life cycle consists of a sequence of the following stages:
stage | handle | describe |
---|---|---|
validate | Verification items | Verify that the project is correct and that all necessary information is available |
compile | Execute compilation | Source code compilation is completed at this stage |
Test test | test | Run unit tests using the appropriate JUnit framework, for example. |
package | pack | Create a JAR/WAR package, as in POM The packages mentioned are defined in XML |
Check verify | inspect | Check the results of integration test to ensure that the quality meets the standard |
Install install | install | Install packaged items to the local warehouse for use by other projects |
deploy | deploy | Copy the final project package to the remote warehouse for sharing with other developers and projects |
In order to complete the default lifecycle, these phases (including other lifecycle phases not listed above) will be executed sequentially.
Maven has three standard life cycles:
- clean: processing of project cleaning
- Default (or build): processing of project deployment
- Site: processing of project site document creation
The build phase consists of plug-in goals
A plug-in goal represents a specific task (more elaborate than the construction phase), which helps in the construction and management of the project. These targets may be bound to multiple phases or unbound. Targets that are not bound to any build phase can be executed through direct calls outside the build lifecycle. The order in which these goals are executed depends on the order in which they are invoked and the construction phase.
For example, consider the following command:
clean and pakage are the construction phase, and dependency: copy dependencies is the goal
mvn clean dependency:copy-dependencies package
Here, the clean phase will be executed first, then the dependency: copy dependencies target will be executed, and finally the package phase will be executed.
Clean life cycle
When we execute the MVN post clean command, Maven calls the clean life cycle, which includes the following stages:
- Pre clean: perform some work that needs to be completed before clean
- clean: remove all files generated by the last build
- Post clean: perform some work that needs to be completed immediately after clean
The clean in mvn clean is the above clean. When a phase is run in a life cycle, all previous phases will be run. That is, if mvn clean is executed, the following two life cycle phases will be run:
pre-clean, clean
If we run MVN post clean, we run the following three lifecycle phases:
pre-clean, clean, post-clean
We can modify the operation behavior of this part by defining goals at any stage of the clean life cycle above.
In the following example, we add the maven antrun plugin: run target to the pre clean, clean, and post clean phases. In this way, we can display text information at all stages of the clean life cycle.
We have created a POM in the C:\MVN\project directory XML file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-clean</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-clean</id> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Now open the command console and jump to POM XML directory, and execute the following mvn command.
C:\MVN\project>mvn post-clean
Maven will begin processing and displaying all phases of the clean lifecycle.
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0 [INFO] task-segment: [post-clean] [INFO] ------------------------------------------------------------------ [INFO] [antrun:run {execution: id.pre-clean}] [INFO] Executing tasks [echo] pre-clean phase [INFO] Executed tasks [INFO] [clean:clean {execution: default-clean}] [INFO] [antrun:run {execution: id.clean}] [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO] [antrun:run {execution: id.post-clean}] [INFO] Executing tasks [echo] post-clean phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: < 1 second [INFO] Finished at: Sat Jul 07 13:38:59 IST 2012 [INFO] Final Memory: 4M/44M [INFO] ------------------------------------------------------------------
You can try to modify the mvn clean command to display pre clean and clean, and do nothing in the post clean phase.
Default (Build) lifecycle
This is the main life cycle of Maven, which is used to build applications, including the following 23 stages:
Life cycle phase | describe |
---|---|
validate | Verify that the project is correct and that all necessary information can complete the construction process of the project. |
initialize | Initialize the build state, such as setting property values. |
Generate sources | Generate any source code included in the compilation phase. |
Process sources | Processing source code, for example, filtering arbitrary values. |
Generate resources | Generate resource files that will be included in the project package. |
Process resources | Copy and process resources to the target directory, which is best prepared for the packaging phase. |
compile | Compile the source code of the project. |
Process classes | Handle the files generated by compilation, such as bytecode improvement and optimization of Java class files. |
Generate test sources | Generate any test source code included in the compilation phase. |
Process test sources | Process the test source code, for example, filter arbitrary values. |
Generate test resources | Create a resource file for the test. |
Process test resources | Copy and process test resources to the target directory. |
Test compile | Compile the test source code to the test target directory |
Process test classes | Process the files generated by compiling the test source code. |
test | Run tests using the appropriate unit test framework (Juint is one of them). |
Prepare package | Before the actual packaging, perform any necessary operations to prepare for the packaging. |
package | Package the compiled code into files in distributable format, such as JAR, WAR or EAR files. |
Pre integration test | Take necessary actions before performing integration test. For example, build the required environment. |
Integration test | Process and deploy projects into an integration test environment that can be run. |
Post integration test | Take necessary actions after the integration test is completed. For example, clean up the integration test environment. |
verify | Run arbitrary checks to verify that the project package is effective and meets quality standards. |
install | Install the project package to the local warehouse so that the project package can be used as a dependency for other local projects. |
deploy | Copy the final project package to the remote warehouse and share it with other developers and projects. |
There are some important concepts related to Maven life cycle that need to be explained:
When a phase is called by Maven command, such as mvn compile, only the phases before and including the phase will be executed. Different Maven targets will be bound to different Maven life cycle stages according to the type of package (JAR / WAR / EAR). In the following example, we add the maven antrun plugin: run target to a part of the Build lifecycle. In this way, we can display the text information of the life cycle. We have updated pom.com in C:\MVN\project directory XML file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.validate</id> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>validate phase</echo> </tasks> </configuration> </execution> <execution> <id>id.compile</id> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>compile phase</echo> </tasks> </configuration> </execution> <execution> <id>id.test</id> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>test phase</echo> </tasks> </configuration> </execution> <execution> <id>id.package</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>package phase</echo> </tasks> </configuration> </execution> <execution> <id>id.deploy</id> <phase>deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Now open the command console and jump to POM XML directory and execute the following mvn command.
C:\MVN\project>mvn compile
Maven will begin processing and displaying the phases of the build lifecycle up to the compilation phase.
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0 [INFO] task-segment: [compile] [INFO] ------------------------------------------------------------------ [INFO] [antrun:run {execution: id.validate}] [INFO] Executing tasks [echo] validate phase [INFO] Executed tasks [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\project\src\main\resources [INFO] [compiler:compile {execution: default-compile}] [INFO] Nothing to compile - all classes are up to date [INFO] [antrun:run {execution: id.compile}] [INFO] Executing tasks [echo] compile phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: 2 seconds [INFO] Finished at: Sat Jul 07 20:18:25 IST 2012 [INFO] Final Memory: 7M/64M [INFO] ------------------------------------------------------------------
Command line call
In the development environment, use the following commands to build and install the project to the local warehouse
mvn install
This line of command can also be used in the case of multiple modules, that is, projects with multiple sub projects. Maven will execute the clean command in each sub project, and then execute the deploy command.
Site lifecycle
- Pre site: perform some work that needs to be done before generating site documents
- Site: generate the site document of the project
- Post site: perform some work that needs to be done after generating the site document and prepare for deployment
- Site deploy: deploy the generated site documents to a specific server
The Site phase and Site deploy phase are often used here to generate and publish Maven sites. This is a very powerful function of Maven. Manager s prefer it. Documents and statistics are automatically generated, which is very good-looking. In the following example, we add the maven antrun plugin: run target to all phases of the Site lifecycle. In this way, we can display all text information of the life cycle.
We have updated pom.com in C:\MVN\project directory XML file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-site</id> <phase>pre-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.site</id> <phase>site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-site</id> <phase>post-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.site-deploy</id> <phase>site-deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>site-deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Now open the command console and jump to POM XML directory and execute the following mvn command.
C:\MVN\project>mvn site
Maven will start processing and displaying the various stages of the site lifecycle up to the site stage.
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0 [INFO] task-segment: [site] [INFO] ------------------------------------------------------------------ [INFO] [antrun:run {execution: id.pre-site}] [INFO] Executing tasks [echo] pre-site phase [INFO] Executed tasks [INFO] [site:site {execution: default-site}] [INFO] Generating "About" report. [INFO] Generating "Issue Tracking" report. [INFO] Generating "Project Team" report. [INFO] Generating "Dependencies" report. [INFO] Generating "Project Plugins" report. [INFO] Generating "Continuous Integration" report. [INFO] Generating "Source Repository" report. [INFO] Generating "Project License" report. [INFO] Generating "Mailing Lists" report. [INFO] Generating "Plugin Management" report. [INFO] Generating "Project Summary" report. [INFO] [antrun:run {execution: id.site}] [INFO] Executing tasks [echo] site phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: 3 seconds [INFO] Finished at: Sat Jul 07 15:25:10 IST 2012 [INFO] Final Memory: 24M/149M [INFO] ------------------------------------------------------------------