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:
Similar Reads
Building a Java Project with Maven Maven is one of the most popular build automation tools. It is developed using the Java programming language and is primarily used for Java-based projects. However, it has been updated to support programming languages like C#, Ruby, and more. This article will teach us how to build a Java project wi
2 min read
Steps to Build a Typical Linux Project Linux is an operating system just like Windows and Unix. Meanwhile, Linux is an open-source project which keeps it different from other operating systems. There is a misconception in the developer community thinking that Linux is the tough part to handle that a normal person can't use it. In reality
4 min read
How to Test a Maven Project using EasyMock? Always a software project is prepared and tested as an individual unit. We pass different scenarios and expect the original values should match. One such approach is "Easymock" and in this article via a sample project, handled when and how to mock void methods. We are going to see this approach via
3 min read
How to Test a Maven Project using XMLUnit2? XML stands for Extensible Markup Language and it is widely used in many places for software projects. Whenever we need to port information across multiple operating systems/multiple languages, XML is an effective way of communication. To convey messages easily also, XML helps and it should be well-f
6 min read
How to create TestLInk Project? Testlink is a web-based software that is used for test management which helps to check for QA or quality assurance, the testlink software is an open-source project. Table of Content Steps to create a TestLink ProjectConclusionIt is a software that offers various features such as test cases, test pla
4 min read
Multi-Module Project with Maven A multi-module project in Maven allows you to manage a collection of related projects in a single build. This approach is particularly useful for large applications where different modules have distinct functionalities but need to be managed, built, and deployed together. By using the parent POM (Pr
4 min read
10 Best Software Testing Project Ideas The process of assessing the functionality of a software program is known as Software testing. It is a type of process in which an application is checked to determine whether it has any defaults or errors and if it meets the requirements of the customer. In this article, detailed knowledge is given
8 min read
Maven Remote Repository Maven Remote Repository is one of the repositories in the Maven repositories. It is a repository on a remote server that contains artifacts, that can be used by Maven projects. These artifacts include libraries, plugins, and other dependencies that a project may require. The remote repository is acc
4 min read
How to Configure a Maven Project in Jenkins ? Jenkins is a powerful automation server broadly utilized in software development for continuous integration (CI) and continuous delivery (CD) processes, it empowers developers to automate different parts of the product advancement lifecycle, including building, testing, and deploying applications, o
8 min read