Java 拉取 nexus 私服 maven 仓库目录及jar包到本地,快速实现 nexus 2 仓库快速迁移复制

最近公司重整CenterOs 服务器资源,项目组一直使用的基于 nexus2 搭建的 Maven 私服所在的服务器要资源回收。于是我打算在部门现有的一台闲置的Wiin10 开发主机上重新搭建一个 Maven 私服。

说做就做!

第一步将原服务器仓库里的jar包和其他文件按照仓库目录结构拉取到本地

通过分析Nexus 私服页面可以归纳出 ,仓库的路径是通过构造 table 表格的方式展示在页面上的,仓库文件夹和文件名称在Html 标签的位置可以通过“table>tbody>tr>td>a” 定位获得,通过页面内容可以分析出仓库的文件夹名称以"/"结尾,文件已后缀名结尾,基于此使用java 递归逻辑遍历 仓库目录树,在本地磁盘创建对应目录 并下载文件。
java  实现代码:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * 复刻Nexus 远程仓库目录树到本地
 */
public class CloneNexusRepository {

    public static final Logger log = LoggerFactory.getLogger(CloneNexusRepository.class);

    private static final String NEXUS_REPOSITORY_ROOTURL = "http://10.47.142.194:8081/nexus/content/repositories/releases/";

    private static final String LOCAL_DISK_NAME = "D:" + File.separator + "repository";

    private static final int jsoup_timeout = 10000;

    private static int countPom = 0;

    public static void main(String[] args) throws IOException {

        String firstPath = NEXUS_REPOSITORY_ROOTURL + "com/";
        String Localroot = LOCAL_DISK_NAME + File.separator + File.separator + "com";
        cloneNexusTree(firstPath, Localroot);

        System.out.println();

        System.out.println("共下载 :" + countPom + " 个文件.");
    }

    private static void cloneNexusTree(String cureentNextNode, String localPath) throws IOException {

        // 修建本地目录。
        File dir = new File(localPath);
        if (!dir.exists()) {
            dir.mkdir();
            System.out.println("创建路径:" + dir.getPath());
        }

        Document doc = Jsoup.connect(cureentNextNode).userAgent("Mozilla").timeout(jsoup_timeout).get();

        if (doc != null) {
            Elements elements = doc.select("table>tbody>tr>td>a");
            if (elements != null && elements.size() > 0) {
                for (Element element : elements) {
                    //判断是文件或文件夹
                    String nodeName = element.text();
                    if (!nodeName.equalsIgnoreCase("Parent Directory")) {
                        if (nodeName.endsWith("/")) {
                            // 非叶子节点.
                            String nextNodePath = cureentNextNode + nodeName;
                            String nextLocalPath = localPath + File.separator + nodeName.replaceFirst("/", "");
                            cloneNexusTree(nextNodePath, nextLocalPath);
                        } else {
                            // 叶子节点,下载文件.
                            download(cureentNextNode + nodeName, dir, nodeName);
                        }
                    }
                }
            }
        }
    }

    public static void download(String downloadUrl, File localDir, String fileName) {
        try {
            URL url = new URL(downloadUrl);
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(5 * 1000);
            InputStream inputStream = connection.getInputStream();
            byte[] byteArr = new byte[1024];
            int len;
            if (localDir.exists() == false)
                localDir.mkdirs();
            OutputStream outputStream = new FileOutputStream(localDir + File.separator + fileName);
            while ((len = inputStream.read(byteArr)) != -1) {
                outputStream.write(byteArr, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            System.out.println("下载文件失败:" + downloadUrl);
        }

        System.out.println("下载文件完成:" + downloadUrl);

        if (fileName.endsWith(".pom")) {
            countPom++;
        }
    }

}

执行代码在将在本地复刻一份仓库。


 安装nexus 私服,版本nexus-2.12.0-01-bundle.zip,解压Zip文件,依次执行下列操作:
 1、打开C:\nexus-2.12.0-01\conf\nexus.properties 文件, 在文件尾部增加“wrapper.startup.delay=30”。
 2、进入nexus bin文件夹 执行nexus install,执行完成输出如图。


 3、继续执行 nexus start 执行完成输出如图。
 
 4、打开nexus 存储仓库目录“C:\sonatype-work\nexus\storage” ,将本地复刻的仓库目录拷贝到对应的库目录下即可。

 5、访问nexus 私服 仓库管理页面。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值