Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an executable JAR with dependencies using Maven?

I want to package my project in a single executable JAR for distribution.

How can I make a Maven project package all dependency JARs into my output JAR?

like image 998
soemirno Avatar asked Feb 22 '09 08:02

soemirno


People also ask

How do I create an executable jar of Maven project?

First, we specify the goal copy-dependencies, which tells Maven to copy these dependencies into the specified outputDirectory. In our case, we'll create a folder named libs inside the project build directory (which is usually the target folder). The most important part of this is the manifest configuration.

How do I create an executable jar file?

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.


2 Answers

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

and you run it with

mvn clean compile assembly:single 

Compile goal should be added before assembly:single or otherwise the code on your own project is not included.

See more details in comments.


Commonly this goal is tied to a build phase to execute automatically. This ensures the JAR is built when executing mvn install or performing a deployment/release.

<build>   <plugins>     <plugin>       <artifactId>maven-assembly-plugin</artifactId>       <configuration>         <archive>           <manifest>             <mainClass>fully.qualified.MainClass</mainClass>           </manifest>         </archive>         <descriptorRefs>           <descriptorRef>jar-with-dependencies</descriptorRef>         </descriptorRefs>       </configuration>       <executions>         <execution>           <id>make-assembly</id> <!-- this is used for inheritance merges -->           <phase>package</phase> <!-- bind to the packaging phase -->           <goals>             <goal>single</goal>           </goals>         </execution>       </executions>     </plugin>   </plugins> </build> 
like image 192
8 revs, 8 users 31% Avatar answered Sep 21 '22 00:09

8 revs, 8 users 31%


You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-dependency-plugin</artifactId>     <executions>         <execution>             <id>copy-dependencies</id>             <phase>prepare-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> <plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-jar-plugin</artifactId>     <configuration>         <archive>             <manifest>                 <addClasspath>true</addClasspath>                 <classpathPrefix>lib/</classpathPrefix>                 <mainClass>theMainClass</mainClass>             </manifest>         </archive>     </configuration> </plugin> 

Alternatively use ${project.build.directory}/classes/lib as OutputDirectory to integrate all jar-files into the main jar, but then you will need to add custom classloading code to load the jars.

like image 34
André Aronsen Avatar answered Sep 20 '22 00:09

André Aronsen