Including dependencies in a jar with Maven

To include dependencies in a JAR file with Maven, you can use the maven-assembly-plugin to create an assembly that includes all the dependencies of your project.

First, add the maven-assembly-plugin to your pom.xml file:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <!-- Configure the assembly here -->
      </configuration>
    </plugin>
  </plugins>
</build>

Next, configure the assembly to include all the dependencies of your project. You can do this by adding a dependencySet element to the configuration of the maven-assembly-plugin.

For example, to include all the dependencies in a JAR file called project-with-dependencies.jar, you can use the following configuration:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <descriptors>
          <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
      </configuration>
    </plugin>
  </plugins>
</build>
<!-- src/main/assembly/assembly.xml -->
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>project</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Finally, run the assembly:single goal of the maven-assembly-plugin to create the JAR file with dependencies.

mvn assembly:single

This will create the JAR file project-with-dependencies.jar in the target directory, which includes all the dependencies of your project.

Note that the assembly:single goal creates an assembly for a single artifact, so it will only include the dependencies of the artifact that you specify. If you want to include the dependencies of multiple artifacts, you can use