我可以通过install:install-file安装一个工件, 但我怎么下载藏物呢?
例如:
mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST
我可以通过install:install-file安装一个工件, 但我怎么下载藏物呢?
例如:
mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST
当前回答
下面是我最终使用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 \
其他回答
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选项。
下面是使用Maven 3.6获取ASM-7的示例:
mvn dependency:get -DremoteRepositories=maven.apache.org -Dartifact=org.ow2.asm:7.0:sources:jar
或者你可以从这里下载罐子:https://search.maven.org/search?q=g:org.ow2.asm%20AND%20a:asm然后
mvn install:install-file -DgroupId=org.ow2.asm -DartifactId=asm -Dversion=7.0 -Dclassifier=sources -Dpackaging=jar -Dfile=/path/to/asm-7.0.jar
下面是我最终使用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 \
不幸的是,maven-dependency-plugin:get不支持版本范围,例如[2.17.1,)或[,)
如果你需要下载一个特定的maven工件,但是使用版本范围,就像我做的那样,看这里:
下载带有版本范围的Maven工件