Getting started with Maven

java dev.to

As part of my training, I recently started learning Maven and how it is used in Java projects. Although I had worked with Java before, I had not really paid much attention to how dependencies were managed, how projects were built, or how tests were executed.

While working through a small Maven project, I got the opportunity to understand these concepts more practically. This post is a record of the basics I learned about Maven and some of the tasks I completed along the way.

What is Maven?

Maven is a build automation and project management tool that is commonly used with Java projects.

In simple terms, Maven helps us manage a Java project without having to manually handle every part of the build process. It can manage external libraries, compile our code, run tests, and package the application.

One of the most important files in a Maven project is the pom.xml file.

POM stands for Project Object Model. This file contains information about the project and tells Maven how the project should be managed.

Why Use Maven?

One of the main reasons Maven is useful is that it makes managing a Java project easier.

1. Dependency management

Java applications often need external libraries. Instead of downloading and adding these libraries manually, Maven can download them based on the dependencies defined in pom.xml.

For example, when I needed JUnit for my project, I added it as a dependency in the POM file.

2. Build automation

Maven can perform common tasks such as compiling code, running tests, and packaging an application using simple commands.

3. Standard project structure

Maven follows a standard project structure. This makes it easier to understand and work with projects created by other developers.

4. Plugins

Maven uses plugins to perform different tasks during the build process. For example, plugins can be used for compiling Java code, running tests, and packaging applications.

Overall, Maven provides a consistent way of managing and building Java projects.

Installing Maven

In Ubuntu, Maven can be installed using the following commands:

sudo apt update
sudo apt install maven

After installing Maven, we can check whether it was installed correctly using:

mvn -version

This command displays information such as the Maven version, Java version, Maven installation location, and operating system.

For example, the output looks similar to:

Apache Maven 3.8.7
Maven home: /usr/share/maven
Java version: 21.0.12, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.17.0-14-generic", arch: "amd64", family: "unix"
Enter fullscreen mode Exit fullscreen mode

Understanding the pom.xml File

The pom.xml file is one of the most important parts of a Maven project.

It contains information about the project, its dependencies, and its build configuration.

For example, dependencies are placed inside the section:

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-library</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

The <dependencies> element acts as a container, while each <dependency> represents one library that the project needs.

In my project, I added JUnit 5 for testing and Lombok later for reducing the amount of boilerplate code.

Maven Dependencies

A dependency is an external library that our project needs.

For example, instead of writing our own testing framework, we can add JUnit as a dependency and use it to create unit tests.

I added the JUnit 5 dependency to my pom.xml and then created a test for a simple Java method.

I also added Lombok later in the project. Lombok can generate commonly used methods such as getters and setters automatically using annotations.

This helped me understand that Maven doesn't just build the project,but also makes managing the libraries used by the project much easier.

Maven Repositories

While learning about dependencies, I came across Maven repositories. A repository is a place where Maven stores or retrieves project dependencies and other artifacts.

There are three main types of Maven repositories.

1. Local Repository

The local repository is stored on our own computer.

When Maven downloads a dependency, it stores a copy locally so that it can be reused later without downloading it again.

On Linux, the local Maven repository is usually located under:

~/.m2/repository

2. Central Repository

The Maven Central Repository is a public repository containing a large number of Java libraries and project artifacts.

When we add a commonly used dependency to pom.xml, Maven can retrieve it from Maven Central.

3. Remote Repository

A remote repository is hosted on a remote server and can be used by organizations to store and share their own libraries and artifacts.

Companies may use private remote repositories for internal projects and dependencies.

A simple way to remember the three is:

Local Repository → My computer
Central Repository → Public Maven repository
Remote Repository → Remote/private server

Creating My First Maven Project

After learning the basics, I created a Maven Java project.

The project had the standard Maven structure, including:

src/main/java
src/test/java
pom.xml

I then created a simple Calculator class with a method that accepts an integer and returns its square.

public class Calculator {
    public int square(int number) {
        return number * number;
    }
}
Enter fullscreen mode Exit fullscreen mode

For example, if the method receives 5, it returns 25.

Adding a Unit Test

After creating the method, I added JUnit 5 to the project and created a unit test.

The test checks whether the square() method returns the expected result:

@Test
void testSquare() {
    Calculator calculator = new Calculator();
    assertEquals(25, calculator.square(5));
}
Enter fullscreen mode Exit fullscreen mode

This was a useful example because it showed me how Maven, dependencies, and unit testing work together.

Running Unit Tests with Maven

To run the test the command I used was:

mvn test

Maven then compiles the necessary code, runs the tests, and displays the results in the terminal.

Another command that can be used is:

mvn clean test

The clean part removes the previous build output before the tests are run.

When the tests pass, Maven displays a successful build message.

Understanding the Maven Wrapper

The Maven Wrapper allows a project to use a specific Maven version without requiring every developer to manually install that version on their computer.

A Maven project can contain files such as:

mvnw
mvnw.cmd
.mvn/

On Linux, Maven commands can then be run using:

./mvnw test

Instead of:

mvn test

This is useful when a team wants everyone working with the same Maven version.

Creating a Student Class

As another part of the exercise, I created a Student class with the following properties:

id        - int
name      - String
age       - int
subjects  - List<String>
Enter fullscreen mode Exit fullscreen mode

I created a Student object in the main method and set its values:

ID: 1
Name: Jane Doe
Age: 20
Subjects: Mathematics, English, History
Enter fullscreen mode Exit fullscreen mode

Initially, I created the getter and setter methods manually.

For example:

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
Enter fullscreen mode Exit fullscreen mode

Later, I added Lombok and used annotations to generate these methods automatically.

@Getter
@Setter
public class Student {
    private int id;
    private String name;
    private int age;
    private List<String> subjects;
}
Enter fullscreen mode Exit fullscreen mode

This showed me how a dependency can be used to simplify the code in a Java project.

Some Maven Commands I Learned

Here are some of the Maven commands I learned during this exercise:

mvn -version - Displays the installed Maven version
mvn clean - Removes previous build output
mvn compile - Compiles the source code
mvn test - Runs the unit tests
mvn package - Builds and packages the project
mvn install - Installs the project's artifact in the local repository

Some of the main things I learned:

  • Maven is used to manage and build Java projects.
  • pom.xml contains important project and dependency information.
  • Dependencies allow us to use external libraries in our projects.
  • Maven repositories are used to store and retrieve dependencies and other artifacts.
  • Maven can automate tasks such as compiling, testing, and packaging.
  • JUnit can be added as a Maven dependency to write unit tests.
  • The Maven Wrapper helps projects use a consistent Maven version.
  • Lombok can reduce repetitive getter and setter code.

Source: dev.to

arrow_back Back to Tutorials