我可以通过install:install-file安装一个工件, 但我怎么下载藏物呢?

例如:

mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST

当前回答

使用Maven依赖插件的最新版本(2.8),从Maven中央存储库下载工件非常简单:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]

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

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

其他回答

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

并不是插件的所有属性都可以在maven CLI中使用。可以指定定义了“User Property:”属性的属性。在下面的例子中,我将junit下载到我的临时文件夹中,并从jar文件中剥离版本。

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=junit:junit:4.11 -DoutputDirectory=/tmp -Dmdep.stripVersion=true

在哪里 Artifact =junit:junit:4.11是maven坐标。你指定的人造体为groupId:artifactId:version[:packaging[:classifier]]

(感谢Pascal Thivent首先提供了他的https://stackoverflow.com/a/18632876/2509415。我补充另一个答案)

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

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

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

Curl命令:

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

关于如何获取工件二进制文件,Pascal Thivent的答案是它,但也要获得工件源jar,我们可以使用:

mvn dependency:get -Dartifact=groupId:artifactId:version:jar:sources

e.g.

mvn dependency:get -Dartifact=junit:junit:4.12:jar:sources

这是因为工件参数实际上由groupId:artifactId:version[:packaging][:classifier]组成。只有包装和分类器是可选的。

使用jar作为打包器,使用源代码作为分类器,maven依赖插件可以理解我们要求的是源jar,而不是工件jar。

不幸的是,现在源jar文件不能传递下载,这是有意义的,但理想情况下,我相信它也可以像maven eclipse插件一样尊重downloadSources选项。

你也可以在PowerShell中使用docker:

docker run -it --rm -v ${PWD}:/build/source -v ${HOME}/.m2:/build/.m2 --net=host aemdesign/centos-java-buildpack:jdk8 /bin/bash --login -c 'mvn dependency:get -Dmaven.repo.local=/build/.m2/repository -DrepoUrl=https://repo1.maven.org/maven2 -Dartifact=io.prometheus.jmx:jmx_prometheus_javaagent:LATEST -Ddest=/build/source/jmx_prometheus_javaagent.jar'

或者用bash:

docker run -it --rm -v $PWD:/build/source -v $HOME/.m2:/build/.m2 --net=host aemdesign/centos-java-buildpack:jdk8 /bin/bash --login -c 'mvn dependency:get -Dmaven.repo.local=/build/.m2/repository -DrepoUrl=https://repo1.maven.org/maven2 -Dartifact=io.prometheus.jmx:jmx_prometheus_javaagent:LATEST -Ddest=/build/source/jmx_prometheus_javaagent.jar'

使用Maven依赖插件的最新版本(2.8),从Maven中央存储库下载工件非常简单:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]

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

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