如何在一个命令行中下载特定的Maven工件?

Xiè*_*léi 153 maven-2

我可以通过安装工件install:install-file,但是如何下载工件?

例如:

mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 169

您可以使用maven依赖插件,它dependency:get自2.1版以来有一个很好的目标.不需要pom,一切都在命令行上发生.

为了确保找到dependency:get目标,您需要明确告诉maven使用版本2.1,即您需要使用插件的完全限定名称,包括版本:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
    -DrepoUrl=url \
    -Dartifact=groupId:artifactId:version
Run Code Online (Sandbox Code Playgroud)

更新:使用旧版本的Maven(2.1之前的版本),可以dependency:get通过强制您的maven副本使用给定版本的插件来正常运行(不使用完全限定的名称和版本).

这可以按如下方式完成:

1.在文件的<settings>元素中添加以下行~/.m2/settings.xml:

<usePluginRegistry>true</usePluginRegistry>
Run Code Online (Sandbox Code Playgroud)

2. ~/.m2/plugin-registry.xml使用以下内容添加文件:

<?xml version="1.0" encoding="UTF-8"?>
<pluginRegistry xsi:schemaLocation="http://maven.apache.org/PLUGIN_REGISTRY/1.0.0 http://maven.apache.org/xsd/plugin-registry-1.0.0.xsd"
xmlns="http://maven.apache.org/PLUGIN_REGISTRY/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <useVersion>2.1</useVersion>
      <rejectedVersions/>
    </plugin>
  </plugins>
</pluginRegistry>
Run Code Online (Sandbox Code Playgroud)

但这似乎不再适用于maven 2.1/2.2.实际上,根据插件注册表简介,plugin-registry.xml已经重新设计了(为了便携性),插件注册表目前在Maven 2中处于半休眠状态.所以我认为我们现在必须使用长名称(当使用没有pom的插件时,这是背后的想法dependency:get).

  • 使用较新版本的 Maven(例如 3.6.0),您仍然可以只使用 `mvn dependency:get -Dartifact=group-id:artefact-id:version` (4认同)
  • 发现它,只需将`:jar:sources`附加到**工件**,请参阅http://stackoverflow.com/a/31109185/537554 (3认同)
  • 这"取"的目标似乎"只是工作"了(没有额外的配置/黑客需要;插件2.5.1,3.0.4 MVN):=例如=> MVN org.apache.maven.plugins:Maven的依赖关系的插件: 2.5.1:get -DremoteRepositories = http://repo.maven.apache.org -Dartifact = org.apache.ant:ant:1.8.1-Ddest = ant-1.8.1.jar (2认同)

Dan*_*nga 82

使用Maven Dependency Plugin的最新版本(2.8),从Maven Central Repository下载工件非常简单:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]
Run Code Online (Sandbox Code Playgroud)

其中groupId:artifactId:version,等等是Maven坐标

一个例子,使用Maven 2.0.9,Maven 2.2.1和Maven 3.0.4进行测试:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.hibernate:hibernate-entitymanager:3.4.0.GA:jar:sources
Run Code Online (Sandbox Code Playgroud)

(感谢Pascal Thivent首先提供他的精彩答案.我正在添加另一个答案,因为它不适合评论,而且对于编辑来说太广泛了.)


ver*_*guy 37

以下是最适合我使用Maven 3.1.1下载最新版本的"component.jar"工件的原因(其他建议没有,主要是因为maven版本更改我认为)

这实际上是下载文件并将其复制到本地工作目录中

来自bash:

mvn dependency:get \
    -DrepoUrl=http://.../ \
        -Dartifact=com.foo.something:component:LATEST:jar \
        -Dtransitive=false \
        -Ddest=component.jar \
