Comprehensive Maven Guide: Configuration, Dependency Management, Multi‑Module Projects, and Plugins
This article provides a detailed tutorial on using Maven for Java projects, covering basic repository setup, dependency declaration, scope options, indirect dependencies, exclusions, variable properties, multi‑module configuration, dependencyManagement, and essential plugins such as jar, assembly, and shade, with practical code examples and best‑practice recommendations.
1. Basic Configuration
Maven introduces the concept of repositories; artifacts are downloaded from the central repository to a local cache, and organizations may host private repositories for internal use.
Typical Maven project structure includes groupId , artifactId , and version elements.
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
<!-- Fixed 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 inside the dependencies tag.
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>runtime</scope>
</dependency>
</dependencies>2.2 Scope Options
The scope element can be compile , provided , runtime , test , or system (see image in original article).
2.3 Transitive Dependencies
Maven resolves transitive dependencies automatically; only the direct dependency needs to be declared.
2.4 Excluding Dependencies
Use the excludes tag to prevent unwanted transitive artifacts.
<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.5 Variable Configuration
Define reusable version numbers in properties and reference them with ${} .
<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. Multi‑Module Configuration
3.1 Module Management
Declare sub‑modules with the modules tag.
<modules>
<module>module-1</module>
<module>module-2</module>
</modules>3.2 Module Inheritance
Child modules inherit groupId and version from a parent pom; the relativePath points to the parent pom.
<parent>
<groupId>xyz.ibudai</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>4. Unified Dependency Management
Place common dependencies in the parent’s dependencyManagement section; child modules only declare the dependency without version.
<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
Customise the JAR manifest and add extra entries.
<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>
<manifestEntries>
<Plugin-Id>demo-plugin</Plugin-Id>
<Plugin-Version>1.0.0</Plugin-Version>
</manifestEntries>
</manifest>
</archive>
</configuration>
</plugin>5.2 Assembly Plugin
Creates an “uber‑JAR” that bundles all runtime 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>5.3 Shade Plugin
Provides more flexible shading, allowing selective inclusion, package relocation, and file filtering.
<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>The article concludes with promotional material unrelated to Maven; that part is omitted from the technical summary.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.