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 WorkflowBelow 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 LifecycleHere 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
<?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: 2. This command compiles the source code of the project.
mvn compile Output: Maven Build Success:
3. This command runs the unit tests for the project.
mvn test Output: 4. This command packages the compiled code into a JAR or WAR file.
mvn package Output: 5. This command runs any checks to verify the quality of the package.
mvn verify Output: 6. This command installs the package into the local repository for use as a dependency in other projects locally.
mvn install Output: Build Successfully:
 7. This command deploys the package to a remote repository for sharing with other developers.
mvn deploy Output:
Example 2: Clean LifecycleIn this example, we explain the Clean Lifecycle phases. Follow the steps below:
1. Executes processes needed before cleaning:
mvn pre-clean Output:
2. Removes files generated by the previous build:
mvn clean Output:
3. Executes processes needed after cleaning:
mvn post-clean Output:
Example 3: Site LifecycleIn 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:
2. Generates the project’s site documentation
mvn site Output:
3. Executes processes needed after generating the site documentation
mvn post-site Output:
4. Deploys the generated site documentation to a web server
mvn site-deploy Output:
|