How can I create an executable/runnable JAR with dependencies using Maven?
How can I create an executable/runnable JAR with dependencies using Maven?
632
21-Jul-2023
Aryan Kumar
22-Jul-2023To create an executable/runnable jar with dependencies using Maven, you can use the
maven-assembly-plugin. Themaven-assembly-pluginallows you to package your project and its dependencies into a single JAR file.To use the
maven-assembly-plugin, you need to add the following dependency to your pom.xml file:You also need to add the following configuration to your pom.xml file:
The
mainClasselement specifies the class that should be executed when the JAR file is run. In this case, the main class iscom.example.MyApplication.The
jar-with-dependenciesdescriptor ref tells themaven-assembly-pluginto include all of the dependencies of your project in the JAR file.Once you have added the
maven-assembly-pluginconfiguration to your pom.xml file, you can build your project by running the following command:" mvn assembly:single "
This will create a JAR file in the
target/directory of your project. The JAR file will be namedproject-name-jar-with-dependencies.jar.You can then run the JAR file by executing the following command:
" java -jar project-name-jar-with-dependencies.jar "
This will run the
com.example.MyApplicationclass, which is the main class of your project.