Comprehensive Maven Guide: Repository Setup, Dependency Management, Multi‑Module Projects, and Plugin Configuration

This article provides a step‑by‑step tutorial on using Maven for Java backend projects, covering repository configuration, basic POM structure, dependency declaration, scope handling, exclusion, variable properties, multi‑module management, dependencyManagement, and common build plugins such as jar, assembly, and shade.

Top Architect
Top Architect
Top Architect
Comprehensive Maven Guide: Repository Setup, Dependency Management, Multi‑Module Projects, and Plugin Configuration

1. Basic Configuration

Maven introduces the concept of repositories; developers publish JARs to a remote repository and Maven downloads them to the local repository when referenced, optionally using a private repository before falling back to the central one.

A minimal Maven project includes groupId, artifactId, and version elements.

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    <!-- fixed 4.0.0, specifies the POM model version -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>xyz.ibudai</groupId>
    <artifactId>maven-demo</artifactId>
    <version>1.0.0‑SNAPSHOT</version>
    <description>This is maven demo.</description>
</project>

2. Dependency Management

2.1 Dependency Introduction

Dependencies are added via the dependencies tag.

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql‑connector‑java</artifactId>
        <version>8.0.27</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

The scope element can be compile, provided, runtime, test, or system.

2.2 Transitive Dependencies

Maven resolves transitive dependencies automatically; only the direct dependency needs to be declared.

2.3 Dependency Exclusion

Conflicting versions can be excluded using the excludes tag.

<dependency>
    <groupId>xyz.ibudai</groupId>
    <artifactId>demo‑a</artifactId>
    <version>1.0.0</version>
    <excludes>
        <exclude>
            <groupId>xyz.ibudai</groupId>
            <artifactId>dependency‑b</artifactId>
            <version>1.0.0</version>
        </exclude>
    </excludes>
</dependency>

2.4 Variable Configuration

Version numbers can be centralized with properties and referenced via ${}.

<properties>
    <mysql.version>8.0.30</mysql.version>
    <junit.version>4.13.2</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql‑connector‑java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
    </dependency>
</dependencies>

3. Module Configuration

Multi‑module projects use the modules tag in the parent POM.

<modules>
    <module>module‑1</module>
    <module>module‑2</module>
</modules>

Child modules inherit groupId, artifactId, and version from the parent via the parent element.

4. Unified Dependency Management

Common dependencies are declared once in the parent’s dependencyManagement section and inherited by child modules.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql‑connector‑java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

5. Plugin Management

5.1 Maven‑Jar‑Plugin

Customizes the JAR manifest.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven‑jar‑plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>org.example.MyTest</mainClass>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <manifestEntries>
                    <Plugin‑Id>demo‑plugin</Plugin‑Id>
                    <Plugin‑Version>1.0.0</Plugin‑Version>
                </manifestEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>

5.2 Maven‑Assembly‑Plugin

Packages all project classes together with their dependencies.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven‑assembly‑plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar‑with‑dependencies</descriptorRef>
        </descriptorRefs>
        <finalName>${project.artifactId}-${project.version}-all</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <attach>false</attach>
    </configuration>
    <executions>
        <execution>
            <id>make‑assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

5.3 Maven‑Shade‑Plugin

Creates an uber‑JAR with optional class relocation and selective inclusion.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven‑shade‑plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <minimizeJar>true</minimizeJar>
        <artifactSet>
            <includes>
                <include>com.fasterxml.jackson.core:jackson‑core</include>
            </includes>
        </artifactSet>
        <relocations>
            <relocation>
                <pattern>com.fasterxml.jackson.core</pattern>
                <shadedPattern>com.ibudai.fasterxml.jackson.core</shadedPattern>
            </relocation>
        </relocations>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META‑INF/license/**</exclude>
                    <exclude>META‑INF/*</exclude>
                    <exclude>LICENSE</exclude>
                    <exclude>NOTICE</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
</plugin>

6. Build Configuration

6.1 JDK Version

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven‑compiler‑plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>8</source>
        <target>8</target>
    </configuration>
</plugin>

6.2 Resource Exclusion

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>application.yml</exclude>
        </excludes>
    </resource>
</resources>

6.3 Main Class Specification

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring‑boot‑maven‑plugin</artifactId>
    <configuration>
        <mainClass>xyz.ibudai.TestWebApplication</mainClass>
        <layout>JAR</layout>
    </configuration>
</plugin>

By following these configurations, developers can efficiently manage dependencies, avoid version conflicts, build modular Maven projects, and produce executable JARs with the desired metadata and bundled libraries.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentdependency managementmavenbuild tools
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.