How to Package Java Projects into a Clean JAR with a Separate lib Directory Using Maven
Learn how to avoid creating a fat JAR by packaging your own Java classes into a standalone jar while placing third‑party dependencies in a dedicated lib folder, using Maven plugins and configuration steps demonstrated with a sample project, pom.xml, and runtime command.
Goal
In many Java deployments we prefer not to bundle all classes and dependencies into a single fat JAR. Instead we want the project's own classes in a clean xxx.jar and third‑party libraries placed under a lib/ sub‑directory for easier management, replacement, and multi‑environment deployment.
Demo Project Setup
A new Maven project is created to illustrate the approach. The main class com.anfioo.Main is written (see the screenshot below).
Adding a Third‑Party Dependency
Gson is added as an example external library.
POM Configuration
The pom.xml is configured with two plugins:
maven-jar-plugin – sets the final jar name, defines the main class in the manifest, and directs the output to ${project.build.directory}/output.
maven-dependency-plugin – copies all runtime dependencies into ${project.build.directory}/output/lib, creating the desired lib/ folder.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anfioo</groupId>
<artifactId>module-admin</artifactId>
<version>1.0.0</version>
<build>
<finalName>module-admin</finalName>
<plugins>
<!-- Make the JAR aware of the main class -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.anfioo.Main</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}/output</outputDirectory>
</configuration>
</plugin>
<!-- Copy dependencies to lib directory -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/output/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>Build and Run
Execute the Maven build: mvn clean package The build produces module-admin.jar and a lib/ directory containing the copied dependencies (see screenshot).
Run the application with the classpath pointing to both the jar and the lib folder:
java -cp "module-admin.jar;lib/*" com.anfioo.MainThe program starts correctly, confirming that the modular packaging works as intended.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
