Maven Build Lifecycle is a fundamental concept in Maven that defines a sequence of phases and goals to be executed for managing the entire build process, from compilation to deployment.
There are three primary types of build lifecycles in Maven:
- Default Lifecycle: Handles the standard deployment process of the project, from compiling the code to packaging and installing it.
- Clean Lifecycle: Manages the cleaning process by removing artifacts from previous builds to ensure a fresh build environment.
- Site Lifecycle: Deals with generating and deploying the project's site documentation, such as project reports and other documentation.
Each lifecycle consists of several phases, and when you execute a phase, all preceding phases are also executed.

1. Default Lifecycle
The Default Lifecycle is the most commonly used lifecycle, focusing on the build process from validation to deployment. Phases in this lifecycle are as follows:
- Validate Phase: Checks the project configuration to ensure all necessary information is present and valid.
- Compile Phase: Compiles the source code of the project.
- Test Phase: Runs unit tests using a testing framework to ensure the code works as expected.
- Package Phase: Packages the compiled code into a JAR, WAR, or other artifact format.
- Verify Phase: Performs additional checks on the packaged code to ensure it is valid.
- Install Phase: Installs the packaged artifact into the local repository for use as a dependency in other projects.
- Deploy Phase: Deploys the packaged artifact to a remote repository for sharing with other developers or teams.
2. Clean Lifecycle
The Clean Lifecycle is designed for cleaning the project, ensuring that artifacts generated from previous builds are removed before starting the new build process. Phases in the Clean Lifecycle:
- pre-clean: Executes processes before cleaning.
- clean: Removes files generated by the previous build.
- post-clean: Executes processes after cleaning.
3. Site Lifecycle
The Site Lifecycle is responsible for generating and deploying project documentation. Phases in the Site Lifecycle:
- pre-site: Executes processes before generating site documentation.
- site: Generates the site documentation.
- post-site: Executes processes after generating site documentation.
- site-deploy: Deploys the generated site documentation to a web server.
Example Workflow Using Maven
Once the business logic is developed, navigate to your Maven project folder using the command prompt. Execute the following Maven commands to observe the expected output in the build lifecycle.
Example 1: Default Lifecycle
Here we created a sample Maven project using the Spring Tool Suite IDE with the required project dependencies. Below we provide those dependencies for your reference. Once the project is created successfully. After this, the build process is started.
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavenbuild</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavenbuild</name>
<description>Spring Reactive</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
<!-- Javadoc Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<source>17</source>
</configuration>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- This command checks if the project is correct and all necessary information is available.
mvn validate
- This command compiles the source code of the project.
mvn compile
Maven Build Success:

- This command runs the unit tests for the project.
mvn test
- This command packages the compiled code into a JAR or WAR file.
mvn package
- This command runs any checks to verify the quality of the package.
mvn verify
- This command installs the package into the local repository for use as a dependency in other projects locally.
mvn install
Build Successfully:

- This command deploys the package to a remote repository for sharing with other developers.
mvn deploy
Example 2: Clean Lifecycle
In this example, we explain the Clean Lifecycle phases. Follow the steps below:
- Executes processes needed before cleaning:
mvn pre-clean

- Removes files generated by the previous build:
mvn clean

- Executes processes needed after cleaning:
mvn post-clean

Example 3: Site Lifecycle
In this example, we explain the Site Lifecycle phases. Follow the steps below:
- Executes processes needed before generating the site documentation.
mvn pre-site

- Generates the project’s site documentation
mvn site

- Executes processes needed after generating the site documentation
mvn post-site

- Deploys the generated site documentation to a web server
mvn site-deploy

Examples:
Example 1: Default Lifecycle
Here we created a sample Maven project using the Spring Tool Suite IDE with the required project dependencies. Below we provide those dependencies for your reference. Once the project is created successfully, develop the required business logic. After this, the build process is started.
Project Folder Structure:

pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavenbuild</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavenbuild</name>
<description>Spring Reactive</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
<!-- Javadoc Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<source>17</source>
</configuration>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Once the business logic is developed, navigate to your Maven project folder using the command prompt. Execute the following Maven commands to observe the expected output in the build lifecycle.
1. Validate
- This command checks if the project is correct and all necessary information is available.
mvn validate
Output:

- This command compiles the source code of the project.
mvn compile
Output:

Maven Build Success:

- This command runs the unit tests for the project.
mvn test

- This command packages the compiled code into a JAR or WAR file.
mvn package

- This command runs any checks to verify the quality of the package.
mvn verify

- This command installs the package into the local repository for use as a dependency in other projects locally.
mvn install

Build Successfully:

- This command deploys the package to a remote repository for sharing with other developers.
mvn deploy

Example 2: Clean Lifecycle
In this example, we explain the Clean Lifecycle phases. Follow the steps below:
- Executes processes needed before cleaning:
mvn pre-clean

- Removes files generated by the previous build:
mvn clean

- Executes processes needed after cleaning:
mvn post-clean

Example 3: Site Lifecycle
In this example, we explain the Site Lifecycle phases. Follow the steps below:
- Executes processes needed before generating the site documentation.
mvn pre-site

- Generates the project’s site documentation
mvn site

- Executes processes needed after generating the site documentation
mvn post-site

- Deploys the generated site documentation to a web server
mvn site-deploy
