Comprehensive Maven Guide: Configuration, Dependency Management, Modules, and Plugins
This article provides a detailed tutorial on Maven, covering repository setup, basic POM structure, dependency management, module configuration, unified dependency versions, and common build plugins such as jar, assembly, and shade, with practical XML examples and code snippets.
Maven is a project management and build automation tool for Java that simplifies dependency handling, module organization, and plugin configuration, allowing developers to focus on business logic.
1. Basic Configuration
The Maven repository concept lets developers publish JARs to a central or private repository, from which dependencies are automatically downloaded to the local repository.
A minimal pom.xml includes the three essential coordinates: groupId , artifactId , and version :
<?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
Dependencies are declared inside the <dependencies> tag. The scope element can be compile , runtime , test , etc.
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>runtime</scope>
</dependency>
</dependencies>Transitive dependencies are resolved automatically; only the direct dependency needs to be declared.
To exclude a transitive dependency, use 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>
</exclude>
</excludes>
</dependency>Optional dependencies can be marked with <optional>true</optional> to prevent them from being transitively passed to downstream projects.
3. Variable Configuration
Version numbers can be centralized using the <properties> section and referenced 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>4. Module Configuration
Multi‑module projects use the <modules> tag to list sub‑modules. Sub‑modules inherit the parent’s coordinates and can share dependency management.
<modules>
<module>module-1</module>
<module>module-2</module>
</modules>Each sub‑module can declare a <parent> to inherit the parent POM:
<parent>
<groupId>xyz.ibudai</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module-1</artifactId>5. Unified Dependency Management
Define common versions in the parent’s <dependencyManagement> section; child modules only declare the dependency without a version, inheriting the parent’s 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>6. Plugin Management
Common Maven plugins include:
maven-jar-plugin : adds custom entries to the JAR manifest.
maven-assembly-plugin : creates an “uber‑jar” that bundles all dependencies.
maven-shade-plugin : similar to assembly but allows fine‑grained inclusion/exclusion and package relocation.
Example configuration for the shade plugin:
<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>These plugins enhance the build process by customizing the JAR manifest, bundling dependencies, and handling class‑path conflicts.
7. Build Configuration
Within the <build> section you can set the Java compiler version, exclude resource files from the final JAR, and specify the main class for executable JARs.
<plugins>
<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>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>xyz.ibudai.TestWebApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>By mastering these Maven features, developers can efficiently manage large Java projects, ensure consistent dependency versions, and produce reliable build artifacts.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.