我可以通过install:install-file安装一个工件, 但我怎么下载藏物呢?
例如:
mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST
我可以通过install:install-file安装一个工件, 但我怎么下载藏物呢?
例如:
mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST
当前回答
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
}
其他回答
你可以使用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)。
命令:
mvn install:install-file
通常在本地存储库中安装工件,因此不需要下载它。然而,如果您想与他人共享您的工件,您将需要将工件部署到一个中央存储库,请参阅deploy插件了解更多细节。
此外,向POM添加依赖项将在构建项目时自动获取所需的任何第三方工件。例如,这将从中央存储库下载工件。
下面是我最终使用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 \
要在指定位置复制工件,请使用copy而不是get。
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.2:copy \
-DrepoUrl=someRepositoryUrl \
-Dartifact="com.acme:foo:RELEASE:jar" -Dmdep.stripVersion -DoutputDirectory=/tmp/
LATEST已弃用,请尝试使用range [,)
./mvnw org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
-DremoteRepositories=repoId::default::https://nexus/repository/maven-releases/ \
"-Dartifact=com.acme:foo:[,)"