请求推荐基于GAV坐标扫描Nexus仓库获取JAR包的简便方案
简便实现方案推荐
方案1:使用Maven Resolver(原Aether)
这是Maven官方的依赖解析库,能直接处理远程仓库的GAV坐标解析与下载,比NexusIndexer更直观易上手。
步骤1:添加依赖
在你的pom.xml中引入核心依赖:
<dependency> <groupId>org.apache.maven.resolver</groupId> <artifactId>maven-resolver-api</artifactId> <version>1.9.15</version> </dependency> <dependency> <groupId>org.apache.maven.resolver</groupId> <artifactId>maven-resolver-impl</artifactId> <version>1.9.15</version> </dependency> <dependency> <groupId>org.apache.maven.resolver</groupId> <artifactId>maven-resolver-transport-http</artifactId> <version>1.9.15</version> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-core</artifactId> <version>3.9.5</version> </dependency>
步骤2:核心代码实现
import org.apache.maven.resolver.RepositorySystem; import org.apache.maven.resolver.RepositorySystemSession; import org.apache.maven.resolver.artifact.Artifact; import org.apache.maven.resolver.artifact.DefaultArtifact; import org.apache.maven.resolver.repository.RemoteRepository; import org.apache.maven.resolver.util.DefaultRepositorySystem; import org.apache.maven.resolver.util.DefaultRepositorySystemSession; import java.io.File; public class NexusJarDownloader { public static void downloadJar(String groupId, String artifactId, String version, String nexusRepoUrl) throws Exception { // 初始化仓库系统 RepositorySystem repoSystem = new DefaultRepositorySystem(); RepositorySystemSession session = new DefaultRepositorySystemSession(); // 配置远程Nexus仓库 RemoteRepository nexusRepo = new RemoteRepository.Builder("nexus", "default", nexusRepoUrl).build(); // 定义目标Artifact的GAV坐标 Artifact targetArtifact = new DefaultArtifact(String.format("%s:%s:%s:jar", groupId, artifactId, version)); // 解析并下载Artifact到本地 repoSystem.resolveArtifact(session, targetArtifact, nexusRepo); // 获取下载后的文件路径 File downloadedJar = targetArtifact.getFile(); System.out.println("JAR包已下载至:" + downloadedJar.getAbsolutePath()); } }
方案2:直接调用Nexus REST API
如果不想引入Maven相关依赖,可通过HTTP请求直接调用Nexus的REST接口,实现更轻量:
步骤1:构造下载URL
根据GAV坐标拼接Nexus的下载路径,注意将groupId中的.替换为/,格式示例:
http://你的Nexus地址/repository/maven-public/com/example/demo/1.0.0/demo-1.0.0.jar
步骤2:HTTP请求下载
用Java原生HttpClient实现下载逻辑:
import java.io.FileOutputStream; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class NexusRestDownloader { public static void downloadViaRest(String groupId, String artifactId, String version, String nexusBaseUrl) throws Exception { // 转换groupId格式并构造完整下载链接 String groupPath = groupId.replace(".", "/"); String downloadUrl = String.format("%s/%s/%s/%s/%s-%s.jar", nexusBaseUrl, groupPath, artifactId, version, artifactId, version); // 发送GET请求下载 HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(downloadUrl)) .GET() .build(); HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); // 将响应内容写入本地文件 try (FileOutputStream fos = new FileOutputStream(String.format("%s-%s.jar", artifactId, version))) { fos.write(response.body()); } } }
额外提示
如果需要先验证GAV坐标是否存在,可调用Nexus的搜索接口:发送GET请求到http://你的Nexus地址/service/rest/v1/search?maven.groupId={groupId}&maven.artifactId={artifactId}&maven.baseVersion={version},通过返回的JSON结果判断是否存在对应Artifact。
内容的提问来源于stack exchange,提问作者priyam singh
相关产品推荐
相关产品推荐

