Including dependencies in a jar with Maven
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Maven is a powerful project management tool used primarily for Java projects. It handles project builds, dependency management, and other aspects seamlessly through its Project Object Model (POM) configuration. When it comes to packaging Java projects, Maven provides an efficient way of bundling code along with its dependencies into a single executable JAR (Java ARchive) file. This process is especially useful when your project needs to be portable or easily distributable without requiring separate dependency installations.
Understanding Maven Dependencies
Dependencies in Maven are external Java libraries that your project requires to function. These are typically specified in the pom.xml file of your project under the <dependencies> tag. Maven automatically downloads these dependencies from central repositories and includes them in your project’s classpath during the build phase. However, by default, the standard JAR file created by Maven does not include these dependencies, resulting in a lightweight JAR file that requires those dependencies to be present in the classpath during runtime.
Why Include Dependencies in a JAR?
Including dependencies directly in your JAR file can significantly simplify deployment and execution requirements, ensuring that all necessary code is packaged together. This approach, known as creating a "fat JAR" or "uber JAR," is particularly beneficial when deploying standalone applications where you want to minimize external configurations or when distributing a tool to users who may not be familiar with Java or Maven.
How to Include Dependencies in a JAR with Maven
To bundle dependencies within a JAR file in Maven, you need to use a plugin capable of handling this task. The most commonly used plugins are:
- Maven Assembly Plugin: This plugin is used to create a single distributable archive which includes the project's dependencies along with the compiled classes.
- Maven Shade Plugin: This plugin provides similar functionality to the Assembly plugin but with additional capabilities like renaming or removing specific classes and resources, and more fine-grained control over the final JAR.
Using Maven Assembly Plugin
To use the Maven Assembly Plugin, you must define it in your pom.xml. Here's a simple example:
This configuration snippet tells Maven to bind the assembly:single goal to the package phase, which assembles the project’s code along with its dependencies into a single JAR specified by the jar-with-dependencies descriptor.
Using Maven Shade Plugin
Here’s an example configuration using the Maven Shade Plugin:
This configures the Shade Plugin to create a shaded (fat) JAR during the package phase, including handling of the manifest for specifying the main class.
Summary Table:
| Feature | Maven Assembly Plugin | Maven Shade Plugin |
| Primary Use | Bundle dependencies and project files into a single JAR. | Same as Assembly, with additional features for handling conflicts and transforming contents. |
| Configuration Complexity | Moderate | High, due to additional features. |
| Flexibility | Less flexible, basic packaging needs. | More flexible, with extensive control over the JAR’s contents. |
| Common Usage | Simple projects with straightforward dependencies. | Complex projects needing detailed configuration of packaged JAR. |
Conclusion
Packaging a JAR with all dependencies in Maven simplifies deployment by ensuring all necessary components are contained within a single, executable JAR file. Depending on your project’s needs, you can choose between the Maven Assembly Plugin or the Maven Shade Plugin to achieve this packaging. While the Assembly plugin is often sufficient for many projects, the Shade Plugin offers enhanced features that are valuable for more complex builds.

