批量将本地的maven仓库导入到nexus3中

前提
浏览器登录nexus管理界面–>设置图标–>Repository–>Repositories–>maven-releases–>Hosted–>请选择‘Allow redeploy’策略,(默认是disable策略,然后保存。 请注意,不同版本的nexus,进入的路径可能有细微区别)

image

  • 依赖
     <dependency>
         <groupId>com.squareup.okhttp3</groupId>
         <artifactId>okhttp</artifactId>
         <version>3.8.1</version>
     </dependency>
     

  • 代码(需要在代码中修改nexus3的请求url和base64加密的字符串)

     

    package com.test;

    import okhttp3.*;

    import java.io.File;
    import java.io.IOException;
    import java.util.*;

    public class LocalRepositoryImport {

        /**
         * 本地仓库路径
         */
        private static String basePath = "D:\\soft\\apache-maven-3.6.2\\repo\\";

        /**
         * SNAPSHOT版本
         *
         */
        private static String check = "SNAPSHOT";

        /**
         * 忽略jar文件
         */
        private static String[] ignorePath = {};

        private static List<String> jarFiles = new ArrayList<>();

        /**
         * http://192.168.8.103:30022/ web访问地址
         */
        private static String RELEASES_URL = "http://192.168.8.103:30022/service/rest/v1/components?repository=maven-releases";

        private static String SNAPSHOTS_URL = "http://192.168.8.103:30022/service/rest/v1/components?repository=maven-snapshots";

        /**
         * http请求处理器.
         */
        private static final OkHttpClient CLIENT = new OkHttpClient().newBuilder().build();


        public static void main(String[] args) throws IOException {
            loadJarFilePath(basePath);
            uploadJarFile();
        }

        /**
         * 查找jar文件
         *
         * @param basePath
         */
        private static void loadJarFilePath(String basePath) {
            File tmp = new File(basePath);
            if (!tmp.exists()) {
                return;
            }
            File[] files = tmp.listFiles();
            for (File file : files) {
                if (file.getName().endsWith(".jar")) {
                    //加入到待上传jar文件列表
                    jarFiles.add(file.getAbsolutePath());
                }
                if (file.isDirectory()) {
                    //深搜遍历
                    loadJarFilePath(file.getAbsolutePath());
                }
            }
        }

        /**
         * 上传文件
         *
         * @throws IOException
         */
        private static void uploadJarFile() throws IOException {
            if (jarFiles.size() > 0) {
                for (String jarFile : jarFiles) {
                    String url = jarFile.contains(check) ? SNAPSHOTS_URL : RELEASES_URL;

                    String spitPath = jarFile.substring(basePath.length());
                    Map<String, String> textMap = getTextMap(spitPath);
                    RequestBody formBody = getFormBody(jarFile, textMap.get("fileName"),
                            textMap.get("groupId"), textMap.get("artifactId"), textMap.get("version"));

                    Request request = new Request.Builder()
                            .url(url)
                            .method("POST", formBody)
                            //YWRtaW4lM0FhZG1pbjEyMw== BASE64加密-->格式 用户名:密码
                            .addHeader("Authorization", "Basic YWRtaW4lM0FhZG1pbjEyMw==")
                            .build();
                    Response response = CLIENT.newCall(request).execute();
                    String s = response.body().string();
                    if (s != null && !s.equals("")) {
                        System.out.println("错误信息:" + s + "\n失败jar文件: " + jarFile);
                    }
                }
            }
        }


        private static Map<String, String> getTextMap(String filePath) {
            Map<String, String> ret = new HashMap<>();
            String[] split = filePath.split("\\\\");
            ret.put("fileName", split[split.length - 1]);
            ret.put("version", split[split.length - 2]);
            ret.put("artifactId", split[split.length - 3]);
            StringJoiner groupId = new StringJoiner(".");
            for (int i = 0; i < split.length - 3; i++) {
                groupId.add(split[i]);
            }
            ret.put("groupId", groupId.toString());
            return ret;
        }


        private static RequestBody getFormBody(String jarFilePath, String fileName, String groupId, String artifactId, String version) {
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            RequestBody jarFileBody = RequestBody.create(MediaType.parse("application/octet-stream"), new File(jarFilePath));
            builder.addFormDataPart("maven2.asset1", fileName, jarFileBody);
            builder.addFormDataPart("maven2.groupId", groupId);
            builder.addFormDataPart("maven2.artifactId", artifactId);
            builder.addFormDataPart("maven2.version", version);
            builder.addFormDataPart("maven2.asset1.extension", "jar");
            return builder.build();
        }

    }


    原文转载自:https://www.cnblogs.com/anlalala/p/15608073.html

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将本地的 maven 仓库批量上传至私服,可以按照以下步骤进行: 1. 首先,确保你已经创建了私服的仓库,可以使用 Nexus、Artifactory 等工具进行搭建。 2. 确认你本地 maven 的配置文件(settings.xml)已经配置了私服的仓库地址和认证信息。一般情况下,配置文件位于 ~/.m2/settings.xml (Linux/macOS) 或 C:\Users\username\.m2\settings.xml (Windows)。 3. 打开终端或命令行界面,进入到本地 maven 仓库的根目录,该目录默认为 ~/.m2/repository (Linux/macOS) 或 C:\Users\username\.m2\repository (Windows)。 4. 使用以下命令将本地仓库上传至私服: ```shell mvn deploy:deploy-file -Durl=<私服仓库地址> -DrepositoryId=<私服仓库ID> -Dfile=<本地仓库文件路径> -DgroupId=<组织ID> -DartifactId=<项目ID> -Dversion=<版本号> -Dpackaging=<打包类型> ``` 其,需要将 `<私服仓库地址>` 替换为实际的私服仓库地址,`<私服仓库ID>` 替换为私服仓库的 ID(在 settings.xml 配置),`<本地仓库文件路径>` 替换为本地仓库文件的路径,`<组织ID>`、`<项目ID>`、`<版本号>`、`<打包类型>` 分别替换为实际的组织 ID、项目 ID、版本号和打包类型。 5. 重复上述命令,对每个需要上传的本地仓库文件进行操作,直到所有文件都上传完成。 注意:在执行命令时,确保你的网络连接稳定,并且私服仓库地址和认证信息在 settings.xml 正确配置。另外,如果你有多个私服仓库,可以在 settings.xml 配置多个 `<server>` 元素来表示每个仓库。 通过以上步骤,你可以将本地的 maven 仓库批量上传至私服,使其他开发者可以从私服获取到你上传的第三方库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值