How to package third-party JAR files with Maven?

There are several ways to package third-party JAR files:

  1. operating system
<dependency>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/example.jar</systemPath>
</dependency>
  1. You can install a third-party jar file into the local repository using Maven plugins, and then simply import that dependency in the pom.xml file as usual.
mvn install:install-file -Dfile=path/to/example.jar -DgroupId=com.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar
<dependency>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
</dependency>
  1. Use the Maven assembly plugin to package the project and third-party jar files together into an executable jar file.

Configure the assembly plugin in the pom.xml file.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.example.MainClass</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>

Simply execute the command “mvn clean package” to generate an executable JAR file with third-party JARs included.

bannerAds