The Maven Build Lifecycle
Introduction
Maven’s build lifecycle is a set of phases that define the process of building and managing a project.
Each phase represents a step in the build process, from validating the project’s structure to installing
the final artifact. This article explains the phases of the Maven build lifecycle, how Maven executes commands
like mvn clean install
, and how to integrate plugins to extend build capabilities.
Phases of the Build Lifecycle
Maven organizes its build process into three main lifecycles:
- Clean - Cleans up files from previous builds.
- Default - Handles the main build tasks, including compilation, testing, packaging, and deployment.
- Site - Generates project documentation and reports.
Each lifecycle comprises a sequence of phases that determine the order of operations. Let’s have a look at them.
1. Clean Lifecycle Phases
The Clean Lifecycle is designed to handle the cleanup of the project environment before a new build begins. It ensures that any files or artifacts from previous builds are removed, providing a clean slate for the next build process. This helps avoid potential issues caused by leftover files or outdated artifacts.
Key Phases Include:
-
pre-clean
This initial phase prepares the project environment for the cleaning process. It can involve tasks such as verifying conditions or making minor adjustments that are necessary before the actual cleanup begins. For example, you might use this phase to set up configurations or perform checks to ensure that the cleaning will proceed smoothly. To execute this phase, use the command:mvn pre-clean
. -
clean
The core of the Clean Lifecycle, this phase is responsible for removing files and directories from previous builds. The clean phase typically deletes the target directory, which houses compiled classes, packaged artifacts, and other build outputs. By clearing these old files, it ensures that the build starts from a completely clean slate, free from remnants that might interfere with the new build. To run this phase, use the command:mvn clean
. -
post-clean
After the cleaning process is complete, the post-clean phase takes over to handle any additional tasks required. This phase might include notifying systems or tools that the cleanup is finished or performing final adjustments. Essentially, it wraps up the cleanup process, making sure that the environment is fully prepared for the next steps in the build lifecycle. To execute this phase, use the command:mvn post-clean
.
2. Default Lifecycle Phases
The Default Lifecycle Phases cover a comprehensive set of steps for building and managing a project. We will focus on the most essential and frequently used phases, including validate, compile, test, package, verify, install, and deploy.
-
validate
The first phase in Maven’s lifecycle is the validation of the project. During this phase, Maven ensures that the project’s structure is correct and all the necessary configuration is in place. It checks thepom.xml
file to verify that everything is properly set up before moving forward.
To execute this phase, use the command:mvn validate
-
compile
Once the project is validated, the next step is to compile the source code. Maven takes the Java files located in thesrc/main/java
directory and compiles them into bytecode. This bytecode is then placed in thetarget/classes
directory, preparing it for further testing and packaging.
To execute this phase, use the command:mvn compile
-
test
After compiling the code, Maven proceeds to run unit tests. These tests, located in thesrc/test/java
directory, are executed against the compiled code to ensure that everything functions as expected. The results of the tests are stored in thetarget/test-classes
directory. This phase helps confirm the code’s reliability.
To execute this phase, use the command:mvn test
-
package
Once the code has been validated, compiled, and tested, Maven packages it into a distributable format, such as a JAR or WAR file. The package is stored in thetarget
directory and is now ready for distribution or further deployment. This phase is essential for creating an executable version of the project.
To execute this phase, use the command:mvn package
-
verify
At this stage, Maven checks the integrity and correctness of the packaged code. It may run additional verification tasks defined by plugins to ensure the package is complete and functions properly. This step adds another layer of assurance before installation or deployment.
To execute this phase, use the command:mvn verify
-
install
The install phase involves placing the packaged artifact into the local Maven repository. This allows the project’s package to be reused as a dependency in other local projects. The package is stored in the~/.m2/repository
directory, making it available for further use.
To execute this phase, use the command:mvn install
-
deploy
Finally, Maven deploys the packaged artifact to a remote repository, making it accessible to other developers or teams. This phase typically occurs in a collaborative environment, where the project’s package needs to be shared for further integration into larger systems.
To execute this phase, use the command:mvn deploy
3. Site Lifecycle Phases
The Site Lifecycle Phases handle the creation and management of project documentation, ensuring that all necessary reports and documentation are generated and deployed effectively.
-
pre-site
This phase involves preparations needed before the actual site generation begins. Tasks in this phase may include configuring settings or verifying prerequisites required for generating accurate and complete documentation. The pre-site phase sets the stage for the documentation process, ensuring that everything is in place before generating the site.
To execute this phase, use the command:mvn pre-site
. -
site
The core of the Site Lifecycle, this phase generates the project documentation and reports. During the site phase, Maven creates various reports and documentation based on the project’s source code, dependencies, and other configurations. This might include generating a project website, Javadoc, and other relevant reports that provide insights into the project’s state and quality.
To run this phase, use the command:mvn site
. -
post-site
After generating the site, the post-site phase handles any follow-up actions required. This might involve finalizing the generated documentation, validating its completeness, or preparing it for deployment. The post-site phase ensures that the generated reports and documentation are correctly processed and ready for the next steps.
To execute this phase, use the command:mvn post-site
. -
site-deploy
The final phase in the Site Lifecycle, site-deploy, focuses on deploying the generated site to a web server or repository. This phase uploads the created documentation and reports to a remote location where it can be accessed by other team members or stakeholders. This step is crucial for making the documentation available for review and use by others.
To deploy the site, use the command:mvn site-deploy
.
How Maven Executes Multiple Phases
Maven commands often involve executing a sequence of phases to complete the build process.
For example, when you run the command mvn clean install
, Maven will first execute the clean
phase,
which includes operations like pre-clean
and clean
. Afterward, it will proceed with the install
phase,
along with all the phases that come before it in the default lifecycle,
such as validate
, compile
, test
, package
, and verify
.
Relationship Between Phases, Goals, Plugins
Maven’s build process is structured around phases, each associated with specific goals provided by plugins. To illustrate this relationship, see the image below:
Phases and Goals
Maven’s build lifecycle consists of several phases, such as compile
, test
, package
, and install
.
Within each phase, Maven executes specific tasks known as goals. For example, during the compile
phase,
Maven might run the compile
goal, which compiles the source code. These goals are provided by Maven plugins.
Plugins
Maven plugins are responsible for executing the goals associated with each phase.
Each plugin contains one or more goals that perform specific tasks, such as compiling code or packaging artifacts.
For instance, the Maven Compiler Plugin handles compilation with goals like compile
and testCompile
,
while the Maven JAR Plugin manages packaging with goals like jar
.
How They Work Together
When Maven executes a phase, it triggers the goals defined for that phase. For example, in the compile
phase,
Maven runs the compile
goal from the Maven Compiler Plugin to process the source code.
Plugins integrate with Maven’s phases by associating their goals with specific phases.
This means that when a phase is executed, Maven knows which goals to run based on the plugins configured
in your pom.xml
.
Executing Plugin Goals
Plugins provide specific goals that can be executed directly from the command line. Goals perform individual tasks such as compiling code or running tests.
For example, to compile code, you would use the command mvn compiler:compile
.
To run tests, you would use mvn surefire:test
. These goals can be executed independently
or as part of the overall build lifecycle.
Integrating Plugins to Extend Build Capabilities
Maven plugins enhance the build process by adding functionality beyond the standard phases. Plugins can be used for tasks such as code analysis, packaging, deployment, and more.
Adding Plugins to pom.xml
Plugins are defined in the <build>
section of the pom.xml
file.
You can specify the plugin’s group ID, artifact ID, and version.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
In this example, the maven-compiler-plugin
is used to set the Java source and target versions,
while the maven-surefire-plugin
is used for running unit tests.
Conclusion
Understanding the Maven build lifecycle is essential for managing and automating the build process effectively.
Each phase represents a step in the process, from validating the project to deploying the final artifact.
By combining commands like mvn clean install
, you can perform comprehensive build operations.
Integrating and configuring plugins allows you to extend Maven’s capabilities and tailor the build process
to meet specific project needs. Mastering these aspects will help you streamline your development workflow
and ensure consistent, reliable builds.