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.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
How to Package Java Projects into a Clean JAR with a Separate lib Directory Using Maven

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).

Main class source code
Main class source code

Adding a Third‑Party Dependency

Gson is added as an example external library.

Maven dependency declaration
Maven dependency declaration

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).

Build output with jar and lib folder
Build output with jar and lib folder

Run the application with the classpath pointing to both the jar and the lib folder:

java -cp "module-admin.jar;lib/*" com.anfioo.Main

The program starts correctly, confirming that the modular packaging works as intended.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javabuild automationdependency managementmavenJar packaging
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.