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