Getting Started with Maven | Developing and Using Your Own Library | Including Dependencies with maven-assembly-plugin
Including Dependency Libraries
When packaging, you can include dependency libraries in the Jar file. This can be done by configuring a plugin called maven-assembly-plugin. The settings for storing dependency libraries inside the Jar file are shown below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
About the <configuration> Tag
The <configuration> tag, which describes configuration information, provides a <descriptorRefs> tag and then a <descriptorRef> tag inside it. By specifying jar-with-dependencies here, you indicate that this is the configuration for creating a unified Jar.
After that, configure the required settings in <execution> under <executions>. The <phase> is set to package and the <goal> under <goals> is set to single. This tells Maven to bundle everything into one Jar file during packaging. The Jar file name will include -jar-with-dependencies, such as SampleMavenApp-1.0-jar-with-dependencies.jar.
Add this plugin setting to pom.xml, then run mvn package again to package the project. Then run the Jar file again with the java command.
$ java -classpath target\SampleMavenApp-1.0-jar-with-dependencies.ja com.devkuma.App
This time, the program should run correctly because MyLib is included in the Jar file. The generated Jar file will also run without problems if you copy it to another computer.
Complete pom.xml for SampleMavenApp
The complete build script for SampleMavenApp’s pom.xml is shown below.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devkuma</groupId>
<artifactId>SampleMavenApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SampleMavenApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.devkuma.mylib</groupId>
<artifactId>MyLib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.devkuma.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.devkuma.App</mainClass>
<addClasspath>true</addClasspath>
<addExtensions>true</addExtensions>
<packageName>com.devkuma</packageName>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
As you can see from this work, using your own library does not require anything special in the library itself. The only requirement is to install the library into the local repository with the mvn install command.
The main point is in the project that uses the library. First, adding the library with a <dependency> tag is required. With this, it can be run with exec:java.
Then add the exec-assembly-plugin configuration that bundles all dependency libraries into a single Jar. Since there are no project-specific settings here, you can copy and paste the tags shown here as-is.