Using Maven to Create an Executable JAR with Dependencies

Maven

Packaging Maven Projects into a Single Executable JAR

When dealing with Java projects, it is frequently required to package your application into a single executable JAR file to facilitate distribution. Maven, a robust build automation tool, enables developers to integrate all dependencies within a single output JAR.

This article will walk you through the steps necessary to configure your Maven project to include all dependency JARs in the final executable JAR. By following these steps, you may streamline the deployment process and ensure that your application functions well across several settings.

Command Description
<plugin> Defines a Maven plugin for usage in the build process. Plugins can perform specific activities, such as code compilation or project packaging.
<groupId> Specifies the Maven project's group identification, which is commonly a reverse domain name.
<artifactId> Specifies the artifact's identifier, which is the project name.
<version> Specifies the project's current version.
<build> This file contains the project's build configuration, which includes plugins and resources.
<descriptorRef> Specifies a predetermined descriptor for the Maven Assembly Plugin to utilize, such as "jar-with-dependencies".
<mainClass> Specifies the primary class that will be executed when the JAR file is run.
<execution> Defines an execution block within a plugin that specifies tasks to be executed throughout various build phases.
mvn clean package Command to clean the project and package it as a JAR file with all dependencies.
java -jar target/...jar Command to launch the created JAR file, specifying its directory.

Creating and running an executable JAR with Maven

The scripts above show how to configure a Maven project to package all of its dependencies into a single executable JAR file. The first script is a Maven file that includes the project's settings. The element in this file defines the Maven Assembly Plugin. This plugin is required for building an executable JAR with all dependencies. The tag in the plugin configuration defines the use of the jar-with-dependencies descriptor. This guarantees that all the project's dependencies are packed into the final JAR file. The tag within the section is necessary because it informs Maven which class has the main method to run when the JAR is executed.

The block in the plugin setup is where the action takes place. The plugin's aim must be executed during the step of the build lifecycle. This is what initiates the generation of the JAR file with dependencies. The second script contains a basic command line instruction: mvn clean package. This command cleans the project (removes previous build artifacts), compiles the source code, runs tests, and packages it as a JAR file. The project's directory will contain the final JAR file, which will be named after the and specified in the pom.xml.

Using Maven, create an executable JAR file with dependencies.

Maven Configuration Script

<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.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <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.example.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Run the Maven command to package the JAR.

Command Line Instructions

mvn clean package
# This command will compile the code, run the tests, and create the JAR file
# The JAR file will include all dependencies specified in the pom.xml
# It will be located in the target directory of the project
# The final JAR file will be named my-app-1.0-SNAPSHOT-jar-with-dependencies.jar

# To run the JAR file, use the following command:
java -jar target/my-app-1.0-SNAPSHOT-jar-with-dependencies.jar
# Ensure that the mainClass specified in the pom.xml is correct
# This will start your application with all dependencies included

Advanced Maven Techniques for Packaging Dependencies.

Beyond the basic settings for building an executable JAR with Maven, there are several approaches that might help you improve your build process. One technique is to use the plugin rather than the plugin. The Maven Shade Plugin provides more extensive features for building uber-JARs (JARs that include dependencies). It allows you to rename packages to minimize classpath conflicts and improves handling of duplicate classes and resources. In addition, it provides fine-grained control over which dependencies are included in the final JAR.

Another critical component is controlling the size of your JAR file. Large JAR files might be difficult and slow to transmit or load. The allows you to omit superfluous files and classes, hence lowering the size of the final JAR. You can also configure the plugin to reduce the JAR size by deleting unnecessary information like unused classes or redundant resources. These sophisticated options are very handy when working on large projects with multiple dependencies.

  1. How do I indicate the primary class in my JAR?
  2. To specify the primary class, use the tag in the section of the Maven plugin configuration.
  3. How can I exclude certain dependencies from the final JAR?
  4. Use the tag in the plugin configuration to exclude dependencies from the final JAR.
  5. What is an uber-JAR?
  6. An uber-JAR is a JAR file that contains both your compiled code and all of its dependencies.
  7. How can I prevent classpath conflicts in my JAR?
  8. To avoid conflicts, you can rename packages within dependencies using the command.
  9. How do I deal with duplicate classes in dependencies?
  10. Configure the to manage redundant classes and resources, defining how duplicates should be handled in the final JAR.
  11. Can I only include certain files from a dependency?
  12. Yes, you may set the or to only include particular files from a dependence.
  13. How can I run the packed JAR?
  14. Use the command , followed by the path to your JAR file.
  15. How can I check the contents of a JAR file?
  16. To list the contents of a JAR file, use the command .
  17. What happens if my JAR file is too large?
  18. Use to omit superfluous files and reduce JAR size.

Final Thoughts on Maven Executable JARs

Using Maven to create an executable JAR with dependencies makes it easier to deploy Java applications. By properly configuring the file and employing plugins like the Maven Assembly Plugin or Maven Shade Plugin, developers can bundle all essential dependencies into a single JAR file. This method ensures that the application runs perfectly across multiple contexts, making distribution and execution simple. Following these procedures will allow you to successfully manage your project's dependencies and generate reliable, executable JAR files.