Master Maven: Essential Dependency Management and Plugin Configuration Guide
This comprehensive guide walks you through Maven's core concepts—including repository setup, basic POM coordinates, dependency management, handling transitive dependencies, exclusions, variable properties, multi‑module projects, and powerful plugins such as jar, assembly, and shade—while also covering build‑time settings like compiler version, resource exclusion, and main‑class configuration.
If you have development experience, you may have struggled with managing JAR files in the lib directory and dealing with compatibility issues; Maven dramatically simplifies this by handling project builds and dependencies.
Maven is a project management tool that can build Java projects and manage dependencies.
1. Repository Configuration
Maven introduces the concept of a repository. When you publish a JAR to a repository, other developers can reference it as a dependency; Maven first checks the central repository, then a private repository if configured, and finally the local repository.
2. Basic POM Information
groupId : usually the reversed domain of the organization.
artifactId : the project name.
version : the project version.
name : a short name for the project.
description : a brief description of the project.
Example POM snippet:
<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>3. Dependency Management
Define common dependencies in a dependencyManagement section so that sub‑modules can inherit version information without repeating it.
<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>Modules can then declare only the groupId and artifactId, inheriting the version from the parent.
4. Handling Transitive Dependencies
When a dependency itself depends on other libraries, Maven resolves the full dependency graph automatically. You only need to declare the direct dependency; Maven will pull in the required transitive ones.
5. Excluding and Optional Dependencies
Use the excludes tag to prevent specific transitive dependencies from being included, and the optional flag to stop a dependency from being transitively passed to downstream projects.
<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>6. Variable Configuration
Define reusable version variables in a properties section and reference them with ${} placeholders.
<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>7. Multi‑Module Projects
Use the modules tag in the parent POM to list sub‑modules. Sub‑modules inherit the parent’s configuration, including dependency management.
<modules>
<module>module-1</module>
<module>module-2</module>
</modules>Example sub‑module POM (module‑1) inheriting the parent:
<parent>
<groupId>xyz.ibudai</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>8. Plugin Management
8.1 Jar Plugin
The maven-jar-plugin lets you add custom entries to the JAR manifest, such as the main class.
<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>8.2 Assembly Plugin
The maven-assembly-plugin can create an "uber‑jar" that bundles all project 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>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>8.3 Shade Plugin
The maven-shade-plugin also creates an uber‑jar but offers advanced features such as selective inclusion, class relocation, and 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>9. Build‑time Settings
9.1 Compiler Plugin
Specify the Java source and target versions.
<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>9.2 Resource Exclusion
Exclude configuration files (e.g., application.yml) from the packaged JAR so they can be modified without rebuilding.
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.yml</exclude>
</excludes>
</resource>
</resources>9.3 Main‑Class Configuration
Define the entry point of the application to avoid "no main class found" errors.
<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 Maven configurations you can manage dependencies efficiently, build modular multi‑module projects, and create executable JARs with the exact content you need.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
