Maven Build Lifecycle

Last Updated : 10 Jan, 2026

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.

file

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:

  1. Validate Phase: Checks the project configuration to ensure all necessary information is present and valid.
  2. Compile Phase: Compiles the source code of the project.
  3. Test Phase: Runs unit tests using a testing framework to ensure the code works as expected.
  4. Package Phase: Packages the compiled code into a JAR, WAR, or other artifact format.
  5. Verify Phase: Performs additional checks on the packaged code to ensure it is valid.
  6. Install Phase: Installs the packaged artifact into the local repository for use as a dependency in other projects.
  7. 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:

  1. pre-clean: Executes processes before cleaning.
  2. clean: Removes files generated by the previous build.
  3. 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:

  1. pre-site: Executes processes before generating site documentation.
  2. site: Generates the site documentation.
  3. post-site: Executes processes after generating site documentation.
  4. 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
<?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
mvn validate
  • This command compiles the source code of the project.
mvn compile
mvn compile

Maven Build Success:

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

Build Successfully:

Build Success
  • This command deploys the package to a remote repository for sharing with other developers.
mvn deploy
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
mvn pre-clean
  • Removes files generated by the previous build:
mvn clean
mvn clean
  • Executes processes needed after cleaning:
mvn post-clean
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
mvn pre-site
  • Generates the project’s site documentation
mvn site
mvn site
  • Executes processes needed after generating the site documentation
mvn post-site
mvn post-site
  • Deploys the generated site documentation to a web server
mvn site-deploy
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:

Project Folder Structure

pom.xml:

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:

mvn validate
  • This command compiles the source code of the project.
mvn compile

Output:

mvn compile

Maven Build Success:

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

Build Successfully:

Build Success
  • This command deploys the package to a remote repository for sharing with other developers.
mvn deploy
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
mvn pre-clean
  • Removes files generated by the previous build:
mvn clean
mvn clean
  • Executes processes needed after cleaning:
mvn post-clean
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
mvn pre-site
  • Generates the project’s site documentation
mvn site
mvn site
  • Executes processes needed after generating the site documentation
mvn post-site
mvn post-site
  • Deploys the generated site documentation to a web server
mvn site-deploy
mvn site-deploy


Comment
Article Tags:

Explore