I'm using mvn package
to create a runnable jar with all dependencies packed inside, which runs fine.
But I'd prefer to have all external dependencies packed in a separate folder. What would I have to change therefore?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>my.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Normally, when we package a project into a jarW file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpathW in order to execute a class in the project's jar file that uses one of the dependencies.
You can use the newly created jar using a <classifier> tag.
The value jar-with-dependencies tells Maven to build a JAR file with dependencies which is another term for a Fat JAR. The executions XML element tells Maven which Maven build phase and goal this Maven plugin should be executed during. The maven-assembly-plugin should always be executed during the package phase.
Maven - Creating a Single executable Jar with dependencies. Apache Maven Assembly plugin allows to create an executable jar which includes all its dependencies within it. The resultant jar is also called 'fat jar'. Create a project using maven-archetype-quickstart with following parameters.
my.jar will not be an executable JAR since it won't contain itself the dependency classes. It sounds like you want to create an assembly, like a custom ZIP? @Tunaki it don't need to contain dependency classes, in real project there will be launcher too, that will get all libraries and run that .jar with valid classpath. I will edit title.
When creating a jar file, we usually want to run it easily, without using the IDE. To that end, we'll discuss the configuration and pros/cons of using each of these approaches for creating the executable. A quick and practical guide to building and managing Java projects using Apache Maven. Where is the Maven Local Repository?
The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location. The plugin analyses the dependencies in your project, and copies dependencies from Maven Central (or the repository you have configured).
Use the maven-dependencies-plugin
to specify an output directory for the copy-dependencies
execution.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Update:
To let the jar know where to find the lib
folder, you can specify this as a Class-Path
value in the manifest
using the maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>foo.bar.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With