Month: February 2016

How to copy maven dependencies

Posted on Updated on

Well, today topic is ‘How to copy maven dependencies’. This may not look so important to a regular user, but when you are working on a product which use lot of dependencies  and you need to hold it on a version control system, what we are going to talk now is going to be really useful.

But, how?

Suppose you are maintaining a code in a version control system. Your project involves quite lot of dependencies. General approach is including them in the pom file along with their version numbers, artifactId, and groupId. When someone take a pull or clone from your repository and build the project, maven will download all the required dependencies to a default target folder (~/.m2).

But, suppose you need these dependencies included in your classpath as it is required in javaagent. Then what you should do? How are you going to copy them? Are you going to copy all the required dependencies one by one? Should you ask the user to download and copy all of them? That’s not going to be a pleasant experience to whoever is going to use it. Specially if it contain lot of dependencies as it is mentioned earlier.

What if we can make maven to copy all the required jars to a specific folder given by us, while we build the project using mvn clean install? Then it will be just copying that one folder to wherever the location we want or setting the classpath to that folder. That is where the following code segment comes in handy.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
     <execution>
        <id>copy</id>
        <phase>package</phase>
        <goals>
           <goal>copy-dependencies</goal>
        </goals>
        <configuration>
           <outputDirectory>
           ${project.build.directory}/lib
           </outputDirectory>
           <overWriteReleases>false</overWriteReleases>
           <overWriteSnapshots>false</overWriteSnapshots>
           <overWriteIfNewer>true</overWriteIfNewer>
        </configuration>
      </execution>
    </executions>
</plugin>

The copy-dependencies option of maven-dependency-plugin allows you to copy dependencies listed in the pom to a given folder. Whenever you build the project, it’ll copy dependencies to the given location from the default folder. Therefore, changing version numbers or adding newer dependencies wouldn’t be much of a headache. All you have to do is update the pom to newer values and rebuild the project.

The <outputDirectory> specifies the location where you want to make the folder and copy the dependencies. Above example makes a folder called lib in your project’s  ${project.build.directory}, which is ‘target’ folder by . Well given above is just an initial introduction. You can find more details about attributes of maven copy-dependencies from here.

Well, now you know how to make maven to copy all your dependencies to a certain folder while you build the product.