maven推送離線jar包

  一、修改maven的settings.xml文件

<servers>
    <server>
      <id>maven-releases</id>
      <username>admin</username>
      <password>admin</password>
    </server>
</servers>

  二、生成腳本

package com.xbd;

import java.io.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Repository {

    public static void main(String[] args) throws Exception {
        //包目錄
        String repositoryPath = "${user.home}/.m2/repository";
        String batPath = "<outPath>\\repository.bat";
        //基礎環境
        String repoUrl="http://127.0.0.1:8081/repository/maven-releases/";
        String repoId="maven-releases";
        writeCmd(repositoryPath, batPath, repoUrl, repoId);

    }

    //寫入命令
    private static void writeCmd(String repositoryPath, String batPath, String repoUrl, String repoId) throws Exception {
        String jarCmd = "mvn deploy:deploy-file " +
                "-Durl=''{0}'' " +
                "-DrepositoryId=''{1}'' " +
                "-Dfile=''{2}'' " +
                "-DpomFile=''{3}'' " +
                "-DgroupId=''{4}'' " +
                "-DartifactId=''{5}'' " +
                "-Dversion=''{6}'' " +
                "-Dpackaging=jar " +
                "-DgeneratePom=false";
        String pomCmd = "mvn deploy:deploy-file " +
                "-Durl=''{0}'' " +
                "-DrepositoryId=''{1}'' " +
                "-Dfile=''{2}'' " +
                "-DgroupId=''{3}'' " +
                "-DartifactId=''{4}'' " +
                "-Dversion=''{5}'' " +
                "-Dpackaging=pom ";
        List<String> cmdList = new ArrayList<>();
        generateCommand(new File(repositoryPath), null, jarCmd, pomCmd, repoUrl, repoId, cmdList);
        if (cmdList.size() > 0) {
            BufferedWriter writer = new BufferedWriter(new FileWriter(batPath));
            for (String cmd : cmdList) {
                writer.append(cmd);
                writer.newLine();
            }
            writer.flush();
            writer.close();
        }

    }

    //生成命令
    private static void generateCommand(File repositoryFile, String tmpDir, String jarCmd, String pomCmd, String repoUrl, String repoId, List<String> cmdList) throws Exception {
        //1、判斷目錄
        if (repositoryFile.isDirectory()) {
            //2、獲取文件目錄
            File[] files = repositoryFile.listFiles();
            if (files != null && files.length > 0) {
                //3、判斷是否爲最後一層
                if (Arrays.stream(files).allMatch(File::isFile)) {
                    //處理目錄爲groupId,artifactId,version
                    String[] dirs = tmpDir.split("&");
                    String version = dirs[dirs.length - 1];
                    String artifactId = dirs[dirs.length - 2];
                    String groupId = String.join(".", Arrays.copyOfRange(dirs, 0, dirs.length - 2));
                    String jarName = artifactId + "-" + version;
                    //路徑
                    String pomFilePath = tmpDir.replaceAll("&", "\\" + File.separator) + File.separator + jarName + ".pom";
                    //4、判斷是否存在jar
                    if (Arrays.stream(files).anyMatch(subFile -> subFile.getName().endsWith(".jar"))) {
                        //jar路徑
                        String jarFilePath = tmpDir.replaceAll("&", "\\" + File.separator) + File.separator + jarName + ".jar";
                        String uploadCmd = MessageFormat.format(jarCmd, repoUrl, repoId, jarFilePath, pomFilePath, groupId, artifactId, version);
                        cmdList.add(uploadCmd);
                    } else if (Arrays.stream(files).anyMatch(subFile -> subFile.getName().endsWith(".pom"))){
                        //腳本
                        String uploadCmd = MessageFormat.format(pomCmd, repoUrl, repoId, pomFilePath, groupId, artifactId, version);
                        cmdList.add(uploadCmd);
                    }
                } else {
                    for (File subFile : files) {
                        generateCommand(subFile, tmpDir != null ? tmpDir + "&" + subFile.getName() : subFile.getName(), jarCmd, pomCmd, repoUrl, repoId, cmdList);
                    }
                }
            }
        }
    }
}

  三、然後在${user.home}/.m2/repository目錄下執行repository.bat腳本即可。

  四、思路說明:

  1)先在默認的settings.xml中加入服務的認證,注意ID要一樣。

  2)在項目中加入需要添加的依賴部分,通過代碼生成腳本。腳本包含jar和pom。

  3)在執行生成的repository.bat腳本來實現離線jar的上傳。

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章