从nexus下载所有jar包方法 迁移到其他nexus中

起因:最近因为阿里云提供了私有仓库服务,所以想停掉自己搭建的个人私服,于是涉及到了一个迁移问题。

途径是通过调用nexus自己的api来实现下载所有jar包

首先在API中获得json文件
在这里插入图片描述
在这里插入图片描述

然后开始解析文件并下载
下载代码贴图,如果仓库需要权限验证,建议先改成匿名访问。

package com.jlbtuz.tools.nexus;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fire.helper.base.StringHelper;
import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DownloadJar {

    private static final String initPath = "D:\\nexusd\\";
//    private static final String filePath = "D:\\project-owner\\tools\\nexus-util\\src\\main\\resources\\product.json";
    private static final String filePath = "D:\\project-owner\\tools\\nexus-util\\src\\main\\resources\\fire.json";
//    private static final String filePath = "/close/tmp/product.json";

    public HashMap<String, Object> readJson() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();


        FileReader fileReader = new FileReader(filePath);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer sb = new StringBuffer();
        String s = "";
        while (StringHelper.isNotEmpty(s = bufferedReader.readLine())) {
            sb.append(s.trim());
        }
        return objectMapper.readValue(sb.toString(), HashMap.class);
    }

    public void download() throws IOException {
        HashMap<String, Object> stringStringHashMap = readJson();

        List<Map> list = (List) stringStringHashMap.get("items");

        for (Map map : list) {
            List<Map> assets = (List<Map>) map.get("assets");
            for (Map dMap : assets) {
                String url = StringHelper.valueOf(dMap.get("downloadUrl"));
                String path = StringHelper.valueOf(dMap.get("path"));
                genFile(url, initPath + path);
            }
        }

    }


    private void genFile(final String urlStr, final String path) {
        System.out.println(path);

        URL url = null;
        try {
            url = new URL(urlStr);
            String tempFileName = path;

            // 先创建文件夹
            File t = new File(path);
            t.getParentFile().mkdirs();
            File temp = new File(tempFileName);
            FileUtils.copyURLToFile(url, temp);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static void main(String args[]) throws IOException {
        new DownloadJar().download();
    }
}

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jlbtuz.tools</groupId>
    <artifactId>nexus-util</artifactId>
    <version>1.0.1</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.jlbtuz.fire.helper</groupId>
            <artifactId>fire-helper</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>

</project>

接下来使用阿里云工具迁移仓库就可以了
参考
在这里插入图片描述

$ java -jar migrate-local-repo-tool.jar -cd "/$HOME/.m2/repository/" -t "http://10.21.11.11/repository/releases/" -u admin -p admin123
参数注解:
-cd   您要迁移的本地目录

-t    目标仓库地址,您可以在云效上点击仓库地址,获取您的目标仓库地址

-p    密码

-u    用户名

注意此处的用户名密码,需要下载setting.xml文件,在里面获取密码。
在这里插入图片描述
后来经过测试,可以用这个jar包迁移任意仓库目录。
这个jar包在阿里云的代码仓库里面,一下地址需要自行注册
https://repomanage.rdc.aliyun.com/my/repo

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值