Updated April 6, 2023
Introduction to Maven Phases
Maven is a building tool that can also be used for many other purposes such as dependency management, unit testing, project documentation and deployment. Here we will see about the different phases involved in the build lifecycles of maven. If a person is aware of a few commands that are necessary for building a project and executing the phases. He can build any maven project easily as POM makes sure that all other dependencies are acquired and desired results are the output. While executing any phase, it is necessary to keep in mind that the only specified phase is not run, all the phases prior to the specified phase and the mentioned one are executed.
Build Lifecycles and Phases
Maven provides us with three build lifecycles that are built-in. These build lifecycles are default, clean and site. Each of them has some purpose and usage and also contains many build phases by which it is defined. The default build lifecycle helps in handling and maintaining the deployment of the project. The second build lifecycle i.e clean build lifecycle helps in cleaning the project while the site build lifecycle is responsible for document creation at the project’s site. Each of these built-in build lifecycles is made up of different phases. The default lifecycle is made up of 23 phases, clean lifecycle of 3 phases and site lifecycle of 4 phases.
For example, the default build life cycle is made of some of the following phases that act as different stages in the lifecycle.
- Validate phase: This phase and stage makes sure that all the necessary things are present and the project is correct in every format required.
- Compile phase: In this phase, the source code of the project is compiled.
- Test phase: In this phase, the source code is being tested for some basic test cases that do not include packing or deployment in it with the help of any unit testing framework.
- Package phase: Takes the compiled code and package in its distributable format, such as a JAR.
- Verify phase : To make sure that the quality is maintained an integration testing on the current source code is being made in this phase.
- Install phase: There is often a necessity to add a dependency of a certain project in some other project. This phase makes sure that the project package is properly installed for making other projects available with adding the dependency of the current project being built.
- Deploy phase: In this phase, the build is completed and the current project is being copied to the remote repository. This makes the built project available for other projects to add as dependency or developers.
All the phases of each of the three build lifecycles are executed in the sequential format. In the default build lifecycle, the project is primarily validated for its correctness and made sure that all the requirements are met correctly after which the source code is compiled and then unit testing is done that is further followed with integration testing and installing the packaged project on local repository after which the project is made available to other projects and developers by deploying it on the remote repository.
We can select any phase that we wish to run according to our convenience and necessity and fire the command from the command line using the below format.
1. mvn package
The above command will create a jar or war file for you depends on the type of project and language you are using. If your application is a java project then java archives i.e jar will be generated and for web applications web archives i.e wars will be created. Depending on your purpose you can fire different commands and run different phases such as for performing unit testing you will fire mvn test command and so on. Whenever you execute a certain command then all the phases previous to it in the sequence of the phases will also execute.
For example, whenever you fire the mvn verify command then all the phases before it in the sequence that are validated, compile, test and package will also get executed. Hence, whenever you want to execute a certain phase just choose the last phase up to which you want to perform the build lifecycle and all the previous phases will also get executed.
Note that certain phases cannot be called from the command-line.
2. mvn clean deploy
Whenever you want to clean the project and deploy your project or artifact into the repository where it can be shared further then you can use this command. This command can also be used when your project is made up of sub projects and contains multiple projects in it. In this case, each sub project is cleaned and deployed i.e clean and deploy build cycles are executed for each of them. This is called a multi-module structure that is supported by maven.
- The build lifecycle or task is further broken down into granular level by breaking each of the phases of lifecycle into multiple plugin goals which are the small task responsible for managing and building the artifact/project.
- A single plugin can be bound to none, one or many build phases.
- Whenever it does not belongs to any of the phases then it can be executed individually outside the lifecycle build process by invoking it directly.
- From Maven 2.0.5 onwards, the POM decides the order in which the multiple plugins belonging to the same phase are executed.
Conclusion – Maven Phases
Maven has three built-in build lifecycles named default, clean and deploy. Each of the build lifecycles is made up of different phases that are even referred to as stages that are executed in the sequential order and perform some specific task. Each phase is further made up of one or more goals with which it is bound that help in managing and building the project.
This is a guide to Maven Phases. Here we discuss the introduction to Maven Phases with build lifecycles and phases, maven commands and plugin goals. You may also have a look at the following articles to learn more –