Java实现将本地maven仓库中的jar包上传至远程仓库
·
1、思路
本文是通过java语言编写工具类,使用cmd命令将本地maven仓库中的jar包,上传至远程仓库。
文中本地maven仓库地址:D:\repository。
文中远程仓库地址(改成自己公司的私服地址即可):http://ip:port/xxx/。
2、Java实现执行cmd命令
编写一个cmd工具类。
package com.gzp.demo.cmd;
import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class CmdUtils {
/**
* 执行cmd命令
*
* @param cmdOrder cmd命令
* @param path 执行路径,即该命令需要在哪个路径下执行
* @return 执行结果
*/
public static String executeCmd(String cmdOrder, String path) {
String result = "";
BufferedReader bufferedReader = null;
BufferedReader errorBufferedReader = null;
try {
String[] command = {"cmd", "/c", cmdOrder};
ProcessBuilder processBuilder = new ProcessBuilder(command);
if (StringUtils.hasText(path)) {
processBuilder.directory(new File(path));
}
Process process = processBuilder.start();
String line;
// 输出正常结果
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));
while ((line = bufferedReader.readLine()) != null) {
result += line + "\n";
}
// 输出异常结果
errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "gbk"));
while ((line = errorBufferedReader.readLine()) != null) {
result += line + "\n";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bufferedReader) {
bufferedReader.close();
}
if (null != errorBufferedReader) {
errorBufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
String cmdOrder = "mvn -v";
String result = executeCmd(cmdOrder, null);
System.out.println(result);
}
}
3、修改settings.xml文件
修改本地${maven.home}\conf\settings.xml文件,添加server凭证信息。
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
- id:自定义。
- username:远程仓库的账号。
- password:远程仓库的密码。
4、jar包上传远程仓库的cmd命令
1、只上传pom
mvn deploy:deploy-file -DgroupId=io.reactivex -DartifactId=rxnetty -Dversion=0.4.20 -Dpackaging=pom -Dfile=D:\repository\io\reactivex\rxnetty\0.4.20\rxnetty-0.4.20.pom -DrepositoryId=deploymentRepo -Durl=http://ip:port/xxx/
2、只上传jar(注:除非该jar包确实没有pom文件,否则generatePom的值建议为false)
mvn deploy:deploy-file -DgroupId=io.reactivex -DartifactId=rxnetty -Dversion=0.4.20 -Dpackaging=jar -Dfile=D:\repository\io\reactivex\rxnetty\0.4.20\rxnetty-0.4.20.jar -DrepositoryId=deploymentRepo -Durl=http://ip:port/xxx/ -DgeneratePom=false
3、上传jar和pom
mvn deploy:deploy-file -DgroupId=io.reactivex -DartifactId=rxnetty -Dversion=0.4.20 -Dpackaging=jar -Dfile=D:\repository\io\reactivex\rxnetty\0.4.20\rxnetty-0.4.20.jar -DrepositoryId=deploymentRepo -Durl=http://ip:port/xxx/ -DpomFile=D:\repository\io\reactivex\rxnetty\0.4.20\rxnetty-0.4.20.pom
4、上传jar和pom,jar包的名字带有classifier
mvn deploy:deploy-file -DgroupId=com.github.jnr -DartifactId=jffi -Dversion=1.2.15 -Dpackaging=jar -Dclassifier=native -Dfile=D:\repository\com\github\jnr\jffi\1.2.15\jffi-1.2.15-native.jar -DrepositoryId=deploymentRepo -Durl=http://ip:port/xxx/ -DpomFile=D:\repository\com\github\jnr\jffi\1.2.15\jffi-1.2.15.pom
5、只上传jar,jar包的名字带有classifier
mvn deploy:deploy-file -DgroupId=com.github.jnr -DartifactId=jffi -Dversion=1.2.15 -Dpackaging=jar -Dclassifier=native -Dfile=D:\repository\com\github\jnr\jffi\1.2.15\jffi-1.2.15-native.jar -DrepositoryId=deploymentRepo -Durl=http://ip:port/xxx/ -DgeneratePom=false
- packaging:项目的打包方式:jar、war、pom等。
- file:要上传的文件路径,packaging=jar,即jar包的文件路径;packaging=pom,即pom包的文件路径。
- repositoryId:远程仓库ID,与settings.xml中的server中的id保持一致。
- url:远程仓库地址。
- generatePom:是否创建pom文件,建议值为false,最好不要自动生成。
- pomFile:要上传的pom文件路径,若提供了pomFile,则无需再提供generatePom,二者配置一个即可。
- classifier:分类,若依赖jar中存在分类,需要提供,否则无需提供。
5、Java实现本地jar包上传远程仓库
package com.gzp.demo.cmd;
import java.io.File;
public class UploadJar {
/**
* 本地maven仓库根目录名称
*/
private static final String REPOSITORY = "repository";
private static final String SEPARATOR = ":";
private static String jarAndPomCommand = "mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=jar -Dfile=%s -DrepositoryId=xxx -Durl=http://ip:port/xxx -DpomFile=%s";
private static String pomCommand = "mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=pom -Dfile=%s -DrepositoryId=xxx -Durl=http://ip:port/xxx";
private static String classifierJarAndPomCommand = "mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=jar -Dclassifier=%s -Dfile=%s -DrepositoryId=xxx -Durl=http://ip:port/xxx -DpomFile=%s";
private static String classifierJarCommand = "mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=jar -Dclassifier=%s -Dfile=%s -DrepositoryId=xxx -Durl=http://ip:port/xxx -DgeneratePom=false";
/**
* 上传jar包到私服
*
* @param filePath jar包所在目录路径
*/
private static void updateJar(String filePath) {
File file = new File(filePath);
updateJar(file);
}
/**
* 上传jar包到私服
*
* @param file jar包所在目录文件
*/
private static void updateJar(File file) {
// jar文件,如:jffi-1.2.15.jar
String jarFileName = null;
// pom文件,如:jffi-1.2.15.pom
String pomFileName = null;
// classifierJar文件,如:jffi-1.2.15-native.jar
String classifierJarFileName = null;
// classifierJar文件的classifier,如:jffi-1.2.15-native.jar的native
String classifier = null;
String groupId = null;
String artifactId = null;
String version = null;
File[] files = file.listFiles();
assert files != null;
for (File f : files) {
// 文件绝对路径
String absolutePath = f.getAbsolutePath();
// 文件名称
String fileName = f.getName();
// 父文件名
String parentFileName = f.getParentFile().getName();
if (f.isFile()) {
if (fileName.endsWith(".jar")) {
// 如果文件名称的后缀是“父文件名.jar”,说明是jar文件,否则便是classifierJar文件
if (fileName.endsWith(parentFileName + ".jar")) {
jarFileName = absolutePath;
} else {
classifierJarFileName = absolutePath;
classifier = fileName.substring(fileName.lastIndexOf(parentFileName) + parentFileName.length() + 1, fileName.lastIndexOf(".jar"));
}
}
if (fileName.endsWith(".pom")) {
pomFileName = absolutePath;
// 一个依赖包中,pom文件基本上是一定存在的,所以直接在pom文件中获取三要素,防止重复执行逻辑
String threeElements = getThreeElements(absolutePath, fileName);
if (null != threeElements) {
String[] threeElementsArr = threeElements.split(SEPARATOR);
if (threeElementsArr.length == 3) {
groupId = threeElementsArr[0];
artifactId = threeElementsArr[1];
version = threeElementsArr[2];
}
}
}
} else {
updateJar(f);
}
}
// jar文件和pom文件都存在
if (null != jarFileName && null != pomFileName) {
if (null != groupId && null != artifactId && null != version) {
String cmdOrder = String.format(jarAndPomCommand, groupId, artifactId, version, jarFileName, pomFileName);
// System.out.println("jarAndPomCommand:" + cmdOrder);
executeCmd(cmdOrder);
} else {
System.out.println("error: jar:" + jarFileName + ", pom:" + pomFileName);
}
}
// jar文件不存在,classifierJar文件和pom文件都存在
if (null == jarFileName && null != classifierJarFileName && null != classifier && null != pomFileName) {
if (null != groupId && null != artifactId && null != version) {
String cmdOrder = String.format(classifierJarAndPomCommand, groupId, artifactId, version, classifier, classifierJarFileName, pomFileName);
// System.out.println("classifierJarAndPomCommand:" + cmdOrder);
executeCmd(cmdOrder);
} else {
System.out.println("error: classifierJar:" + classifierJarFileName + ", pom:" + pomFileName);
}
}
// jar文件、classifierJar文件和pom文件都存在
if (null != jarFileName && null != classifierJarFileName && null != classifier && null != pomFileName) {
if (null != groupId && null != artifactId && null != version) {
String cmdOrder = String.format(classifierJarCommand, groupId, artifactId, version, classifier, classifierJarFileName);
// System.out.println("classifierJarAndPomCommand:" + cmdOrder);
executeCmd(cmdOrder);
} else {
System.out.println("error: classifierJar:" + classifierJarFileName + ", pom:" + pomFileName);
}
}
// jar文件和classifierJar文件都不存在,仅pom文件都存在
if (null == jarFileName && null == classifierJarFileName && null != pomFileName) {
if (null != groupId && null != artifactId && null != version) {
String cmdOrder = String.format(pomCommand, groupId, artifactId, version, pomFileName);
// System.out.println("pomCommand:" + cmdOrder);
executeCmd(cmdOrder);
} else {
System.out.println("error: pom:" + pomFileName);
}
}
}
/**
* 获取三要素
*
* @param jarPath jar包路径
* @param jarName jar包文明
* @return 三要素
*/
private static String getThreeElements(String jarPath, String jarName) {
try {
String groupId = null;
String artifactId = null;
String version = null;
jarPath = jarPath.substring(jarPath.indexOf(REPOSITORY) + REPOSITORY.length() + 1, jarPath.lastIndexOf(jarName) - 1);
String[] jarPathArr = jarPath.split("\\\\");
version = jarPathArr[jarPathArr.length - 1];
artifactId = jarPathArr[jarPathArr.length - 2];
jarPath = jarPath.substring(0, jarPath.lastIndexOf(artifactId) - 1);
groupId = jarPath.replaceAll("\\\\", ".");
return groupId + SEPARATOR + artifactId + SEPARATOR + version;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 执行cmd命令
*
* @param cmdOrder cmd命令
*/
private static void executeCmd(String cmdOrder) {
String execResult = CmdUtils.executeCmd(cmdOrder, null);
if (execResult.contains("BUILD FAILURE")) {
System.out.println("执行失败:" + cmdOrder);
} else {
System.out.println("执行成功:" + cmdOrder);
}
}
public static void main(String[] args) {
String filePath = "D:\\repository";
updateJar(filePath);
}
}
更多推荐




所有评论(0)