批量上传 Jar 包到 Maven 私服的工具

基本信息

适用环境:内网环境下的 Maven 私服,无法连接外网(或者需要翻墙),需要通过其他手段下载完依赖后导入到内网私服的情况。

功能描述:

  • 单个依赖包含的pom,jar等文件应该在一个单独的目录中,可以指定下面的路径,上传 gson 到私服。
    这里写图片描述

  • 还可以指定到 f:\\.m2\\repository\\Gson\\gson,上传 gson 的多个版本。

  • 也可以直接 f:\\.m2\\repository,将整个仓库下面的所有 jar 包的所有版本都上传到私服。

注意: 上传前,如果允许重复上传到私服,就需要在私服配置,允许 redeploy,否则已经存在的会报错。

下载 Jar 包

如果是下载单个的jar包,可以从 http://mvnrepository.com/ 搜素下载,下载的时候(根据连接打开一个地址,下载pom,jar,source,javadoc)。

如果是针对项目,可以先配置一个新的本地仓库路径(避免和已有jar搅和一起不好区分)。

为了可以下载source和javadoc,在 settings.xml 中增加下面的配置:

<profiles>  
  <profile>  
    <id>downloadSources</id>  
    <properties>  
      <downloadSources>true</downloadSources>  
      <downloadJavadocs>true</downloadJavadocs>             
    </properties>  
  </profile>  
</profiles>  

<activeProfiles>  
  <activeProfile>downloadSources</activeProfile>  
</activeProfiles> 

在项目下面执行:mvn clean install 命令。

执行完成后,再次执行:mvn dependency:sources下载源码。

如果需要 javadoc ,可以执行命令: mvn dependency:resolve -Dclassifier=javadoc

需要在 settings.xml 中设置好账号密码,参考如下。

<server>
    <id>thirdpart</id>
    <username>admin</username>
    <password>123456</password>
</server>

上传命令

使用下面的命令可以上传依赖到私服。

mvn deploy:deploy-file -Durl=file:///home/me/m2-repo -DrepositoryId=some.repo.id -Dfile=./path/to/artifact-name-1.0.jar -DpomFile=./path/to/pom.xml -Dsources=./path/to/artifact-name-1.0-sources.jar -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar

自动化

手动使用这个命令上传时,还不如直接通过nexus的前台进行上传,为了可以自动批量上传,我们可以写个小程序来利用这个命令进行批量操作。

当写一个可以批量上传依赖的程序时,还需要考虑如果packaging=pom或者packaging=bundle时,需要特殊处理。pom时,Dfile DpomFile两个参数都指定为pom文件即可,bundle时,需要指定-Dpackaging=jar,由于jar时这个参数也没问题,所以无论bundle还是jar都带上这个命令。

下面开始代码。

/**
 * 上传依赖到 Maven 私服
 *
 * @author liuzenghui
 * @since 2017/7/31.
 */
public class Deploy {
    /**
     * mvn -s F:\.m2\settings.xml
     * deploy:deploy-file 
     * -Durl=http://IP:PORT/nexus/content/repositories/thirdpart 
     * -DrepositoryId=thirdpart
     * -Dfile=antlr-2.7.2.jar
     * -DpomFile=antlr-2.7.2.pom
     * -Dpackaging=jar
     * -DgeneratePom=false
     * -Dsources=./path/to/artifact-name-1.0-sources.jar
     * -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
     */
    public static final String BASE_CMD = "cmd /c mvn " +
            "-s F:\\.m2\\settings.xml " + 
            "deploy:deploy-file " +
            "-Durl=http://IP:PORT/nexus/content/repositories/thirdpart " +
            "-DrepositoryId=thirdpart " +
            "-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);

先看第一部分,BASE_CMD 是基础的命令部分。

