How can I create an executable/runnable JAR with dependencies using Maven?

Cover Image for How can I create an executable/runnable JAR with dependencies using Maven?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🚀 Creating an Executable JAR with Dependencies using Maven: A Complete Guide 📦

So, you've built your awesome Java project, and now you want to package it into a single, self-contained JAR for easy distribution. But there's one problem – your project has several dependencies! 😱

No need to panic! Maven, the powerful build tool, comes to the rescue. In this guide, we'll show you how to create an executable JAR with all its dependencies using Maven, so your users can run your project without any hassle. Let's get started! 💪

1. Start with a Maven project 🏗️

Before we dive into the juicy details, make sure you have a Maven project set up. If you don't, fear not! Simply head over to the Maven website, download and install Maven, and use it to create a new project.

2. Configure Maven's assembly plugin 🛠️

To create a fully executable JAR, we need to configure Maven's assembly plugin. Open your project's pom.xml file and ensure that the following plugin is included within the <build> section:

<plugins>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.yourproject.YourMainClass</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  ...
</plugins>

Make sure to replace com.yourproject.YourMainClass with the actual fully qualified name of your project's main class.

3. Build your executable JAR 🏭

Now that you have Maven set up and the assembly plugin configured, it's time to build your executable JAR. Open a terminal or command prompt, navigate to your project's root directory, and run the following command:

mvn clean package

Maven will start building your project and packaging all its dependencies into the JAR. Once the process is complete, you will find your shiny new executable JAR in the target directory of your project.

4. Test your JAR 🔍

Testing is vital before sharing your JAR with the world. To ensure everything is working as expected, run your executable JAR using the following command:

java -jar target/your-jar-filename.jar

Replace your-jar-filename.jar with the actual filename of your JAR. If your project has a graphical user interface (GUI), check that it launches correctly and all its functionalities work seamlessly.

5. Share and distribute your JAR 🌍

Congratulations, you've successfully created an executable JAR with all its dependencies! Now it's time to share your masterpiece with the world. There are various ways to distribute your JAR:

  • Upload it to a file-sharing platform like Dropbox or Google Drive and share the download link.

  • Create a GitHub repository and upload your project, making the JAR available for download.

  • Publish your project on a package registry like Maven Central, so others can easily include it as a dependency in their projects.

📣 Engage with the Community!

Did you find this guide helpful? Have you encountered any issues or discovered additional tips along the way? We'd love to hear from you!

Head over to our blog and leave a comment, share your experience, or ask any questions you may have. Together, let's continue learning and growing as a community of tech enthusiasts! 🌐💬🚀

That concludes our guide on creating an executable JAR with dependencies using Maven. We hope you found it informative and easy to follow. Now go ahead and package your project into a self-contained JAR, and let it shine for the world to see! 🌟✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello