Java Programming Tutorials

Java programming tutorials with many code examples!

Maven package with dependencies


How to use Maven to package a project with dependencies? The solution is to use maven-assembly-plugin as in the following example.


Here we’ll use very simple Maven project with compile dependency on Apache Commons Lang3 library. Packaging of the bundle is done by maven-assembly-plugin according to a configuration. Fortunately, the plugin comes with a couple of ready to use configurations, one of which is jar-with-dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""






Sample Java code using our dependency (Apache Commons Lang3):


import org.apache.commons.lang3.LocaleUtils;

import java.util.Locale;

public class JavaSolved {

    public static void main(String[] args) {
        for (Locale locale : LocaleUtils.availableLocaleSet()) {
            System.out.printf("Country: %s, language: %s%n",
                    locale.getCountry(), locale.getLanguage());

Now, running mvn assembly:assembly command will tell Maven to run the assembly plugin with configuration from the pom.xml.

All project classes with dependencies have been packed into target/solved-0.0.1-SNAPSHOT-jar-with-dependencies.jar. Here are sample entries that confirm correct packaging:

-rw-r--r--      1067  21-Aug-2015  18:16:20  com/farenda/java/JavaSolved.class
-rw-r--r--      7004   3-Apr-2015  14:30:26  org/apache/commons/lang3/AnnotationUtils.class
-rw-r--r--       246   3-Apr-2015  14:30:26  org/apache/commons/lang3/builder/Builder.class

As you can see dependent JAR has been unpacked and repackaged with project code. Such single JAR is easier to distribute than whole bunch.

Was it helpful? Share!