Solution 1: Using maven-dependency-plugin
in the pom.xml
This solution adds a profile in pom.xml
to copy dependencies during a specific build phase, such as install
or package
. You can customize the profile ID and phase according to your requirements.
Example configuration:
<project>
...
<profiles>
<profile>
<id>qa</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase> <!-- Specify the build phase, e.g., install -->
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Solution 2: Direct Plugin Configuration in the Build Section
If you want this to run every time without specifying a profile, place the maven-dependency-plugin
directly in the build/plugins
section with the prepare-package
phase.
Example configuration:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Command to run:
mvn clean package
This approach automatically copies dependencies during the package
phase.
Solution 3: Command-Line Execution Without pom.xml
Changes
For a one-off dependency copy without modifying pom.xml
, use the command below to copy dependencies to the target/lib
folder directly from the command line.
Command to run:
mvn clean package dependency:copy-dependencies -DoutputDirectory=${project.build.directory}/lib
This command is useful if you only occasionally need to copy dependencies.
Solution 4: Creating an Uber JAR with Dependencies Included
If you want a single JAR file that includes both your code and all dependencies, use the maven-assembly-plugin
with the jar-with-dependencies
descriptor.
Example configuration:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>jar-with-dependencies</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Command to run:
mvn clean package
This will generate an “uber” JAR that includes all dependencies within target
as projectname-jar-with-dependencies.jar
.
Solution 5: WAR or EAR Packaging for Web Applications
If you’re building a web application and need dependencies in a WAR or EAR file, Maven can bundle dependencies automatically:
- Set
<packaging>war</packaging>
or<packaging>ear</packaging>
in yourpom.xml
. - Run the standard Maven
package
goal.
Maven will place dependencies in the appropriate directories inside the WAR or EAR file based on the structure expected by your application server.
Command to run:
mvn clean package
Summary Table
Scenario | Solution |
---|---|
Copy dependencies to a custom location permanently | Solution 1 or Solution 2 |
One-time dependency copy | Solution 3 |
Bundle dependencies in a single JAR file | Solution 4 |
Package dependencies in a WAR or EAR file for deployment | Solution 5 |
These methods provide flexibility based on your project’s packaging needs and build requirements.