How can I create an executable/runnable JAR with dependencies using Maven?
To create an executable JAR with dependencies using Maven, you can use the maven-assembly-plugin. This plugin allows you to package your project and its dependencies into a single JAR file.
To create an executable JAR with dependencies using Maven, you can use the maven-assembly-plugin. This plugin allows you to package your project and its dependencies into a single JAR file.
To use the maven-assembly-plugin, you will need to add it to your pom.xml file and configure it to create an executable JAR. Here's an example of how to do this:
---
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>---
This configuration creates an executable JAR file with the name ${project.artifactId}-${project.version}-jar-with-dependencies.jar that includes all of the project's dependencies. The mainClass element specifies the fully qualified name of the main class that should be used to execute the JAR file.
To create the executable JAR, run the following command:
---
mvn package---
This will create the JAR file in the target directory. You can then run the JAR file using the java -jar command, like this:
---
java -jar target/your-app-1.0.0-jar-with-dependencies.jar---
Note that this configuration includes all compile and runtime dependencies in the JAR file. If you need more control over dependency filtering or shading, consider using the maven-shade-plugin instead.