一、修改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的上传。
标签:repoUrl,String,离线,jar,maven,File,subFile,repoId From: https://www.cnblogs.com/ll409546297/p/17904172.html