I want to package it not in a single executable jar for distribution. I need an executable to be something like main.jar and all dependencies to be in libs/*.jar
How can I make maven executable jar without preincluded into it dependencies libraries?
In How can I create an executable JAR with dependencies using Maven? there is a note by answered Dec 1 '10 at 10:46 André Aronsen, but that one simply doesn't work (failed s.a.descriptorRef is not set).
In Eclipse you can do it simply as follows : Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.
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.
4. mvn package. This command builds the maven project and packages them into a JAR, WAR, etc.
I prefer this a bit modified solution. Create Your executable jar with classpath set and copy all dependencies to given directory.
You don't need any additional files.
Dependencies are being copied during install phase.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!-- optional -->
<!-- exclude copying test and provided dependencies -->
<includeScope>runtime</includeScope>
<excludeScope>provided</excludeScope>
<!-- optional -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I updated the answer to copy only runtime dependencies as described here - copying the right dependencies. Test and provided dependencies are excluded.
It turns out that if you want to exclude dependencies from both the test and provided scopes, you need to exclude the provided scope and include the runtime scope.
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