Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a runnable jar with Maven 2

I'm relatively new to the Maven mantra, but I'm trying to build a command-line runnable jar with Maven. I've setup my dependencies, but when I run mvn install and attempt to run the jar, two things happen. First, no main class is found, which is correctable. When I've corrected this, I get errors on run stating that classes cannot be found.

Maven is not packaging my dependency libraries inside of the jar, so I am unable to run the jar as a stand-alone application. How do I correct this?

like image 581
Stefan Kendall Avatar asked Jan 07 '10 16:01

Stefan Kendall


People also ask

Can I add jars to Maven 2 build classpath without installing them?

Maven install plugin has command line usage to install a jar into the local repository, POM is optional but you will have to specify the GroupId, ArtifactId, Version and Packaging (all the POM stuff). -1, sometimes you just want to add a jar file without the trouble of installing it.

How do I create a runnable JAR file?

To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next. On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.


1 Answers

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 
like image 78
Pascal Thivent Avatar answered Sep 20 '22 04:09

Pascal Thivent