Master Maven: Essential Dependency Management and Build Techniques for Java Projects
This article walks developers through Maven's core concepts, including repository configuration, basic project structure, dependency management, module inheritance, unified dependency versions, and common plugins such as jar, assembly, and shade, while also covering build settings like JDK version, resource exclusion, and main‑class configuration.
If you have Java development experience, you’ve likely struggled with manually handling JAR files and compatibility issues; Maven simplifies this by managing project builds and dependencies.
Basic Configuration
1. Repository Configuration
Maven uses repositories to store and retrieve JAR files. Developers can rely on the central repository or set up a private repository that Maven checks before falling back to the central one.
2. Basic Project Structure
A minimal Maven project includes groupId, artifactId, and version. Example:
<?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>Dependency Management
1. Adding Dependencies
Use the dependencies tag to declare required libraries:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>runtime</scope>
</dependency>
</dependencies>2. Transitive Dependencies
Maven resolves transitive dependencies automatically, so you only need to declare the direct dependency.
3. Dependency Order
Maven follows a “first defined, first imported” rule when multiple versions of the same transitive dependency exist.
4. Excluding Dependencies
Use the excludes tag to prevent specific transitive dependencies from being included.
<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>5. Optional Dependencies
Mark a dependency as optional to stop it from being transitively propagated.
<dependency>
<groupId>xyz.ibudai</groupId>
<artifactId>demo-a</artifactId>
<version>1.0.0</version>
<optional>true</optional>
</dependency>6. Property Variables
Define version numbers and other values in properties and reference them with ${}:
<properties>
<mysql.version>8.0.30</mysql.version>
<junit.version>4.13.2</junit.version>
</properties>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>Module Configuration
1. Module Management
Define sub‑modules with the modules tag:
<modules>
<module>module-1</module>
<module>module-2</module>
</modules>2. Module Inheritance
Child modules inherit groupId, artifactId, and version from a parent POM via the parent element.
<parent>
<groupId>xyz.ibudai</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>Unified Management
1. Dependency Management
Place common dependency versions in the parent’s dependencyManagement section so child modules can reference them without specifying versions.
<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>Plugin Management
1. Jar Plugin
Customize the JAR manifest using maven-jar-plugin:
<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>2. Assembly Plugin
Package the project together with all its dependencies into a single JAR:
<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>3. Shade Plugin
Creates an uber‑JAR with optional class relocation and dependency minimization:
<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
1. JDK Version
Set the Java source and target versions in the 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>2. Resource Exclusion
Exclude configuration files from the packaged JAR:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.yml</exclude>
</excludes>
</resource>
</resources>3. Main Class
Specify the entry point for the application:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>xyz.ibudai.TestWebApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>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.
