批量上传依赖到私库的工具类

由于网络限制,私库不能去中央仓库下载对应的jar包,因此需要手动上传jar到私库。本文给出一个批量自动化上传工具,使得上传高效编辑。

代码中的Durl根据自己的实际情况更改。

使用:直接运行或者打成jar包运行。根据提示输入目录即可,可以试任意层目录,会自动进行扫描,批量上传。

public class Deploy {

    public static final String BASE_CMD = "cmd /c mvn " +
            "org.apache.maven.plugins:maven-deploy-plugin:2.4:deploy-file " +
            "-Durl=http://userName:password@ip:port/repository/3part/ " +
            "-DrepositoryId=xxx-central " +
            "-DgeneratePom=false";

    public static final Pattern DATE_PATTERN = Pattern.compile("-[\\d]{8}\\.[\\d]{6}-");

    public static final Runtime CMD = Runtime.getRuntime();

    public static final Writer ERROR;

    public static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(10);

    static {
        Writer err = null;
        try {
            err = new OutputStreamWriter(new FileOutputStream("deploy-error.log"), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
        ERROR = err;
    }


    public static void main(String[] args) throws IOException {
        System.out.println("/**\n" +
                " * 退出输入exit\n" +
                " * 上传请输入上传jar包的文件夹地址\n" +
                " */");
        BufferedReader buf = new BufferedReader (new InputStreamReader(System.in));
        String str = buf.readLine();
        while(!str.equals("exit"))
        {
            deploy(new File(str).listFiles());
            str = buf.readLine();
        }
        buf.close();
        
        EXECUTOR_SERVICE.shutdown();
        System.out.println("程序退出");
        try {
            ERROR.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void error(String error){
        try {
            System.err.println(error);
            ERROR.write(error + "\n");
            ERROR.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void deploy(File[] files) {
        if (files.length == 0) {
            //ignore
        } else if (files[0].isDirectory()) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deploy(file.listFiles());
                }
            }
        } else if (files[0].isFile()) {
            File pom = null;
            File jar = null;
            File source = null;
            File javadoc = null;
            //忽略日期快照版本,如 xxx-mySql-2.2.6-20170714.095105-1.jar
            for (File file : files) {
                String name = file.getName();
                if(DATE_PATTERN.matcher(name).find()){
                    //skip
                } else if (name.endsWith(".pom")) {
                    pom = file;
                } else if (name.endsWith("-javadoc.jar")) {
                    javadoc = file;
                } else if (name.endsWith("-sources.jar")) {
                    source = file;
                } else if (name.endsWith(".jar")) {
                    jar = file;
                }
            }
            if(pom != null){
                if(jar != null){
                    deploy(pom, jar, source, javadoc);
                }
            }
        }
    }
    
    public static void deploy(final File pom, final File jar, final File source, final File javadoc) {
        EXECUTOR_SERVICE.execute(new Runnable() {
            @Override
            public void run() {
                StringBuffer cmd = new StringBuffer(BASE_CMD);
                cmd.append(" -DpomFile=").append(pom.toPath().toString());
                if(jar != null){
                    //当有bundle类型时,下面的配置可以保证上传的jar包后缀为.jar
                    cmd.append(" -Dpackaging=jar -Dfile=").append(jar.toPath().toString());
                } else {
                    cmd.append(" -Dfile=").append(pom.getName());
                }
                if(source != null){
                    cmd.append(" -Dsources=").append(source.getName());
                }
                if(javadoc != null){
                    cmd.append(" -Djavadoc=").append(javadoc.getName());
                }

                try {
                    final Process proc = CMD.exec(cmd.toString(), null, pom.getParentFile());
                    InputStream inputStream = proc.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    String line;
                    StringBuffer logBuffer = new StringBuffer();
                    logBuffer.append("\n\n\n=======================================\n");
                    while((line = reader.readLine()) != null){
                        if (line.startsWith("[INFO]") || line.startsWith("Upload")) {
                            logBuffer.append(Thread.currentThread().getName() + " : " + line + "\n");
                        }
                    }
                    System.out.println(logBuffer);
                    int result = proc.waitFor();
                    if(result != 0){
                        error("上传失败:" + pom.getAbsolutePath());
                    }
                } catch (IOException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                } catch (Exception e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值