简体   繁体   English

使用Maven将依赖项下载到命令行上的目录

[英]Using Maven to download dependencies to a directory on the command line

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. 我需要将项目的所有传递依赖项下载到命令行上的目录, 而不需要pom.xml文件或其他脚本。 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. 据我所知,这至少是mvn的两步过程。

  1. Download dependencies to the local repository 将依赖项下载到本地存储库
  2. Copy the dependencies to the lib directory 将依赖项复制到lib目录

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. 不幸的是,dest param并没有帮助我,因为它不会将所有传递依赖项放在dest中。

So now I need to copy that JAR file and all of its transitive dependencies into my lib directory. 所以现在我需要将该JAR文件及其所有传递依赖项复制到我的lib目录中。 I know this part has been asked many time on StackOverflow but nothing has worked for my yet. 我知道这部分已经在StackOverflow上被问了很多次,但是我的工作没有任何效果。 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. 从阅读StackOverflow上的文档和其他答案的副本依赖和复制我认为我可以从命令行使用它们没有pom.xml但mvn似乎需要一个。 My Maven version is Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600). 我的Maven版本是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? 任何人都可以给我一个使用没有pom.xml的mvn复制传递依赖的例子吗?

Is there a better way to do what I'm trying accomplish here? 有没有更好的方法来做我在这里尝试完成的事情?

Apache ivy can be run as a standalone jar to download Maven dependencies. Apache ivy可以作为独立jar运行,以下载Maven依赖项。 No POM required. 不需要POM。

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

The first command almost gets you what you need - the POM of the dependency in question. 第一个命令几乎可以获得您所需要的东西 - 相关依赖项的POM。 Once you have that, you should not need a further project POM to run copy:dependencies : 一旦你有了这个,你就不需要另外一个项目POM来运行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, eg by adding -DexcludeTypes=test-jar to filter out the test JARs. 正如Everett Toews所指出的,您可以使用copy:dependencies其他选项copy:dependencies来进一步细化下载的内容,例如通过添加-DexcludeTypes=test-jar来过滤掉测试JAR。

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. 这样做的简单方法是创建一个简单的 pom.xml是的,请创建pom ),它将创建WAR文件并依赖于您的工件。 After mvn clean package your will find all dependencies artifacts (include transitive) in the lib folder of the WAR. mvn clean package您将在WAR的lib文件夹中找到所有依赖项工件(包括传递)。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM