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
Linda HamiltonOriginal
2024-12-22 15:43:18310browse

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>

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:
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