Understanding Maven and Its Configuration

T04BF
👋 Series: Algorithms | JAVA | MySQL | C Language
🫵 Did you code today? 1. Introduction to Maven

1.1 What is Maven

According to Apache's official definition:

Apache Maven is a software project management and comprehension tool.
Based on the concept of a project object model (POM), Maven can manage
a project’s build, reporting and documentation from a central piece
of information.
Maven serves as a project management utility. It uses a POM (Project Object Model) to control project builds, reports, and documentation through a concise configuration file.

Essentially, Maven functions as a project management tool that retrieves JAR dependencies via configuration in the pom.xml file instead of manually adding them.

1.2 Benefits of Using Maven

It simplifies and streamlines development, enhancing productivity and reducing bugs.

1.3 Creating a Maven Project in IntelliJ IDEA

1.4 Core Capabilities of Maven

These capabilities are mainly centered around two areas:

1.4.1 Build Management

Maven offers straightforward commands for handling project builds.

For instance, packaging a JAR file can be done with a single command like package.

1.4.2 Dependency Handling

Dependency Declaration
With Maven, JAR files are no longer added manually; instead, they are defined through the pom.xml file. For example, when integrating JavaFX, one would typically download and add the JavaFX libraries manually.

However, with Maven, it's sufficient to declare JavaFX dependencies in the pom.xml file:

<dependencies>
    <!-- Add dependency coordinates here, which may include multiple entries -->
</dependencies>

To include JavaFX dependencies, we would use:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>17.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-graphics -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>17.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-base -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-base</artifactId>
        <version>17.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>17.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-swing -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-swing</artifactId>
        <version>17.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>17.0.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-media -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>17.0.11</version>
    </dependency>
</dependencies>

Note: Always refresh the project after modifying the pom.xml file, especially when adding new dependencies.

Once refreshed, the newly added JARs will appear in the project.

Transitive Dependencies
When certain JARs reference other JARs, Maven handles these automatically. Without Maven, developers must track all transitive dependencies manually.

With Maven, once a dependancy is declared, it automatically fetches related JARs due to its transitive nature.

As illustrated, if project A includes jarA via Maven, then jarB and jarC are also pulled in automatically.

In this case, jarA and jarB/jarC are direct and indirect dependencies respectively.

Excluding Dependencies
Suppose project A doesn’t require jarB. This can be resolved by excluding it explicitly:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>6.0.6</version>
        
        <!-- Exclude specific dependency -->
        <exclusions>
            <exclusion>
                <artifactId>spring-jcl</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Additionally, plugins can assist in managing such exclusions more efficiently.

This screen allows direct exclusion of dependencies.

Dependency Resolution
When conflicts arise among dependencies in a project, Maven applies the shortest path first principle to resolve them.

1.5 Maven Repositories

Previously mentioned, Maven enables us to pull JAR dependencies into our projects with just a few lines in pom.xml. How does this work?

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>17.0.11</version>
</dependency>

These lines form the "coordinates" that uniquely identify each JAR.

Maven identifies JARs using the combination of groupId, artifactId, and version—none can be omitted.

After defining a dependency in pom.xml and refreshing the project, Maven searches the repository for the specified JAR based on these coordinates, downloads it, and integrates it into the project. This location is known as the repository.

Maven repositories are broadly categorized into local and remote types.

1.5.1 Local Repository

This refers to a directory on your local machine. Maven first checks the local repository for required JARs.

If present, it uses them directly; otherwise, it retrieves them from the remote repository.

The local repository path can be found in Maven settings.

1.5.2 Central Repository

This is a default remote repository built into Maven. It serves the global community and is maintained by the Maven team.

Repository URL: https://repo1.maven.org/maven2
Use https://mvnrepository.com to search and retrieve artifacts.

1.5.3 Private Repository (Nexus/Artifactory)

A private repository is often set up within organizations or departments, requiring specific access rights.

With a private repository, the download process changes:

(1) First, check the local repository; if found, return immediately.
(2) If not available locally, request from the private repository; return if found.
(3) If not found in the private repo, try downloading from the central repository; fail if unavailable.
(4) If found in the central repository, cache it in both the private and local repositories before serving.

1.6 Configuring Domestic Mirrors for Maven

Since the central repository is located overseas, downloading can be slow. Therefore, setting up domestic mirrors improves performance.

Modify the Maven configuration file, which can be downloaded from the article resources.

Replace the existing file at the appropriate location.

Thank you for reading! Please consider following!
T04BF
🫵 Don't forget to code today!

Tags: Maven java build-tools dependency-management repository

Posted on Tue, 07 Jul 2026 16:21:33 +0000 by ludjer