Maven - Build & Test Project
Last Updated :
18 Jun, 2024
Maven is a build automation tool used for Java projects. It simplifies the process of project building, dependency management, and project documentation. This guide will walk you through the steps of building and testing a Maven project.
Key Concepts:
- Project Object Model: It is the heart of the Maven project because it includes project configuration and required dependencies project artifacts and other things.
- Dependencies: Libraries or other projects required to build your project.
- Plugins: Extension to Maven to execute various build tasks such as compiling code, running test cases, and others.
- Goals: A single task that contributes to the build and test process.
- Life cycle: A sequence of phases that includes default phases like validate, compile, test, package, verify, install, and other phases.
Workflow Diagram:
Below we provide how internally the maven build process is done with testing test cases also.
graph TB
subgraph Project Structure
A[Project Source Code]
A1[pom.xml]
A2[src/main/java/com/example/App.java]
A3[src/test/java/com/example/AppTest.java]
end
subgraph Maven Commands
B[mvn validate]
C[mvn compile]
D[mvn test]
E[mvn package]
F[mvn verify]
G[mvn install]
H[mvn deploy]
end
A --> B
B --> C
C --> D
D --> E
E --> F
F --> G
G --> H
Description of Each Phase:
- Validate: The Maven Ensures the project is valid and all required information is available.
- Compile: Maven Build tool Compiles the source code.
- Test: Executes tests to validate code correctness
- Package: Packages the compiled code into a distributed formats like JAR, WAR and other formats
- Verify: Performs additional checks, typically for integration testing.
- Install: Installs the package to the local Maven repository.
- Deploy: Deploys the package to a remote repository for use by other projects.
Build and Test Maven Project
Here we created a sample Maven project using Spring Initializr with the required dependencies for the project. Below is the step-by-step process for building and testing a Maven project using Maven commands.
Step 1: Create a Sample Maven Project
First create a sample maven project by using your favorite IDE. Here, we use Spring Tool Suite and select required dependencies for this project. Below are the dependencies.
Dependencies:
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Project Structure:
Step 2: Write Sample Test Cases
Once project is created successfully, write sample test cases in the test class which is located in src/test/java location in the project.
MavenbuildApplicationTests.java:
Java
package com.app;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MavenbuildApplicationTests {
@Test
void contextLoads() {
}
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}
Step 3: Validate the Project
Now, open the command prompt and redirect to your project folder and run the following command to validate the project:
mvn validate
Output:
Step 4: Compile the Source Code
Once successfully validated the project, we need to compile the source code.
mvn compile
Output:
Step 5: Run Unit Tests
Now, Run the unit tests once compilation is completed.
mvn test
Output:
Step 6: Package the Compiled Code
Now Package the compiled code.
mvn package
Output:
Step 7: Run Integration Tests and Verify
Run integration tests and perform additional verifications
mvn verify
Output:

Maven Build Success:
Step 8: Install the Package to Local Repository
Now, Install the package to Local Repository.
mvn install
Output:

Maven Build Success:
Step 9: Deploy the Package to Remote Repository
Now Deploy the package to Remote Repository.
mvn deploy
Output:
Maven Build Success: