Master Maven: From Basics to Advanced Dependency and Plugin Management

This comprehensive guide walks Java developers through Maven's core concepts, including repository setup, basic pom.xml elements, dependency scopes, transitive dependencies, exclusions, module configuration, unified dependency management, and powerful plugins for jar creation, assembly, and shading, enabling efficient build automation.

Architect
Architect
Architect
Master Maven: From Basics to Advanced Dependency and Plugin Management

Basic Configuration

Maven introduces the concept of repositories. When a JAR is needed, Maven first checks the local repository; if absent, it downloads from the central repository. Organizations can add private repositories that are consulted before the central one.

Basic Information

A minimal Maven project should define groupId, artifactId, version, name, and description.

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <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>

Dependency Management

Dependency Introduction

Use the dependencies element to add required libraries. The scope can be compile (default), runtime, provided, or test.

compile : needed at compile time (default)

runtime : not needed at compile time but required at runtime

provided : needed at compile time but supplied by the JDK or server at runtime

test : needed only for test compilation

Transitive Dependencies

Maven resolves transitive dependencies automatically. If Dependency-A depends on Dependency-B, which in turn depends on Dependency-C, Maven will pull Dependency-B and Dependency-C without manual specification.

image.png
image.png

Dependency Exclusion

When version conflicts arise, use the excludes tag to remove unwanted transitive dependencies. Alternatively, mark a dependency as optional so it is not propagated to downstream projects.

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

Module Configuration

Module Management

When a project contains multiple sub‑projects, list them under the modules element.

<modules>
  <module>module-1</module>
  <module>module-2</module>
</modules>

Module Inheritance

A child module can declare a parent pom to inherit all dependency and plugin configurations, including groupId and version. The relativePath points to the parent pom (default ../pom.xml).

<parent>
  <groupId>xyz.ibudai</groupId>
  <artifactId>maven-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module-1</artifactId>

Unified Management

Dependency Management

Define common dependencies and their versions once in the parent pom using dependencyManagement. Child modules only need to declare the groupId and artifactId; the version is inherited.

<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>
image.png
image.png

Plugin Management

Jar Plugin

The maven-jar-plugin can add custom entries to 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>
      </manifest>
      <manifestEntries>
        <Plugin-Id>demo-plugin</Plugin-Id>
        <Plugin-Version>1.0.0</Plugin-Version>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

Assembly Plugin

The maven-assembly-plugin can create an “uber‑jar” that bundles all 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>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Shade Plugin

The maven-shade-plugin also builds an uber‑jar but allows selective inclusion and package relocation to avoid version conflicts.

<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>

Build Configuration

Version Specification

Set the Java source and target version with the maven-compiler-plugin.

<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>

Resource Exclusion

Exclude configuration files (e.g., application.yml) from the packaged JAR.

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

Main Class Configuration

Define the entry point for the application using the mainClass element in the Spring Boot Maven plugin.

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <mainClass>xyz.ibudai.TestWebApplication</mainClass>
    <layout>JAR</layout>
  </configuration>
</plugin>
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.

Javadependency managementmavenbuild toolsPlugins
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

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.