Sunday, March 8, 2015

Maven, create an executable jar with all dependencies

You want to package your java application, i.e a console application, into a single jar that you can easily run from command line. Assume you have used Maven as dependency manager, here is the needed configuration.




In your pom.xml add the following plugin:

<build>
   <plugins>
      <plugin>
         <artifactid>maven-assembly-plugin</artifactid>
         <configuration>
            <archive>
               <manifest>
                  <mainclass>com.mytroubleshootings.MainClass</mainclass>
               </manifest>
            </archive>
            <descriptorrefs>
               <descriptorref>jar-with-dependencies</descriptorref>
            </descriptorrefs>
         </configuration>
      </plugin>
   </plugins>
</build>

The specified com.mytroubleshootings.MainClass is the class that contains the public static void main method, replace with yours.

Now you can create your jar from command line, be sure "mvn" command is reachable:

mvn clean compile assembly:single

Browse the directory "target", here will be your jar, i.e. myproject-jar-with-dependencies.jar, you can now execute it running:

java -jar myproject-jar-with-dependencies.jar


No comments:

Post a Comment

(c) Copyright 2020 - MyTroubleshooting.com