Skip to content

Instantly share code, notes, and snippets.

@emoseman
Created May 13, 2013 15:34
Show Gist options
  • Save emoseman/5569218 to your computer and use it in GitHub Desktop.
Save emoseman/5569218 to your computer and use it in GitHub Desktop.
Maven: Packaging dependencies alongside project JAR.
Configure the Maven Jar Plugin to do so (or more precisely, the Maven Archiver):
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.acme.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
...
<dependencies>
<dependency>
<groupId>dependency1</groupId>
<artifactId>dependency1</artifactId>
<version>X.Y</version>
</dependency>
<dependency>
<groupId>dependency2</groupId>
<artifactId>dependency2</artifactId>
<version>W.Z</version>
</dependency>
</dependencies>
...
</project>
And this will produce a MANIFEST.MF with the following entries:
...
Main-Class: fully.qualified.MainClass
Class-Path: lib/dependency1-X.Y.jar lib/dependency2-W.Z.jar
...
and create the following directory structure (...)
This is doable using the Maven Dependency Plugin and the dependency:copy-dependencies goal. From the documentation:
dependency:copy-dependencies takes the list of project direct dependencies and optionally transitive dependencies and copies them to a specified location, stripping the version if desired. This goal can also be run from the command line.
You could bind it on the package phase:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment