Maven is an automation tool for building Java projects. Maven uses POM.xml
to build a project in which project dependencies and other specifications are mentioned.
We may need to create an executable JAR for our Maven project for deployment purposes. The Maven assembly plugin makes this easy.
The Maven assembly plugin helps us create a distributable archive according to the file descriptor defined in the configuration. There are some predefined file descriptors, which are src
, bin
, jar-with-dependencies
, and project
, and we can also define our file descriptor. The file descriptor jar-with-dependencies
creates a single archive or JAR containing a binary of the project and its dependencies.
We’ve to add the following plugin description in the build
tag in the POM.xml
of the project:
<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><mainClass>com.mycompany.app.App</mainClass></manifest></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins>
Line 7: We define the file descriptor jar-with-dependencies
.
Line 10: We define the full qualified name of the main class from which JAR will get executed.
Now, we can create JAR with dependencies by using the following command:
mvn package
This will package all the dependencies inside the jar. If we specify the dependencies in the configurations
tag, then only those dependencies will be added to the produced JAR.
Lines 17–22: Note that to create a jar with dependencies, here we mention in the exections
tag, that the package should be single. Instead of mentioning it in the executions
tag, we can also mention it while compiling the jar like this:
mvn clean compile assembly:single
After this, a jar file with the name {project_name}_{version_number}_jar_with_dependencies
should be generated.
Try executing this jar using the following command in the target directory:
target/java -jar name_of_jar.jar
Note: Please specify the of name of actual jar after
-jar
.
Let's see an example of all of this in action. This executable gets all the data from a URL and displays it to the terminal. We have expanded on the POM.xml
file above to make an executable example.
Maven-Jar-Plugin: This plugin is included to create an executable JAR file that can be run directly. It specifies the main class (com.educative.App
) in its manifest for execution.
Maven-Assembly-Plugin: This plugin is added to package all project dependencies into a single executable JAR file. The jar-with-dependencies
descriptorRef
ensures this behavior.
Dependencies: The example shows the addition of an Apache HttpClient
dependency, demonstrating how to include necessary libraries for your project.
H4sIAH1y8GQAA+1WS2wbRRjehD4tUByBqIR4jNyoB6R9edf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EI23TUJU3kLisQopHZkooVnQhCGtg2ajWgE1N/RrwAwDGyxzGTVMQQM7gISWB3yP0FqAyGm7qyuypWiAK4Cu8hIOrGIfhC7/st2cSPDaeHNPpo2g2zG3Ni2aUDRRwEs1Vp0hAsQqiHITrS+Kp0PM2yIg1pIUZ0UidZCqU+qTUVlmNInNOdCVsJfqhGf7rAPRBKmoI8k9/k8AmSVLdkOWZObDchKYCLwux7HkCnblVlfELqbchdOspfbRnIINCFRDUiRDrAUQ2l2LBw7fx9Ylo2vwbG4hV54oFvJA5kUxkV9lxXbwWntZE7oW5nkhMo8vs6MIWCEb+c7kShNgqB2BSxwd4sZEInGYAUzGygqwgdzNC7b1cnVfLCeiRaELWiwqJIFseya0ZehDs47EiLRlIGpSVtJiNzBUSLfVtGy5hfx0uThTmgMpcdEh6UyWJWPRaWbUtJPq2khB21TPlaKm7wUUxNG42rale9AJ3jHGKWC406/DLnaxi7sZfwMfqN0XABQAAA==
Line 26: Creates a ClosableHttpClient
instance with createDefault()
method.
Line 28: Creates a HTTPGet
request with the get request URL.
Line 30: Executes the get request.
Line 34: Prints the response code received from the server.
Line 36: Prints the response of get request using EntityUtils
.
The Maven Assembly Plugin provides a flexible and powerful way to create distributable archives for Maven projects. By defining custom assembly descriptors, developers can package their project's artifacts, dependencies, and other files into a single archive for easy deployment. The plugin's support for various archive formats, including JAR, ZIP, and TAR, makes it a versatile tool for managing the distribution of Java applications built with Maven.