Home >Java >javaTutorial >How to Package Dependencies into a Single JAR File with Maven?

How to Package Dependencies into a Single JAR File with Maven?

Barbara Streisand
Barbara StreisandOriginal
2024-12-22 05:15:11951browse

How to Package Dependencies into a Single JAR File with Maven?

Packaging Dependencies within a JAR using Maven

Problem:

How to incorporate all dependencies into a solitary JAR file using Maven?

Background:

A project is designed to build into a single JAR file. However, there is a need to include class files from the dependencies within the JAR as well.

Solution:

Maven's maven-assembly-plugin provides a solution for this requirement. It contains a descriptor named "jar-with-dependencies" that enables the following:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

This configuration unpacks JARs specified as dependencies and integrates the class files into the main JAR. It alleviates the limitations of nesting JAR files and ensures that all necessary code is bundled within the single distributable JAR file.

The above is the detailed content of How to Package Dependencies into a Single JAR File with Maven?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn