源码交付、批量导出jar、批量上传至nexus

发布时间 2023-06-02 08:12:42作者: zno2

思路:
新建一个目录用于存放各个项目源码;
新建一个settings文件用于本地仓库和nexus认证配置;
通过maven dependency 插件获取依赖的组件列表和坐标信息;
再通过 maven deploy 插件将组件批量上传。

 

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>D:\mvn2</localRepository>
  <servers>
    <server>
      <id>user-release</id>
      <username>admin</username>
      <password>xiaoshi</password>
    </server>
  <server>
      <id>user-snapshot</id>
      <username>admin</username>
      <password>xiaoshi</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>user-release</id>
          <url>http://192.168.200.55:8081/repository/maven-releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
          <id>user-snapshot</id>
          <url>http://192.168.200.55:8081/repository/maven-snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://192.168.200.55:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

//    mvn -f D:\xs\jiaofu\qishi-wiki\pom.xml dependency:list
// 输出格式过滤"[INFO]    "
//    mvn -f D:\xs\jiaofu\qishi-wiki\pom.xml -DoutputDirectory=D://xs/jiaofu/lib dependency:copy-dependencies
    
//    mvn.cmd -f D:\xs\jiaofu\qishi-cas\pom.xml dependency:list
//    mvn.cmd -f D:\xs\jiaofu\qishi-cas\pom.xml -DoutputDirectory=D://xs/jiaofu/lib/ dependency:copy-dependencies

    static String jiaofuFolder = "D://xs/jiaofu/";
    static String libPath = jiaofuFolder + "lib/";
    static String settingsPath = jiaofuFolder + "settings.xml";

    static String releaseUrl = "http://192.168.200.55:8081/repository/maven-releases/";
    static String snapshotUrl = "http://192.168.200.55:8081/repository/maven-snapshots/";
    static String releaseId = "user-release";
    static String snapshotId = "user-snapshot";

    static Set<String> set = new TreeSet<>();

    public static void main(String[] args) throws IOException {
        File[] poms = search("pom.xml", new File(jiaofuFolder));
        for (File file : poms) {
            String filePath = file.getCanonicalPath();
            if (filePath.contains("target")) {
                continue;
            }
            String listCmdStr = String.format("mvn.cmd -f %s dependency:list", filePath);
            findDep(exe(listCmdStr));
            String copyCmdStr = String.format("mvn.cmd -f %s -DoutputDirectory=%s dependency:copy-dependencies",
                    filePath, libPath);
            exe(copyCmdStr);
        }
        genBatch();
    }

    static void findDep(String result) {
        String[] lines = result.split("\r\n|\r");
        for (String line : lines) {
//            org.bouncycastle:bcprov-jdk16:jar      :1.46   :compile
//            com.aspose      :aspose-words:jar:jdk17:21.11PJ:compile
            if (line.startsWith("[INFO]    ")) {
                set.add(line.replace("[INFO]    ", "").trim());
            }
        }
    }

    static void genBatch() {
        for (String dep : set) {
            String deployCmdPattern = "mvn deploy:deploy-file -Dfile=%s -DartifactId=%s -DgroupId=%s -Dversion=%s -Dpackaging=%s -Durl=%s -DrepositoryId=%s -s %s";
            String[] items = dep.split(":");
            String groupId = items[0];
            String artifactId = items[1];
            String packaging = items[2];
            String classifier = items.length == 6 ? items[3] : null;
            String version = items.length == 6 ? items[4] : items[3];
            boolean isS = version.endsWith("-SNAPSHOT");
            String deployCmdStr = String.format(deployCmdPattern,
                    libPath + artifactId + "-" + version + (classifier == null ? "" : "-" + classifier) + "."
                            + packaging,
                    artifactId, groupId, version, packaging, isS ? snapshotUrl : releaseUrl,
                    isS ? snapshotId : releaseId, settingsPath);
            System.out.println(deployCmdStr);
        }

    }

    public static File[] search(String target, File inWhichDir) {
        if ("*".equals(target)) {
            target = target.replace("*", "[\\d\\D]*");
        } else if (target.lastIndexOf("*.") == 0) {
            target = target.replace("*.", "[\\d\\D]*\\.");
        }
        try {
            List<File> matched = new ArrayList<File>();
            Pattern pattern = Pattern.compile(target);
            list(pattern, inWhichDir, matched);
            return matched.toArray(new File[matched.size()]);
        } catch (Exception e) {
            throw new RuntimeException("搜索失败", e);
        }
    }

    private static void list(Pattern pattern, File inWhichDir, List<File> matched) {
        File[] files = inWhichDir.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            String name = file.getName();
            Matcher matcher = pattern.matcher(name);
            if (matcher.find()) {
                matched.add(file);
            }
            if (file.isDirectory()) {
                list(pattern, file, matched);
            }
        }
    }

    public static String exe(String cmdStr) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(cmdStr);
        String result = toStr(process.getInputStream());
        process.destroy();
        return result;
    }

    public static String toStr(InputStream in) throws IOException {
        BufferedInputStream b = new BufferedInputStream(in);
        byte[] buffer = new byte[1024];
        int l = 0;
        StringBuffer sb = new StringBuffer();
        while ((l = b.read(buffer)) != -1) {
            sb.append(new String(buffer, 0, l));
        }
        return sb.toString();
    }
}

 

效果:

mvn deploy:deploy-file -Dfile=D://xs/jiaofu/lib/antlr-2.7.7.jar -DartifactId=antlr -DgroupId=antlr -Dversion=2.7.7 -Dpackaging=jar -Durl=http://192.168.200.55:8081/repository/maven-releases/ -DrepositoryId=user-release -s D://xs/jiaofu/settings.xml
mvn deploy:deploy-file -Dfile=D://xs/jiaofu/lib/aopalliance-1.0.jar -DartifactId=aopalliance -DgroupId=aopalliance -Dversion=1.0 -Dpackaging=jar -Durl=http://192.168.200.55:8081/repository/maven-releases/ -DrepositoryId=user-release -s D://xs/jiaofu/settings.xml
mvn deploy:deploy-file -Dfile=D://xs/jiaofu/lib/asm-3.3.1.jar -DartifactId=asm -DgroupId=asm -Dversion=3.3.1 -Dpackaging=jar -Durl=http://192.168.200.55:8081/repository/maven-releases/ -DrepositoryId=user-release -s D://xs/jiaofu/settings.xml
.......
.......
.......