Horje
Maven Build Lifecycle

The Maven build lifecycle is a fundamental concept that defines the sequence of phases and goals executed to build and manage a Maven project. Understanding the build lifecycle is crucial for efficiently managing the build process, from compilation to deployment. The Maven Build life cycle includes a lot of build phases to complete the build process in the Maven.

Types Of Maven Build Lifecycle

  • default: This is the main lifecycle, as it’s responsible for project deployment.
  • clean: Handles project cleaning, ensuring that all artifacts generated by previous builds are removed.
  • site: Manages the creation of the project’s site documentation.

Each lifecycle consists of a sequence of phases. Each phase represents a stage in the lifecycle, and executing a phase will trigger the execution of all preceding phases. Below we provide information about each phase in the maven build build life cycle.

Maven Build Life Cycle


Maven Build Workflow

Below is a typical Maven build workflow for the default lifecycle.

  • Validate Phase: Check the project configuration and validate if all information required is present.
  • Compile Phase: Compile the source code of the project.
  • Test Phase: Execute unit tests using a testing framework.
  • Package Phase: Bundle the compiled code into a JAR/WAR file.
  • Verify Phase: Perform checks on the packaged code.
  • Install Phase: Install the package to the local repository for use as a dependency in other projects locally.
  • Deploy Phase: Deploy the package to a remote repository for sharing with other developers.

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="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://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. This command checks if the project is correct and all necessary information is available.

mvn validate

Output:

mvn validate


2. This command compiles the source code of the project.

mvn compile

Output:

mvn compile


Maven Build Success:

Build Success


3. This command runs the unit tests for the project.

mvn test

Output:

mvn test


4. This command packages the compiled code into a JAR or WAR file.

mvn package

Output:

mvn package


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

mvn verify

Output:

mvn verify


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

mvn install

Output:

mvn install


Build Successfully:

Build Success


7. This command deploys the package to a remote repository for sharing with other developers.

mvn deploy

Output:

mvn deploy


Example 2: Clean Lifecycle

In this example, we explain the Clean Lifecycle phases. Follow the steps below:

1. Executes processes needed before cleaning:

mvn pre-clean

Output:

mvn pre-clean


2. Removes files generated by the previous build:

mvn clean

Output:

mvn clean


3. Executes processes needed after cleaning:

mvn post-clean

Output:

mvn post-clean


Example 3: Site Lifecycle

In this example, we explain the Site Lifecycle phases. Follow the steps below:

1. Executes processes needed before generating the site documentation.

mvn pre-site

Output:

mvn pre-site


2. Generates the project’s site documentation

mvn site

Output:

mvn site


3. Executes processes needed after generating the site documentation

mvn post-site

Output:

mvn post-site


4. Deploys the generated site documentation to a web server

mvn site-deploy

Output:

mvn site-deploy






Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Maven Dependency Scopes Maven Dependency Scopes
Feedback Collection System using Spring Boot Feedback Collection System using Spring Boot
How to Create an Executable JAR with Maven? How to Create an Executable JAR with Maven?
Skipping Tests with Maven Skipping Tests with Maven
Security with Spring Security and Spring Webflux Security with Spring Security and Spring Webflux

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14