87

Is there a way to download dependencies from a pom.xml file to a specified folder in java? I'm able to run maven command from java and I got download messages, but I don't know where maven stores these libraries? How can I download these dependencies to a specific folder?

0

5 Answers 5

130

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

Add this to exclude the transitive or inner dependencies:

-DexcludeTransitive=true

2
  • 2
    I'm encountering a strange issue with this method, it downloaded a .pom file for the main dependency but for its subdependencies (the transitive dependencies) it downloaded the proper jars. Am I doing something wrong?
    – jmng
    Commented Jan 9, 2018 at 16:21
  • 1
    Note, on Windows, use mvn dependency:copy-dependencies "-DoutputDirectory=OUTPUT_DIR"
    – AndrWeisR
    Commented Jul 20, 2020 at 3:38
12

As explained here, you can use maven-dependency-plugin:get for this.

For example, if you want to download org.apache.hive:hive-common:2.1.1 in your local folder, execute this:

mvn dependency:get -Ddest=./ -Dartifact=org.apache.hive:hive-common:2.1.1

If you want to download the latest 3.0.0-SNAPSHOT:tar.gz version of com.orientechnologies:orientdb-community-gremlin from https://oss.sonatype.org/content/repositories/snapshots snapshots repository, execute this:

mvn dependency:get -Ddest=./ -DremoteRepositories=sonatype-nexus-snapshots::::https://oss.sonatype.org/content/repositories/snapshots -Dartifact=com.orientechnologies:orientdb-community-gremlin:3.0.0-SNAPSHOT:tar.gz
2
  • You can use -Dartifact=org.apache.hive:hive-common:LASTEST to get the latest version.
    – Matthieu
    Commented Sep 2, 2019 at 10:28
  • 3
    [WARNING] destination/dest parameter is deprecated: it will disappear in future version. Commented Oct 30, 2020 at 11:53
7

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 perform the copy. Combine this with the assembly plugin and you can package everything into a self contained archive for distribution.

1
  • I changed ${project.build.directory} to main\java\resources\libs to build my local library. Thanks! Note: here my path is relative to the usr.dir (project root), but it can also be absolute, like C:\Mydir. Great!
    – WesternGun
    Commented Jul 21, 2017 at 9:05
1

Maven stores all of these in it's local Maven2 repository. By default, it will store them in your user home directory under a directory called repository.

You can use the maven-dependency-plugin's goal called copy to take all of your project's dependencies and put them in a folder.

http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html

1
  • 2
    Is there any other way in java to copy dependencies as I don't write the pom.xml I just run it?
    – Feras Odeh
    Commented Oct 12, 2011 at 15:17
0
  1. Go to this site: http://jar-download.com/online-maven-download-tool.php

  2. Insert the Maven dependencies XML

  3. Download the jar files as a ZIP.

2
  • 13
    I'd refrain from downloading .jars through a third (fourth?) party site, who knows what else they add or how they modify to the archives (malware)? sblundy gave a more straight forward answer. Commented May 16, 2016 at 6:18
  • Site is down and using external APIs Commented Jul 27, 2021 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.