Home > Java > javaTutorial > How Can I Package Project Dependencies into a Single JAR with Maven?

How Can I Package Project Dependencies into a Single JAR with Maven?

Susan Sarandon
Release: 2024-12-26 17:00:10
Original
941 people have browsed it

How Can I Package Project Dependencies into a Single JAR with Maven?

Including Dependencies in a Jar with Maven

When developing software, it's common to rely on libraries and frameworks developed by others. Maven, a popular build automation tool, manages project dependencies effectively. However, a developer may need to include these dependencies within the final distribution jar for various reasons.

To achieve this, the maven-assembly plugin comes to the rescue. By using the "jar-with-dependencies" descriptor, you can configure Maven to package all the dependencies into a single jar. Here's how you do it:

In your project's pom.xml, add the following configuration:

<build>
  <plugins>
    <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>
  </plugins>
</build>
Copy after login

By adding this configuration, you instruct Maven to create a jar file that includes the project's classes along with all the dependent class files. This ensures that your jar is self-contained and distributable without requiring additional dependency installation.

Remember that this approach doesn't literally embed other jars within your jar. Instead, it extracts the class files from the dependencies and packages them alongside your application's classes.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template