You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于ANT或Jenkins插件从Nexus仓库下载制品的方案问询

我来给你两个完全符合需求的解决方案,不用Maven、wget或curl,分别适配Jenkins环境和纯Ant场景:

方案1:使用Jenkins的Nexus Artifact Downloader插件

这是Jenkins端最省心的方案,和你用来上传的Nexus Artifact Uploader属于同生态工具,配置逻辑一致,上手很快:

  1. 安装插件:在Jenkins的「插件管理」里搜索并安装「Nexus Artifact Downloader」插件
  2. 配置下载任务:
    • 如果是自由风格项目:
      新增构建步骤,选择「Nexus Artifact Downloader」,填写核心参数:
      • Nexus Repository URL:你的Nexus仓库地址(比如http://your-nexus:8081/repository/your-repo/)
      • GroupId/ArtifactId/Version:和你上传时完全一致的制品坐标
      • Type:固定填zip
      • Credentials:选择已配置的Nexus认证凭据(如果仓库需要权限验证)
      • Target Directory:指定zip下载后存放的目录(比如${WORKSPACE}/downloads)
    • 如果是Pipeline项目:
      直接用插件提供的流水线语法,示例如下:
      stage('Pull Zip from Nexus') {
          steps {
              nexusArtifactDownloader(
                  nexusUrl: 'http://your-nexus:8081',
                  nexusCredentialsId: 'nexus-auth-cred',
                  artifactId: 'your-project-artifact',
                  groupId: 'com.your.company',
                  version: '1.0.0-SNAPSHOT',
                  type: 'zip',
                  repository: 'your-hosted-repo',
                  outputDirectory: '${WORKSPACE}/target'
              )
          }
      }
      
方案2:纯Ant脚本实现下载

如果需要脱离Jenkins,直接用Ant完成下载,Ant自带的<get>任务就能搞定,核心是构造正确的Nexus下载URL并处理认证:

首先,你需要按照Nexus的规则构造制品的直接下载URL,格式如下:
http://your-nexus:8081/repository/your-repo/com/your/company/your-project-artifact/1.0.0/your-project-artifact-1.0.0.zip
(如果zip带classifier,格式为your-project-artifact-1.0.0-classifier.zip)

然后编写Ant的build.xml示例:

<project name="nexus-zip-download" default="download">

    <!-- 自定义参数,替换成你实际的信息 -->
    <property name="nexus.host" value="http://your-nexus:8081"/>
    <property name="nexus.repo" value="your-hosted-repo"/>
    <property name="group.id" value="com.your.company"/>
    <property name="artifact.id" value="your-project-artifact"/>
    <property name="artifact.version" value="1.0.0"/>
    <property name="artifact.type" value="zip"/>
    <!-- 有classifier就取消注释并填写 -->
    <!-- <property name="artifact.classifier" value="debug"/> -->

    <!-- 自动转换groupId为路径格式(比如com.your.company → com/your/company) -->
    <property name="group.path" value="${replace(group.id, '.', '/')}"/>
    <!-- 构造制品文件名 -->
    <property name="artifact.filename" value="${artifact.id}-${artifact.version}${artifact.classifier?'-'+artifact.classifier:''}.${artifact.type}"/>
    <!-- 最终下载URL -->
    <property name="download.url" value="${nexus.host}/repository/${nexus.repo}/${group.path}/${artifact.id}/${artifact.version}/${artifact.filename}"/>

    <target name="download">
        <echo message="开始从Nexus下载:${download.url}"/>
        <get src="${download.url}" dest="./${artifact.filename}" verbose="true">
            <!-- 如果Nexus需要认证,添加Basic认证头 -->
            <header name="Authorization" value="Basic ${base64Encode('nexus-username:nexus-password')}"/>
        </get>
        <!-- 校验下载是否成功 -->
        <available file="./${artifact.filename}" property="download.success"/>
        <fail unless="download.success" message="制品下载失败,请检查URL或权限!"/>
        <echo message="制品已成功下载到:./${artifact.filename}"/>
    </target>

</project>

说明:

  • 把参数替换成你实际的Nexus地址和制品信息
  • 如果仓库不需要认证,直接删掉<header>那一行即可
  • base64Encode是Ant自带函数,用来生成Basic认证的编码串,替换成你的Nexus账号密码就行
  • 执行时直接运行ant download就能完成下载

这两个方案完全满足你的限制条件,没有用到Maven或Unix命令,分别适配Jenkins自动化场景和纯Ant离线场景。

内容的提问来源于stack exchange,提问作者Ranjeet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:12:24