将本地maven仓库上传到私有仓库demo

16 篇文章 0 订阅
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 功能:生成maven批量安装jar的语句
 * @author pactera
 *
 */
public class Test {

    //批量安装jar所在目录
    public static String filePath = "D:\\repository";
    public static List<GroupInfo> jarList=new ArrayList<>();

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

    /**
     *   <groupId>com.alibaba.cloud</groupId>
     *                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
     * @param file
     */
    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;
           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.file=file;
               jarList.add(groupInfo);
           }
       }
    }

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

    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("\\\\",".");
    }

    /**
     * 功能:批量安装maven的本地的jar
     */
    public static void printFile() {
        //D:\git\ips-fj\libs>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);
        });
    }

    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;

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值