28

I need to download all transitive dependencies of a project to a directory on the command line without having a pom.xml file or other script. Ideally I would be able to do this with one or two commands. From what I can tell, this is at least a 2 step process with mvn.

  1. Download dependencies to the local repository
  2. Copy the dependencies to the lib directory

To get the dependencies I run

$ mvn org.apache.maven.plugins:maven-dependency-plugin:2.6:get -DgroupId=org.jclouds.provider -DartifactId=rackspace-cloudservers-us -Dversion=1.5.8

Which works great. Unfortunately the dest param doesn't help me as it won't put all transitive dependencies in the dest.

So now I need to copy that JAR file and all of its transitive dependencies into my lib directory. I know this part has been asked many time on StackOverflow but nothing has worked for my yet. I've tried the following.

$ mvn dependency:copy-dependencies ...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.1:copy-dependencies (default-cli): Goal requires a project to execute but there is no POM in this directory

and

$ mvn dependency:copy ...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.1:copy (default-cli): Goal requires a project to execute but there is no POM in this directory

From reading the documentation and other answers here on StackOverflow for copy-dependencies and copy I thought I would be able to use them from the command line without a pom.xml but mvn seems to need one. My Maven version is Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600).

Can anyone give me an example of copying transitive dependencies using mvn without a pom.xml?

Is there a better way to do what I'm trying accomplish here?

Everett Toews
  • 10,337
  • 10
  • 44
  • 45
  • There is a nice antrun plugin , can you try it ? – Shmil The Cat Mar 16 '13 at 14:52
  • I assume you mean http://maven.apache.org/plugins/maven-antrun-plugin/ How would it be used to do what I'm trying to accomplish? – Everett Toews Mar 16 '13 at 15:55
  • I'm using the _run_ goal of the antrun plugin for copying and moving directories , so maybe you could gather all your files (jars, cfg files etc.) to some folder and from there transferring them ? or simply using cp/xcopy ? – Shmil The Cat Mar 16 '13 at 16:59
  • That wouldn't cover the transitive dependencies automatically. I'm really looking for a way to, ideally, do this with a single command or at most two. What you're suggesting would have me writing a script which is exactly what I'm trying to avoid. Thanks for clarifying this for me. I'll update the question. – Everett Toews Mar 16 '13 at 19:22

3 Answers3

37

Apache ivy can be run as a standalone jar to download Maven dependencies. No POM required.

curl -L -O http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar
java -jar ivy-2.3.0.jar -dependency org.jclouds.provider rackspace-cloudservers-us 1.5.8 -retrieve "lib/[artifact]-[revision](-[classifier]).[ext]"

Produces the following files:

├── ivy-2.3.0.jar
└── lib
    ├── aopalliance-1.0.jar
    ├── asm-3.1.jar
    ├── bcprov-jdk16-1.46.jar
    ├── cglib-2.2.1-v20090111.jar
    ├── clojure-1.3.0.jar
    ├── core.incubator-0.1.0.jar
    ├── gson-2.2.jar
    ├── guava-13.0.jar
    ├── guice-3.0.jar
    ├── guice-assistedinject-3.0.jar
    ├── javax.inject-1.jar
    ├── jclouds-compute-1.5.8.jar
    ├── jclouds-core-1.5.8.jar
    ├── jclouds-scriptbuilder-1.5.8.jar
    ├── jsr250-api-1.0.jar
    ├── jsr311-api-1.1.1.jar
    ├── openstack-keystone-1.5.8.jar
    ├── openstack-nova-1.5.8.jar
    ├── rackspace-cloudidentity-1.5.8.jar
    ├── rackspace-cloudservers-us-1.5.8.jar
    ├── rackspace-cloudservers-us-1.5.8-javadoc.jar
    ├── rackspace-cloudservers-us-1.5.8-sources.jar
    ├── rocoto-6.1.jar
    └── tools.logging-0.2.3.jar
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • This is a great answer and works very well but if possible I'd like to stick with Maven only. Thanks! – Everett Toews Mar 18 '13 at 19:36
  • 1
    @JanusTroelsen Use the "-settings" option to pass an ivy settings file pointing at your repo. For an example settings file see: http://stackoverflow.com/questions/4836011/use-public-maven-repository-with-ivy/ – Mark O'Connor Nov 28 '14 at 20:52
  • works for me. downloaded socket.io java client command java -jar ivy-2.3.0.jar -dependency io.socket socket.io-client 0.6.2 -retrieve "lib/[artifact]-[revision](-[classifier]).[ext]" – Iftikar Urrhman Khan Oct 20 '15 at 10:08
  • Or, if you know you need only a single jar, with no dependencies, the same `curl` and URL pattern can get that single jar. Or, for Dockerfile, the `ADD` command. – dbreaux Feb 23 '19 at 00:06
  • @dbreaux The question asked for a mechanism to download the dependencies. – Mark O'Connor Feb 23 '19 at 00:26
  • 1
    Understood. Just noting an additional use for the curl technique and URL, in case anybody else finds this answer while searching for that need. Like I did :-) – dbreaux Feb 23 '19 at 04:08
  • Works well but doesn't seem to download the sources transitively, even if you specify e.g `-types jar source javadoc` – amynbe Oct 24 '19 at 21:46
28

The first command almost gets you what you need - the POM of the dependency in question. Once you have that, you should not need a further project POM to run copy:dependencies:

Here's an example:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.7:get -DgroupId=org.jclouds.provider -DartifactId=rackspace-cloudservers-us -Dversion=1.5.8 -Dtype=pom

mvn org.apache.maven.plugins:maven-dependency-plugin:2.7:copy-dependencies -f /path/to/m2/repo/org/jclouds/provider/rackspace-cloudservers-us/1.5.8/rackspace-cloudservers-us-1.5.8.pom -DoutputDirectory=/path/to/target/dir/don't/use/.

As pointed out by Everett Toews, you can use additional options of copy:dependencies to further refine what's downloaded, e.g. by adding -DexcludeTypes=test-jar to filter out the test JARs.

Community
  • 1
  • 1
Andrew Phillips
  • 1,050
  • 9
  • 16
  • The key point is that you have to specify the version of the dependency plugin. Older versions require a pom. – Evan Apr 06 '20 at 18:42
3

According to my understanding you want to download all dependencies artifacts to one folder on your local computer (without search your local repository). The simple way to do it is to create a simple pom.xml (yes, please create the pom) that will create the WAR file and will depend on your artifact. After mvn clean package your will find all dependencies artifacts (include transitive) in the lib folder of the WAR.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>testwar</groupId>
    <artifactId>examplewar</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>examplewar Maven Webapp</name>
    <dependencies>
        <dependency>
            <groupId>org.jclouds.provider</groupId>
            <artifactId>rackspace-cloudservers-us</artifactId>
            <version>1.5.8</version>
        </dependency>
    </dependencies>

</project>
Verhagen
  • 3,885
  • 26
  • 36
Michael
  • 10,063
  • 18
  • 65
  • 104