Building a runnable jar with Maven 2 Building a runnable jar with Maven 2 java java

Building a runnable jar with Maven 2


The easiest way to do this would be to create an assembly using the maven-assembly-plugin and the predefined jar-with-dependencies descriptor. You'll also need to generate a manifest with a main-class entry for this uber jar. The snippet below shows how to configure the assembly plugin to do so:

<build>  <plugins>    <plugin>      <artifactId>maven-assembly-plugin</artifactId>      <configuration>        <descriptorRefs>          <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>        <archive>          <manifest>            <mainClass>fully.qualified.MainClass</mainClass>          </manifest>        </archive>      </configuration>    </plugin>  </plugins></build>

Then, to generate the assembly, just run:

mvn assembly:assembly

If you want to generate the assembly as part of your build, simply bind the assembly:single mojo to the package phase:

<build>  <plugins>    <plugin>      <artifactId>maven-assembly-plugin</artifactId>      <configuration>        <descriptorRefs>          <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>        <archive>          <manifest>            <mainClass>fully.qualified.MainClass</mainClass>          </manifest>        </archive>      </configuration>      <executions>        <execution>          <phase>package</phase>          <goals>            <goal>single</goal>          </goals>        </execution>      </executions>    </plugin>  </plugins></build>

And simply run:

mvn package


Maven is not packaging your dependencies inside your jar file, because you don't usually do this with Java programs.

Instead you deliver the dependencies together with your jar file and mention them in the Class-Path header of the Manifest.

To go this route, you'll need to enable the addClasspath property (documented here) for the maven-jar-plugin.

If you really want to include all your dependencies in your jar file, then you can use the Maven Assembly plugin to create a jar-with-dependencies.


This is what I would do for small projects. Most of the time you don't want one huge jar.

to build:mvn clean dependency:copy-dependencies package

to execute (in target dir):java -cp myjar.jar:./dependency/* com.something.MyClass