In this article, we will cover how to Build Java Project Using Maven and how to build a Java Sprint Boot Project using Maven.
Maven is a powerful build automation tool used primarily for Java projects. It simplifies the build process and manages project dependencies, allowing developers to focus on coding rather than configuration. This article will guide you through the process of building a Java project using Maven.
Table of Contents
Prerequisites
Before you start, ensure you have the following installed on your system.
- AWS Account with Ubuntu 24.04 LTS EC2 Instance.
- Java Development Kit (JDK), Maven installed.
Step #1:Set Up Ubuntu EC2 Instance
If you don’t have JDK or Maven installed on your system you can install it by using following commands.
First Update the package list.
sudo apt update

Spring Boot requires Java, so install OpenJDK.
sudo apt install openjdk-17-jdk -y

Verify the Java installation.
java -version

You should see the version details of your Java installation.
Now lets install Maven. Maven is the build tool used to manage Java projects.
sudo apt install maven -y

Verify the Maven installation.
mvn -version

Step #2:Create Java Project Using Maven
Maven follows a standard directory layout, which makes it easier to manage projects. To create a new Maven project, you can use the command line.
Run the following command to create a new project.
mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

- groupId: A unique identifier for your project (usually the package name).
- artifactId: The name of your project.
- archetypeArtifactId: The template for the project;
maven-archetype-quickstart
creates a simple Java project. - interactiveMode: Set to
false
to skip interactive prompts.
This command will generate the following directory structure.
myapp
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── App.java
└── test
└── java
└── com
└── example
└── AppTest.java
Navigate to the project directory.
cd helloworld

Open the pom.xml
file and update the file for Spring Boot dependencies.
The pom.xml
(Project Object Model) file is the core of a Maven project. It contains configuration details, such as dependencies, build settings, and project information.
nano pom.xml

replace its content with the following:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>helloworld</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Step #3:Create Spring Boot Application with Maven
Create a new file named HelloworldApplication.java
in the src/main/java/com/example
directory with the following content:
For that first navigate to the directory.
cd src/main/java/com/example

Create a file named HelloworldApplication.java
nano HelloworldApplication.java

Add the following content into it.
package com.example.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
@RestController
class HelloWorldController {
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}
}
}

Step #4:Build and Run the Application
exit the directories till the root directory of application.
cd ../../../../..

Build the application.
mvn clean install

Explanation:
- clean: Deletes the
target
directory, which contains the compiled code and other build artifacts. - install: Compiles the code, runs tests, and packages the application into a JAR file, which is stored in the
target
directory.
If everything is set up correctly, you should see output indicating a successful build.
Run the application from the root of your project directory (where the pom.xml
file is located).
mvn spring-boot:run

Output:

Access the Hello World endpoint. For that open your web browser and run,
http://<EC2-Instance-IP>:8080/hello
You should see the message “Hello World!”.

Conclusion:
Maven is a powerful tool that simplifies the process of building and managing Java projects. By following the steps outlined in this article, you can set up a new Java project, manage dependencies, and build your application effortlessly. With practice, you’ll find that Maven becomes an indispensable part of your Java development workflow.
Related Articles:
How to Install Elastic Stack on Ubuntu 24.04 LTS
Reference: