如何使用Java/curl/JS从JFrog仓库下载构建版本
如何通过curl/Java/JS下载JFrog Artifactory最新构建版本
当然可以通过curl或编程方式下载JFrog Artifactory的最新构建版本,以下是针对不同方式的实操方案:
一、使用curl下载
步骤1:获取最新版本号(以Maven构件为例)
通过JFrog REST API查询指定构件的最新版本,替换你的JFrog实例地址、用户名、API密钥以及构件的groupId和artifactId:
curl -u "你的用户名:你的API密钥" "https://你的jfrog实例地址/artifactory/api/search/latestVersion?g=com.example&a=demo&v=LATEST"
执行后会返回最新版本号(如1.0.3)。
步骤2:下载对应版本的构件
用返回的版本号拼接下载地址,执行下载命令:
curl -u "你的用户名:你的API密钥" -O "https://你的jfrog实例地址/artifactory/你的仓库名/com/example/demo/1.0.3/demo-1.0.3.jar"
通用二进制文件下载(非Maven)
如果是普通二进制文件,先查询目标目录下最新创建的文件(需安装jq解析JSON):
curl -u "你的用户名:你的API密钥" "https://你的jfrog实例地址/artifactory/api/storage/你的仓库名/目标目录?list&deep=0&listFolders=0&sort=created&order=desc" | jq '.children[0].uri'
提取返回的文件路径后,拼接成完整下载地址执行curl下载即可。
二、使用Java下载
方案1:使用JFrog官方Java客户端
官方客户端封装了常用操作,先在pom.xml添加依赖:
<dependency> <groupId>org.jfrog.client</groupId> <artifactId>jfrog-client-java</artifactId> <version>1.15.2</version> </dependency>
然后编写代码:
import org.jfrog.client.api.ArtifactoryClient; import org.jfrog.client.api.impl.ArtifactoryClientImpl; import java.io.File; import java.io.IOException; public class JFrogArtifactDownloader { public static void main(String[] args) throws IOException { // 初始化客户端 ArtifactoryClient client = ArtifactoryClientImpl.create( "https://你的jfrog实例地址/artifactory", "你的用户名", "你的API密钥" ); // 查询Maven构件的最新版本 String latestVersion = client.repository("你的仓库名") .buildArtifactsSearch() .groupId("com.example") .artifactId("demo") .latestVersion() .search() .get(0) .getVersion(); // 拼接构件路径并下载到本地 String artifactPath = String.format("com/example/demo/%s/demo-%s.jar", latestVersion, latestVersion); File targetFile = new File("./demo-latest.jar"); client.repository("你的仓库名").download(artifactPath).toFile(targetFile); System.out.println("最新构件已下载至:" + targetFile.getAbsolutePath()); } }
方案2:使用OkHttp直接调用API
如果不想引入官方客户端,用OkHttp实现原生HTTP请求:
import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class RawJFrogDownloader { public static void main(String[] args) throws IOException { String jfrogBaseUrl = "https://你的jfrog实例地址/artifactory"; String repoName = "你的仓库名"; String username = "你的用户名"; String apiKey = "你的API密钥"; String groupId = "com.example"; String artifactId = "demo"; OkHttpClient client = new OkHttpClient(); String authHeader = "Basic " + java.util.Base64.getEncoder().encodeToString((username + ":" + apiKey).getBytes()); // 1. 获取最新版本号 Request versionReq = new Request.Builder() .url(jfrogBaseUrl + "/api/search/latestVersion?g=" + groupId + "&a=" + artifactId + "&v=LATEST") .header("Authorization", authHeader) .build(); try (Response versionResp = client.newCall(versionReq).execute()) { if (!versionResp.isSuccessful()) throw new IOException("获取版本失败:" + versionResp.code()); String latestVersion = versionResp.body().string().trim(); // 2. 下载构件 String downloadUrl = jfrogBaseUrl + "/" + repoName + "/" + groupId.replace('.', '/') + "/" + artifactId + "/" + latestVersion + "/" + artifactId + "-" + latestVersion + ".jar"; Request downloadReq = new Request.Builder() .url(downloadUrl) .header("Authorization", authHeader) .build(); try (Response downloadResp = client.newCall(downloadReq).execute(); InputStream in = downloadResp.body().byteStream(); FileOutputStream out = new FileOutputStream("./demo-latest.jar")) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } System.out.println("构件下载完成"); } } } }
三、使用JavaScript(Node.js)下载
用axios处理HTTP请求,fs模块保存文件,先安装依赖:
npm install axios
然后编写脚本:
const axios = require('axios'); const fs = require('fs'); const path = require('path'); // 配置参数 const config = { jfrogBaseUrl: 'https://你的jfrog实例地址/artifactory', repoName: '你的仓库名', username: '你的用户名', apiKey: '你的API密钥', groupId: 'com.example', artifactId: 'demo' }; async function downloadLatestArtifact() { try { // 1. 获取最新版本号 const versionResp = await axios.get( `${config.jfrogBaseUrl}/api/search/latestVersion?g=${config.groupId}&a=${config.artifactId}&v=LATEST`, { auth: { username: config.username, password: config.apiKey } } ); const latestVersion = versionResp.data.trim(); // 2. 拼接下载地址并流式下载 const artifactPath = `${config.groupId.replace(/\./g, '/')}/${config.artifactId}/${latestVersion}/${config.artifactId}-${latestVersion}.jar`; const downloadUrl = `${config.jfrogBaseUrl}/${config.repoName}/${artifactPath}`; const downloadResp = await axios.get(downloadUrl, { auth: { username: config.username, password: config.apiKey }, responseType: 'stream' }); const outputPath = path.join(__dirname, `${config.artifactId}-latest.jar`); const writer = fs.createWriteStream(outputPath); downloadResp.data.pipe(writer); await new Promise((resolve, reject) => { writer.on('finish', resolve); writer.on('error', reject); }); console.log(`最新构件已下载至:${outputPath}`); } catch (err) { console.error('下载失败:', err.message); throw err; } } // 执行下载 downloadLatestArtifact();
关键注意事项
- API密钥可在JFrog Artifactory的「个人资料 → 编辑个人资料 → API密钥」页面生成。
- 确保你的账号拥有目标仓库的读取权限,否则会返回403错误。
- 如果是npm、Docker等其他类型的构件,需要调整API查询路径和下载逻辑,可参考JFrog REST API的对应端点。
内容的提问来源于stack exchange,提问作者ArrchanaMohan
相关产品推荐
相关产品推荐