  1. cmd /c 可以保证使用 Java 的runtime 执行命令时,可以找到命令。
  2. -s F:\\.m2\\settings.xml 参数指定了配置文件的路径(避免多个配置的时候不知道配置那个)。
  3. deploy:deploy-file 是上传文件的命令。
  4. -Durl=xxx指定了上传的位置,从nexus中可以找到这个地址。
  5. -DrepositoryId=thirdpart必须和上面指定的地址一致,从nexus仓库配置可以看到这个id,另外上面提到的settings.xml中的用户密码要和这个id匹配。
  6. -DgeneratePom=false因为我们会传pom文件,所以禁用自动生成。

后面的 DATE_PATTERN主要是存在快照版时,忽略日期形式的版本,只保留SNAPSHOT形式的。

再后面获取了一个 CMD 和一个线程池。

继续代码。

    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 error(String error){
        try {
            System.err.println(error);
            ERROR.write(error + "\n");
            ERROR.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

创建了一个文件来记录错误信息,并且提供了一个静态方法方便使用。

下面是参数校验和提示信息。

    public static boolean checkArgs(String[] args){
        if (args.length != 1) {
            System.out.println("用法如: java -jar Deploy D:\\some\\path\\");
            return false;
        }
        File file = new File(args[0]);
        if (!file.exists()) {
            System.out.println(args[0] + " 目录不存在!");
            return false;
        }
        if (!file.isDirectory()) {
            System.out.println("必须指定为目录!");
            return false;
        }
        return true;
    }

下面方法判断pom文件的packaging 是否为 pom:

    public static boolean packingIsPom(File pom){
        BufferedReader reader = null;
        try {
            BufferedReader reader = 
              new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
            String line;
            while((line = reader.readLine()) != null){
                if(line.trim().indexOf("<packaging>pom</packaging>")!=-1){
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
          try{reader.close();}catch(Exception e){}
        }
        return false;
    }

当为pom类型时,只需要上传pom。

public static void deployPom(final File pom) {
        EXECUTOR_SERVICE.execute(new Runnable() {
            @Override
            public void run() {
                StringBuffer cmd = new StringBuffer(BASE_CMD);
                cmd.append(" -DpomFile=").append(pom.getName());
                cmd.append(" -Dfile=").append(pom.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 (InterruptedException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }

注意DpomFile和Dfile都指定的pom文件。

当上传的文件包含 jar 时,使用下面的方式。


    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.getName());
                if(jar != null){
                    //当有bundle类型时,下面的配置可以保证上传的jar包后缀为.jar
                    cmd.append(" -Dpackaging=jar -Dfile=").append(jar.getName());
                } 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 (InterruptedException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }

必须有pom和jar,source和javadoc可选。

下面是一个对上面代码封装后的方法,这个方法用于迭代查找包含pom,jar,source和javadoc的目录和文件。

    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);
                } else if(packingIsPom(pom)){
                    deployPom(pom);
                }
            }
        }
    }

在main方法中,有两种调用方式。

    public static void main(String[] args) {
        deploy(new File("F:\\.m2\\repository").listFiles());
        EXECUTOR_SERVICE.shutdown();
        try {
            ERROR.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

直接指定一个仓库的目录即可。

还可以是更具体的目录:

deploy(new File("F:\\.m2\\repository\\org\\apache\\tomcat\\xxx\\1.0.0\\").listFiles());

如果想通过命令行调用时指定目录,可以用下面的main方法。

    public static void main(String[] args) {
        if(checkArgs(args)){
            File file = new File(args[0]);
            deploy(file.listFiles());
        }
        EXECUTOR_SERVICE.shutdown();
        try {
            ERROR.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

通过上面这种方式可以很轻易的将依赖传到私服中。如果修改上面url参数为-Durl=E:\\.m2\\repository,还可以打包到本地仓库中。

虽然内网使用私服的情况不常见,如果遇到这种情况,使用这个代码批量传多少jar包都会变得很容易。

完整代码

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

/**
 * 上传依赖到 Maven 私服
 *
 * @author liuzenghui
 * @since 2017/7/31.
 */
public class Deploy {
    /**
     * mvn -s F:\.m2\settings.xml
     * org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file
     * -Durl=http://IP:PORT/nexus/content/repositories/thirdpart
     * -DrepositoryId=thirdpart
     * -Dfile=antlr-2.7.2.jar
     * -DpomFile=antlr-2.7.2.pom
     * -Dpackaging=jar
     * -DgeneratePom=false
     * -Dsources=./path/to/artifact-name-1.0-sources.jar
     * -Djavadoc=./path/to/artifact-name-1.0-javadoc.jar
     */
    public static final String BASE_CMD = "cmd /c mvn " +
            "-s F:\\.m2\\settings.xml " + 
            "deploy:deploy-file " +
            "-Durl=http://IP:PORT/nexus/content/repositories/thirdpart " +
            "-DrepositoryId=thirdpart " +
            "-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) {
        deploy(new File("F:\\.m2\\repository").listFiles());
//        if(checkArgs(args)){
//            File file = new File(args[0]);
//            deploy(file.listFiles());
//        }
        EXECUTOR_SERVICE.shutdown();
        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 boolean checkArgs(String[] args){
        if (args.length != 1) {
            System.out.println("用法如: java -jar Deploy D:\\some\\path\\");
            return false;
        }
        File file = new File(args[0]);
        if (!file.exists()) {
            System.out.println(args[0] + " 目录不存在!");
            return false;
        }
        if (!file.isDirectory()) {
            System.out.println("必须指定为目录!");
            return false;
        }
        return true;
    }

    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);
                } else if(packingIsPom(pom)){
                    deployPom(pom);
                }
            }
        }
    }

    public static boolean packingIsPom(File pom){
        BufferedReader reader = null;
        try {
            BufferedReader reader = 
              new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
            String line;
            while((line = reader.readLine()) != null){
                if(line.trim().indexOf("<packaging>pom</packaging>")!=-1){
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
          try{reader.close();}catch(Exception e){}
        }
        return false;
    }

    public static void deployPom(final File pom) {
        EXECUTOR_SERVICE.execute(new Runnable() {
            @Override
            public void run() {
                StringBuffer cmd = new StringBuffer(BASE_CMD);
                cmd.append(" -DpomFile=").append(pom.getName());
                cmd.append(" -Dfile=").append(pom.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 (InterruptedException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }

    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.getName());
                if(jar != null){
                    //当有bundle类型时,下面的配置可以保证上传的jar包后缀为.jar
                    cmd.append(" -Dpackaging=jar -Dfile=").append(jar.getName());
                } 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 (InterruptedException e) {
                    error("上传失败:" + pom.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }
}

使用方式

  1. 导入项目直接运行 main 方法。
  2. 使用 javac 编译为class后运行,由于代码存在中文,java代码需要使用utf8格式保存,编译时通过-encoding utf8参数指定。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值