- When we build a Maven project, it executes a set of clearly defined tasks based on the project pom.xml configuration and the command-line options.
- This standard set of tasks creates the maven build lifecycle.
- The benefit of a clearly defined lifestyle is that we have to remember only a few sets of commands to compile, build, install, and deploy our projects.
- Maven build lifecycle goes through a set of stages, they are called build phases. For example, the default lifecycle is made up of the following phases
Mostly Used Default or Buid Phases/stages :
Sl. | Stage/Command | Information |
1 | validate | This step validates if the project structure is correct. For example – It checks if all the dependencies have been downloaded and are available in the local repository. |
2 | compile | It compiles the source code, converts the .java files to .class, and stores the classes in the target/classes folder. |
3 | test-compile | Compile the source code in the test destination directory |
4 | test | It runs unit tests for the project. |
5 | package | This step packages the compiled code in a distributable format like JAR or WAR. |
6 | integration-test | It runs the integration tests for the project. |
7 | verify | This step runs checks to verify that the project is valid and meets the quality standards. |
8 | install | This step installs the packaged code to the local Maven repository. |
9 | deploy | It copies the packaged code to the remote repository for sharing it with other developers. |
Total 23 Default Phases of maven by maven.apache.org
S.N | Phase | Description |
1 | validate | validate the project is correct and all necessary information is available. |
2 | initialize | initialize build state, e.g. set properties or create directories. |
3 | generate-sources | generate any source code for inclusion in compilation. |
4 | process-sources | process the source code, for example to filter any values. |
5 | generate-resources | generate resources for inclusion in the package. |
6 | process-resources | copy and process the resources into the destination directory, ready for packaging. |
7 | compile | compile the source code of the project. |
8 | process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
9 | generate-test-sources | generate any test source code for inclusion in compilation. |
10 | process-test-sources | process the test source code, for example to filter any values. |
11 | generate-test-resources | create resources for testing. |
12 | process-test-resources | copy and process the resources into the test destination directory. |
13 | test-compile | compile the test source code into the test destination directory |
14 | process-test-classes | post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. |
15 | test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
16 | 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. |
17 | package | take the compiled code and package it in its distributable format, such as a JAR. |
18 | pre-integration-test | perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
19 | integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
20 | post-integration-test | perform actions required after integration tests have been executed. This may include cleaning up the environment. |
21 | verify | run any checks to verify the package is valid and meets quality criteria. |
22 | install | install the package into the local repository, for use as a dependency in other projects locally. |
23 | deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
Phase | Goal |
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar |
install | install:install |
deploy | deploy:deploy |
One can invoke either a maven phase or a goal while building a project.
To invoke a phase — mvn <phase-name>
Sample pom.xml for default build life cycle :
<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> |
C:\Users\gajan\artifactId-MavenApp > mvn validate |
C:\Users\gajan\artifactId-MavenApp > mvn compile |
C:\Users\gajan\artifactId-MavenApp > mvn test |
C:\Users\gajan\artifactId-MavenApp > mvn package |
C:\Users\gajan\artifactId-MavenApp > mvn deploy |
Output response : C:\Users\gajan\artifactId-MavenApp>mvn compile [INFO] Scanning for projects… [INFO] [INFO] —————-< com.companyname.projectgroup:project >—————- [INFO] Building project 1.0 [INFO] ——————————–[ jar ]——————————— [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.validate) @ project — [INFO] Executing tasks [echo] validate phase [INFO] Executed tasks [INFO] [INFO] — maven-resources-plugin:2.6:resources (default-resources) @ project — [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\Users\gajan\artifactId-MavenApp\src\main\resources [INFO] [INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ project — [INFO] No sources to compile [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.compile) @ project — [INFO] Executing tasks [echo] compile phase [INFO] Executed tasks [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 3.033 s [INFO] Finished at: 2021-12-10T20:05:46+05:30 [INFO] ———————————————————————— C:\Users\gajan\artifactId-MavenApp > |