Home What is the Maven Build Lifecycle?
Post
Cancel

What is the Maven Build Lifecycle?

What is the Maven Build Lifecycle?

The Maven build lifecycle is a sequence of phases that define the order in which Maven executes the build. Each phase represents a step in the build process, and each phase may contain one or more goals that are executed in a specific order. The Maven build lifecycle consists of three main lifecycles: the default lifecycle, the clean lifecycle, and the site lifecycle.

The Default Lifecycle

The default lifecycle is the most important and commonly used lifecycle in Maven. It includes three main phases: validate, compile, and package. Each of these phases is executed in order, and each phase may contain one or more goals. The validate phase checks that the project is valid and correct. The compile phase compiles the source code, and the package phase creates the package, such as a JAR or WAR file.

It includes the following phases:

  • validate: Validate the project is correct and all necessary information is available.
  • compile: Compile the source code of the project.
  • test: Test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  • package: Take the compiled code and package it in its distributable format, such as a JAR.
  • verify: Run any checks on results of integration tests to ensure quality criteria are met.
  • 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.

The Clean Lifecycle

The clean lifecycle is used to clean the project and remove any generated files. It includes a single phase, the clean phase, which deletes the target directory and all its contents.

It includes the following phases:

  • 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 Site Lifecycle

The site lifecycle is used to generate the project’s documentation and reports. It includes a single phase, the site phase, which generates the project’s documentation and reports and places them in the target/site directory.

It includes the following phases:

  • 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.

Configuring Maven Build Lifecycle

Maven’s build lifecycle can be configured by creating a pom.xml file. This file contains the project’s metadata, dependencies, and plugins. It also defines the build lifecycle phases and the goals to be executed during each phase.

Here’s an example of how to configure Maven’s build lifecycle in a pom.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0-SNAPSHOT</version>

  <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>
    </plugins>
  </build>
</project>

In this example, we’ve configured the maven-compiler-plugin to use Java 8 as the source and target version. The plugin will be executed during the compile phase of the default build lifecycle.

Conclusion

Maven build lifecycle is an important concept to understand when working with Maven. By understanding how the build lifecycle works, you can more easily troubleshoot issues with your build process, and configure your build to suit your project’s needs. In this blog post, we’ve covered the three main phases of the build lifecycle, and how to configure it in a pom.xml file.

This post is licensed under CC BY 4.0 by the author.