I started to convert my project to maven because I needed to use a library that was distributed in binary form over maven only, but after banging my head against the wall on it for far too long I've decided to stop hurting myself and just use Ant. I'd like to just have maven download the jar and all of its transitive dependencies into a directory of my choosing so I can just check them into my SCM as I normally enjoy and be a blissful developer once again.
Any ideas how to do that easily?

- 49,761
- 33
- 66
- 176

- 37,646
- 24
- 106
- 138
-
2Consider using Ivy and get the transitive dependency management you need, with the Ant you trust. Or bite the bullet and use the Maven directory structure. – Dave Newton Oct 26 '11 at 19:57
-
Have you tried [Ivy](http://ant.apache.org/ivy/)? – Tomasz Nurkiewicz Oct 26 '11 at 19:57
-
5No, but I've spent all of my patience with switching at this point. Maybe next time. I want to get back to coding. Can it be done with maven? – chubbsondubs Oct 26 '11 at 20:11
-
Possible duplicate of [How to use Maven pom to download jar files only to a specific directory?](http://stackoverflow.com/questions/7742252/how-to-use-maven-pom-to-download-jar-files-only-to-a-specific-directory) – Vadzim Feb 14 '17 at 12:20
7 Answers
The maven dependency plugin can potentially solve your problem.
If you have a pom
with all your project dependencies specified, all you would need to do is run
mvn dependency:copy-dependencies
and you will find the target/dependencies
folder filled with all the dependencies, including transitive.
Adding Gustavo's answer from below: To download the dependency sources, you can use
mvn dependency:copy-dependencies -Dclassifier=sources
-
but what if I want to copy all dependencies to my local repo? This is it supposed to be used for? Why do copy if it maybe already be in local repo. It is local anyway. – ses Aug 09 '13 at 19:33
-
2@ses A standard maven build (e.g. compile, test, package, install, etc.; not sure about validate) already copies all dependencies to your local repo by default. This is not for that. Instead it's for situations where you need your app's dependencies for whatever reason. I'm using it right now to inspect the dependent libraries for redundant API definitions (e.g. some libraries will include Javax APIs, which can conflict with other versions of the same API), but it's also good if your app needs its dependencies packaged with it for distribution, or just whatever. – Spanky Quigman Aug 27 '13 at 14:44
-
@Raghuram Technically you end up with all (resolved) *artifacts* in "target/dependencies". Further, there is the potential problem of name collisions as the "groupId" coordinate of a dependency is ignored. In other words, "dependency:copy-dependencies" does not preserve the layout structure. – wh81752 Oct 25 '14 at 10:29
-
@whaefelinger. You just need to set [`useRepositoryLayout`](http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#useRepositoryLayout) to `true` – Raghuram Oct 27 '14 at 06:13
-
@Raghuram "dependency:copy-dependencies" works for plain jars. It copied "some" transitive dependencies; could not figure out why some were left out of the equation. Further, POMs and parent POMs are copied but then without preserving layout structure; same is true for copying over source jars. – wh81752 Oct 31 '14 at 12:56
-
The website tells you to add `
.... ` to your "pom.xml" file but that was ignored in my project while exporting, so not sure if you actually have to do that! You do have to "install" Maven though (technically: download the zip, unzip and add the "bin" folder to PATH) if you're using Eclipse because even though newer versions come with it, it's just a type of plugin, not the full "install" that you need to run the "mvn" commands in cmd! Then just run the command in the folder with the "pom.xml" file and the downloaded jars are going to be in its subfolder. – Neph Sep 04 '19 at 13:47 -
@Raghuram thanks for your answer. How can I download dependencies in maven folder structure way (e.g. org.springframework.spring-aop 5.1.2 release) should be downloaded something like target/dependencies/org/springframework/spring-aop/5.1.2.RELEASE/(actual_jar). This way when I share dependencies.zip to other member, he/she can install jars using maven install into mavenLocal repo and application can be build seamlessly. share your thoughts. – harshit2811 Dec 09 '20 at 12:29
I finally figured out a how to use Maven. From within Eclipse, create a new Maven project.
Download Maven, extract the archive, add the /bin
folder to path.
Validate install from command-line by running mvn -v
(will print version and java install path)
Change to the project root folder (where pom.xml
is located) and run:
mvn dependency:copy-dependencies
All jar-files are downloaded to /target/dependency
.
To set another output directory:
mvn dependency:copy-dependencies -DoutputDirectory="c:\temp"
Now it's possible to re-use this Maven-project for all dependency downloads by altering the pom.xml
Add jars to java project by build path -> configure build path -> libraries -> add JARs..

- 3,375
- 1
- 31
- 45
-
2Doesn't necessarily have to be an eclipse project. I opened a jar file in Java Decompiler and copied the pom.xml to an empty folder. I called `mvn dependency:copy-dependencies` as you described from that folder and got all the dependent jar files nicely copied to one single folder. – Bernhard Döbler Jun 19 '18 at 20:41
Based on @Raghuram answer, I find a tutorial on Copying project dependencies, Just:
Open your project
pom.xml
file and find this:<project> [...] <build> <plugins> ... </plugins> </build> [...] </project>
Than replace the
<plugins> ... </plugins>
with:<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins>
And call maven within the command line
mvn dependency:copy-dependencies
After it finishes, it will create the folder target/dependency
within all the jar
's dependencies on the current directory where the pom.xml
lives.

- 1
- 1

- 8,560
- 11
- 83
- 144
-
Where do I type the "mvn" command? Right click maven-project-> Maven -> .. ?? – Baked Inhalf Jan 17 '18 at 13:53
-
In a terminal command line on the folder where the `pom.xml` file is on. – Evandro Coan Jan 17 '18 at 14:48
-
1Didn't work. I think Maven is installed as a plugin only. I had to manually download Maven as well. Then `mvn dependency:copy-dependencies` worked! – Baked Inhalf Jan 17 '18 at 15:03
I found the next command
mvn dependency:copy-dependencies -Dclassifier=sources
here maven.apache.org

- 785
- 1
- 12
- 31
Please check if you have some config files in ${MAVEN_HOME}/conf
directory like settings.xml
.
Those files overrides settings from .m2
folder and because of that, repository folder from .m2
might not be visible or discarded.

- 19,198
- 2
- 29
- 30
My simple script based on user based on Raghuram.
getmvndep.sh
#!/bin/bash
groupId=$1
artifactId=$2
version=$3
echo "<project>" > pom.xml
echo " <modelVersion>4.0.0</modelVersion>" >> pom.xml
echo " <groupId>com.temp.temp</groupId>" >> pom.xml
echo " <artifactId>temp</artifactId>" >> pom.xml
echo " <packaging>jar</packaging>" >> pom.xml
echo " <version>0.0.0</version>" >> pom.xml
echo " <dependencies>" >> pom.xml
echo " <dependency>" >> pom.xml
echo " <groupId>${groupId}</groupId>" >> pom.xml
echo " <artifactId>${artifactId}</artifactId>" >> pom.xml
echo " <version>${version}</version>" >> pom.xml
echo " </dependency>" >> pom.xml
echo " </dependencies>" >> pom.xml
echo " <build>" >> pom.xml
echo " <plugins>" >> pom.xml
echo " </plugins>" >> pom.xml
echo " </build>" >> pom.xml
echo "</project>" >> pom.xml
mvn dependency:copy-dependencies -DoutputDirectory="./libs/${version}"
rm pom.xml

- 21
- 1
Based on Patrik Polak's answer, based on user, based on Raghuram here's the same script for windows users.
CD /D %~dp0
IF "%1" EQU "" goto:EOF
IF "%2" EQU "" goto:EOF
IF "%3" EQU "" goto:EOF
SET groupId=%1
SET artifactId=%2
SET version=%3
echo ^<project^> > pom.xml
echo ^<modelVersion^>4.0.0^</modelVersion^> >> pom.xml
echo ^<groupId^>com.temp.temp^</groupId^> >> pom.xml
echo ^<artifactId^>temp^</artifactId^> >> pom.xml
echo ^<packaging^>jar^</packaging^> >> pom.xml
echo ^<version^>0.0.0^</version^> >> pom.xml
echo ^<dependencies^> >> pom.xml
echo ^<dependency^> >> pom.xml
echo ^<groupId^>%groupId%^</groupId^> >> pom.xml
echo ^<artifactId^>%artifactId%^</artifactId^> >> pom.xml
echo ^<version^>%version%^</version^> >> pom.xml
echo ^</dependency^> >> pom.xml
echo ^</dependencies^> >> pom.xml
echo ^<build^> >> pom.xml
echo ^<plugins^> >> pom.xml
echo ^</plugins^> >> pom.xml
echo ^</build^> >> pom.xml
echo ^</project^> >> pom.xml
mkdir .\%groupId%-%artifactId%-%version%-jars
call mvn dependency:copy-dependencies -DoutputDirectory=.\%groupId%-%artifactId%-%version%-jars
del pom.xml
pause

- 1
- 1