How can I create an executable/runnable JAR with dependencies using Maven?
How can I create an executable/runnable JAR with dependencies using Maven?
35521-Jul-2023
Home / DeveloperSection / Forums / How can I create an executable/runnable JAR with dependencies using Maven?
Aryan Kumar
22-Jul-2023To create an executable/runnable jar with dependencies using Maven, you can use the
maven-assembly-plugin
. Themaven-assembly-plugin
allows 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
mainClass
element 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-dependencies
descriptor ref tells themaven-assembly-plugin
to include all of the dependencies of your project in the JAR file.Once you have added the
maven-assembly-plugin
configuration 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.MyApplication
class, which is the main class of your project.