Run Code Online (Sandbox Code Playgroud)

  • `dest`参数[已弃用](http://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html#destination),可以替换为[`copy`]的调用(http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html)在`get`之后:`mvn dependency:copy -Dartifact = com.foo.something:component:LATEST: jar -DoutputDirectory = .`(在这种情况下将jar放在工作目录中). (9认同)
  • @Athlan No-pom 支持可能是一个较新的功能,我想。现在我可以只运行 `mvn dependency:copy`(先不用 `dependency:get`)。 (5认同)

rye*_*nus 20

关于如何获取神器二进制文件,Pascal Thivent的答案就是它,但是为了得到神器源jar,我们可以使用:

mvn dependency:get -Dartifact=groupId:artifactId:version:jar:sources
Run Code Online (Sandbox Code Playgroud)

例如

mvn dependency:get -Dartifact=junit:junit:4.12:jar:sources
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为artifact参数实际上由groupId:artifactId:version[:packaging][:classifier].就在包装分类是可选的.

随着jar作为包装sources作为分类,Maven的依赖插件的理解,我们现在需要的来源罐子,而不是神器罐子.

不幸的是,目前来源jar文件无法传递下载,这确实有意义,但理想情况下我相信它也可以downloadSources像maven eclipse插件一样尊重选项.


alo*_*hoo 10

可以使用依赖:copy(http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html),它获取插件配置部分中定义的工件列表并将它们复制到指定位置,如果需要,重命名或剥离版本.如果远程存储库中的工件不存在于本地存储库或反应器中,则此目标可以解析这些工件.

并非所有插件的属性都可以在maven CLI中使用.可以指定具有"User Property:"属性的属性.在下面的例子中,我将junit下载到我的临时文件夹并从jar文件中删除vesion.

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=junit:junit:4.11 -DoutputDirectory=/tmp -Dmdep.stripVersion=true
Run Code Online (Sandbox Code Playgroud)

其中artifact = junit:junit:4.11是maven坐标.并且您将artifcat指定为groupId:artifactId:version [:packaging [:classifier]]

(感谢Pascal Thivent首先提供他的/sf/answers/1304301351/.我正在添加另一个答案)


小智 9

一个内衬下载最新的maven工件没有mvn:

curl -O -J -L  "https://repository.sonatype.org/service/local/artifact/maven/content?r=central-proxy&g=io.staticcdn.sdk&a=staticcdn-sdk-standalone-optimizer&e=zip&v=LATEST"
Run Code Online (Sandbox Code Playgroud)

  • 不赞成投票。该解决方案似乎仅适用于特定的存储库产品。 (2认同)
  • 适用于 Nexus 2.x,不适用于 3.x(尚) (2认同)

小智 7

maven命令: 如果您使用maven,则可以使用dependency:copy将工件下载到本地文件夹。

mvn dependency:copy -Dartifact=groupId:artifactId:version[:packaging[:classifier]] -DoutputDirectory=<your local path>. -U

Run Code Online (Sandbox Code Playgroud)

参考:https://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html

卷曲命令:

# guide: https://help.sonatype.com/repomanager3/rest-and-integration-api/search-api

# https://msnexus.xxxx.com/service/rest/v1/search/assets?sort=version&repository=public&maven.groupId=<groupId>&maven.artifactId=<>&maven.baseVersion=1.46.0-SNAPSHOT&maven.extension=war

download_artifact() {
    local host_url=$1
    local group_id=$2
    local artifact_id=$3
    local artifact_type=$4
    local artifact_version=$5
    local final_name=$6
    local location=$7

    local search_version=$5
    local prerelease="false"
    if [[ "${artifact_version}" == *"SNAPSHOT" ]]; then
        prerelease="true"
    fi
    if [[ "${artifact_version}" == "latest"* ]]; then
        search_version="*"
    fi
    assets_url="${host_url}/service/rest/v1/search/assets?sort=version&repository=public&maven.groupId=${group_id}&maven.artifactId=${artifact_id}&maven.baseVersion=${search_version}&prerelease=${prerelease}&maven.extension=${artifact_type}"

    echo "INFO: Assets url: $assets_url"

    download_url=$(curl "$assets_url" -H "accept: application/json" | jq -r ".items[0].downloadUrl // empty")

    echo "INFO: Downloading artifact from url: $download_url"

    if [[ -z "$download_url" ]]; then
        echo "ERROR: Artifact not exists in Nexus, please check your version [${version}] for [${service_name}]"
        exit 1
    fi

    pre_dir=$(pwd)

    if [[ ! -d "$location" ]]; then
        mkdir -p $location
    fi

    cd $location

    curl -o "${final_name}.${artifact_type}" "$download_url"

    cd $pre_dir
}
Run Code Online (Sandbox Code Playgroud)


小智 6

官方文档中的用法:

https://maven.apache.org/plugins/maven-dependency-plugin/usage.html#dependency:get
Run Code Online (Sandbox Code Playgroud)

对于我的情况,请参阅下面的答案:

mvn dependency:get -Dartifact=$2:$3:$4:$5 -DremoteRepositories=$1 -Dtransitive=false
mvn dependency:copy -Dartifact=$2:$3:$4:$5 -DremoteRepositories=$1 -Dtransitive=false -DoutputDirectory=$6

#mvn dependency:get -Dartifact=com.huya.mtp:hynswup:1.0.88-SNAPSHOT:jar -DremoteRepositories=http://nexus.google.com:8081/repository/maven-snapshots/ -Dtransitive=false
#mvn dependency:copy -Dartifact=com.huya.mtp:hynswup:1.0.88-SNAPSHOT:jar -DremoteRepositories=http://nexus.google.com:8081/repository/maven-snapshots/ -Dtransitive=false -DoutputDirectory=.
Run Code Online (Sandbox Code Playgroud)

使用该命令mvn dependency:get下载特定工件,并使用该命令mvn dependency:copy将下载的工件复制到目标目录-DoutputDirectory