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

例如:

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

当前回答

你也可以在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提供的精彩答案。我正在添加另一个答案,因为它不适合评论,而且它对编辑来说太广泛了。)

你可以使用maven依赖插件,它有一个很好的依赖:从2.1版开始。不需要pom,一切都发生在命令行上。

为了确保找到依赖项:get目标,你需要显式地告诉maven使用2.1版本,也就是说,你需要使用插件的完全限定名,包括版本:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
    -DrepoUrl=url \
    -Dartifact=groupId:artifactId:version

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

可以这样做:

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

<usePluginRegistry>true</usePluginRegistry>

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>

但这似乎不再适用于maven 2.1/2.2。实际上,根据插件注册表介绍,Plugin - Registry .xml的特性已经重新设计(为了可移植性),插件注册表目前在Maven 2中处于半休眠状态。所以我认为我们现在必须使用长名称(当使用没有pom的插件时,这是依赖项背后的思想:get)。

你可以使用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。我补充另一个答案)

在没有mvn的情况下下载最新maven工件的一行代码:

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"

官方文件中的用法:

https://maven.apache.org/plugins/maven-dependency-plugin/usage.html#dependency:get

对于我的案例,请看下面的答案:

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

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