Reading time ∼2 mins

Copying dependency files with Maven

Sometimes, when I build an application with Maven, I need to move some dependency files to the target directory along with the application JAR. These files may not be used by the application directly and hence wouldn't be packaged in the application JAR.

For example, sometimes I'm interested in attaching the Hotswap Agent directly to my Java process through the -javaagent flag while it runs in a container. Many times, those containers are pretty minimal and don't have curl or wget preinstalled to download such dependencies directly. In the case of a multi-stage Dockerfile where one stage builds the application, I can provide instructions to Maven to copy this dependency in the target folder while packaging the application and use it in the later stages.

That's where the maven-dependency-plugin comes in handy. I need to add HotSwap Agent as a dependency and configure the plugin to copy it to the target directory (defined by the property) as follows.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""

    <relativePath/> <!-- lookup parent from repository -->




    <dependency>      <groupId>org.hotswapagent</groupId>      <artifactId>hotswap-agent-core</artifactId>      <version>1.4.1</version>    </dependency>  </dependencies>

      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-dependency-plugin</artifactId>        <executions>          <execution>            <id>copy</id>            <phase>prepare-package</phase>            <goals>              <goal>copy</goal>            </goals>            <configuration>              <artifactItems>                <artifactItem>                  <groupId>org.hotswapagent</groupId>                  <artifactId>hotswap-agent-core</artifactId>                  <outputDirectory>${}</outputDirectory>                </artifactItem>              </artifactItems>            </configuration>          </execution>        </executions>      </plugin>    </plugins>


The above configuration hooks an execution under the prepare-package phase and copies the hotswap-agent-core JAR available on the classpath (that's why it needs to be declared as a dependency).