What Is The Process For Using Maven To Create An Executable JAR With Dependencies In Java?

To create an executable JAR with dependencies using Maven, you need to use the Maven Shade Plugin. The Shade Plugin is designed to create a single, executable JAR that includes all of its dependencies.

Here are the steps to create an executable JAR with dependencies using Maven:

Step 1: Add the Maven Shade Plugin to your project’s pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer>
                                <mainClass>com.example.MainClass</mainClass> <!-- Replace with your main class -->
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Step 2: Run the Maven package command:

mvn package

This will create an executable JAR file in the target directory with all dependencies bundled inside.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!