Home > Java > javaTutorial > How to Create a Self-Contained JAR File with Maven and its Dependencies?

How to Create a Self-Contained JAR File with Maven and its Dependencies?

Linda Hamilton
Release: 2024-12-22 15:43:18
Original
315 people have browsed it

How to Create a Self-Contained JAR File with Maven and its Dependencies?

Creating a Jar File with Maven and Dependencies

When constructing a project that generates a standalone jar file, the inclusion of external dependencies is crucial. Yet, Maven doesn't readily provide this functionality, leaving developers searching for solutions.

Fortunately, the maven-assembly plugin offers an elegant solution with its "jar-with-dependencies" descriptor. This extension empowers developers to package dependencies along with the project's classes within a single jar file. To achieve this:

<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 incorporating this configuration, Maven generates a comprehensive jar file that encapsulates both the project's code and its required dependencies, making deployment and distribution seamless.

The above is the detailed content of How to Create a Self-Contained JAR File with Maven and its Dependencies?. For more information, please follow other related articles on the PHP Chinese website!

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