栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

本地仓库上传到Nexus私仓方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

本地仓库上传到Nexus私仓方法

首先的知道你上传的是什么版本的包

如果不是快照版本的话,则可以使用如下方式

方法1.在本地仓库目录下新建mavenimport.sh 文件

#!/bin/bash
 
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":r:u:p:" opt; do
	case $opt in
		r) REPO_URL="$OPTARG"
		;;
		u) USERNAME="$OPTARG"
		;;
		p) PASSWORD="$OPTARG"
		;;
	esac
done
 
find . -type f -not -path './mavenimport.sh*' -not -path '*/.*' -not -path '*/^archetype-catalog.xml*' -not -path '*/^maven-metadata-local*.xml' -not -path '*/^maven-metadata-deployment*.xml' | sed "s|^./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

用git窗口执行命令即可  

命令指定私仓的账号密码,

./mavenimport.sh -u admin -p admin123 -r http://172.16.3.30:8081/repository/myrep/

方法2.windows本地仓库目录下新建uploadRepo.bat文件

脚本里包含了私仓账号密码,还有本地仓库目录

@echo off 
set work_path= D:lib
C: 
cd %work_path%

for /R %%s in (*.pom) do (

	cd %%~dps

	if exist %%~dpns.jar (
		curl -v -u admin:admin123 -X POST http://172.16.3.30:8081/service/rest/v1/components?repository=myrep -F maven2.asset1=@%%~ns.jar -F maven2.asset1.extension=jar -F maven2.asset2=@%%~ns.pom -F maven2.asset2.extension=pom
	) else (
		curl -v -u admin:admin123 -X POST http://172.16.3.30:8081/service/rest/v1/components?repository=myrep -F maven2.asset2=@%%~ns.pom -F maven2.asset2.extension=pom
	)
) 

pause
如果需要批量上传快照版本的话,就必须使用命令了

下面用java代码写了一个脚本,前提是目录下放的全是快照版本的jar哈,别弄错了

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class Test {

    //批量安装jar所在目录
    public static String filePath = "D:\lib2";
    public static String url = "http://admin:admin123@172.16.3.30:8081/repository/maven-snapshots/";
    public static List jarList=new ArrayList<>();

    public static void main(String[] args) {
        File f = new File(filePath);
        getJarList(f);
//        jarList.forEach(System.out::println);
        printFile();
    }

    
    private static void getJarList(File file) {
       if(file.isDirectory()){
           for (File listFile : file.listFiles()) {
               getJarList(listFile);
           }
       }else if(file.getName().endsWith(".jar")){
           GroupInfo groupInfo = new GroupInfo();
           groupInfo.version=file.getParentFile().getName();
           groupInfo.artifactId=file.getParentFile().getParentFile().getName();
           groupInfo.groupId=getGroupId(file);
           groupInfo.file=file;
           groupInfo.pomFile=getPomFile(file.getParentFile());
           jarList.add(groupInfo);
       }else if(file.getName().endsWith(".pom")){
           if(ifPomDir(file.getParentFile())){
               GroupInfo groupInfo = new GroupInfo();
               groupInfo.version=file.getParentFile().getName();
               groupInfo.artifactId=file.getParentFile().getParentFile().getName();
               groupInfo.groupId=getGroupId(file);
               groupInfo.isPom=true;
               groupInfo.pomFile=file;
               jarList.add(groupInfo);
           }
       }
    }

    private static boolean ifPomDir(File parentFile) {
       return !Arrays.stream(parentFile.listFiles()).anyMatch(f->f.getName().endsWith(".jar"));
    }
    private static File getPomFile(File parentFile) {
       return Arrays.stream(parentFile.listFiles()).filter(f->f.getName().endsWith(".pom")).findAny().get();
    }

    private static String getGroupId(File file) {
        String replace = file.getAbsolutePath().replace(filePath, "");
        String artifactId = file.getParentFile().getParentFile().getName();
        return replace.substring(1,replace.indexOf(artifactId)-1).replaceAll("\\",".");
    }

    
    public static void printFile() {
        //D:gitips-fjlibs>mvn install:install-file -Dfile=ips-data-core-0.0.1-SNAPSHOT.jar -DgroupId=boco -DartifactId=ips-data-core  -Dversion=0.0.1-SNAPSHOT  -Dpackaging=pom
        //mvn install:install-file -DgroupId=com.xxx -DartifactId=xxx -Dversion=1.1.1 -Dpackaging=pom -Dfile=xxx-1.1.1.pom
//        jarList.forEach(jar->{
//            System.out.println("导入jar到本地仓库"+jar);
//            String sb = "mvn install:install-file -Dfile="+jar.file.getAbsolutePath()
//                    +" -DgroupId="+jar.groupId
//                    +" -DartifactId="+jar.artifactId
//                    +" -Dversion="+jar.version
//                    +" -Dpackaging="+(jar.isPom?"pom":"jar");
//            System.out.println(sb);
//            execCommandAndGetOutput(sb);
//        });
        jarList.forEach(jar->{
            System.out.println("导入jar到本地仓库"+jar);
            String sb =null;
            if(!jar.isPom){
                sb = "mvn deploy:deploy-file "-DgroupId="+ jar.groupId+""" +
                        " "-DartifactId="+ jar.artifactId+"" " +
                        ""-Dversion="+jar.version+"" "-Dpackaging=jar"" +
                        " "-Dfile="+jar.file.getAbsolutePath()+"" " +
                        ""-Durl="+url+""" +
                        " "-DpomFile="+jar.pomFile.getAbsolutePath()+""";
            }else{
                sb = "mvn deploy:deploy-file "-DgroupId="+ jar.groupId+""" +
                        " "-DartifactId="+ jar.artifactId+"" " +
                        ""-Dversion="+jar.version+"" "-Dpackaging=pom"" +
                        " "-Dfile="+jar.pomFile.getAbsolutePath()+"" " +
                        ""-Durl="+url+""" ;
            }
            System.out.println(sb);
            execCommandAndGetOutput(sb);
        });
    }

    public static void execCommandAndGetOutput(String comond) {
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("cmd.exe /c "+comond);
            // 输出结果,必须写在 waitFor 之前
            String outStr = getStreamStr(process.getInputStream());
            // 错误结果,必须写在 waitFor 之前
            String errStr = getStreamStr(process.getErrorStream());
            int exitValue = process.waitFor(); // 退出值 0 为正常,其他为异常
            System.out.println("exitValue: " + exitValue);
            System.out.println("outStr: " + outStr);
            System.out.println("errStr: " + errStr);
            process.destroy();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static String getStreamStr(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("n");
        }
        br.close();
        return sb.toString();
    }

    static class GroupInfo{
        String artifactId;
        String version;
        String groupId;
        Boolean isPom=false;
        File file;
        File pomFile;

        @Override
        public String toString() {
            return "GroupInfo{" +
                    "artifactId='" + artifactId + ''' +
                    ", version='" + version + ''' +
                    ", groupId='" + groupId + ''' +
                    '}';
        }
    }
}

运行即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号