Can Maven collect all the dependent JARs for a project to help with application deployment? Can Maven collect all the dependent JARs for a project to help with application deployment? java java

Can Maven collect all the dependent JARs for a project to help with application deployment?


What you want to investigate is Maven's dependency plugin. Add something similar to the following to pom.xml:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-dependency-plugin</artifactId>    <configuration>        <outputDirectory>            ${project.build.directory}        </outputDirectory>    </configuration></plugin>

Then run mvn clean dependency:copy-dependencies to copy perform the copy. Combine this with the assembly plugin and you can package everything into a self contained archive for distribution.


I did not care for the Shade plugin since it rolls all the packages from all the jars together.

To include all of the external libs you can use the Dependency Plugin as mentioned above.

This example will create a "lib" directory under "target/classes" before the "package" phase.

<plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-dependency-plugin</artifactId>  <version>2.6</version>  <executions>    <execution>      <id>copy-dependencies</id>      <phase>prepare-package</phase>      <goals>        <goal>copy-dependencies</goal>      </goals>      <configuration>        <outputDirectory>target/classes/lib</outputDirectory>        <overWriteIfNewer>true</overWriteIfNewer>        <excludeGroupIds>          junit,org.hamcrest,org.mockito,org.powermock,${project.groupId}        </excludeGroupIds>      </configuration>    </execution>    <execution>      <phase>generate-sources</phase>      <goals>        <goal>sources</goal>      </goals>    </execution>  </executions>  <configuration>    <verbose>true</verbose>    <detail>true</detail>    <outputDirectory>${project.build.directory}</outputDirectory>  </configuration></plugin>


Take a look at maven's dependency plugin, specifically the copy-dependencies goal. The usage section describes how to do exactly what you want.

To do it from the command line just do:

$ mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR