批量上传 jar 包到远程 maven 仓库
mvn deploy命令:
mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=log-lib-1.1.jar -DgroupId=com.suncreate -DartifactId=log-lib -Dversion=1.1 -Dpackaging=jar -Durl=https://packages.aliyun.com/maven/repository/xxxxxxxxx -DrepositoryId=rdc-releases --settings D:\Java\maven-repository\settings.xml
mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=hadoop-common-3.1.1.jar -DgroupId=org.apache.hadoop -DartifactId=hadoop-common -Dversion=3.1.1 -Dclassifier=huawei -Dpackaging=jar -Durl=https://packages.aliyun.com/maven/repository/xxxxxxxxx -DrepositoryId=rdc-releases --settings D:\Java\maven-repository\settings.xml
注意:-Dfile不是绝对路径
工具代码思路:遍历本地maven仓库文件夹中的.jar文件,通过正则表达式匹配获取jar包的groupId、artifactId、version、文件名等信息,通过调用cmd命令实现上传
工具是用C#写的,代码:
private void mavenUploads() { Task.Run(() => { try { //string classifier = string.Empty; string classifier = "huawei"; string[] files = Directory.GetFiles(@"D:\Java\maven-repository", "*" + classifier + ".jar", SearchOption.AllDirectories); Log("文件总数:" + files.Length.ToString()); Regex regex = new Regex(@"^D:\\Java\\maven-repository\\([\\a-zA-Z0-9-]+)\\([a-zA-Z0-9-_\.]+)\\([0-9][a-zA-Z0-9-_\.]*)\\([a-zA-Z0-9-_\.]+" + classifier + @"\.jar)$", RegexOptions.IgnoreCase); int count = 0; foreach (string file in files) { Match m = regex.Match(file); if (m.Success) { string dgroupId = m.Groups[1].Value.Replace("\\", "."); string dartifactId = m.Groups[2].Value; string dversion = m.Groups[3].Value; string fileName = m.Groups[4].Value; string tempFileName = fileName; if (fileName.Contains("-sources.jar")) { continue; } if (fileName.Contains("-javadoc.jar")) { continue; } if (!string.IsNullOrWhiteSpace(classifier)) { tempFileName = @"D:\temp\" + fileName.Replace("-" + classifier + ".jar", ".jar"); } if (!File.Exists(tempFileName)) { File.Copy(file, tempFileName, true); bool result = mavenUpload(tempFileName, dgroupId, dartifactId, dversion, classifier); if (result) { count++; Log("已成功上传,count=" + count + ",文件名:" + tempFileName); } else { count++; Log("上传失败,count=" + count + ",文件名:" + tempFileName); } } else { count++; Log("无需重复上传,count=" + count + ",文件名:" + file); } } else { count++; Log("不匹配已跳过,count=" + count + ",文件名:" + file); } } Log("完成"); } catch (Exception ex) { Log("出错:" + ex.Message + "\r\n" + ex.StackTrace); } }); } private bool mavenUpload(string dfile, string dgroupId, string dartifactId, string dversion, string dclassifier) { if (!string.IsNullOrWhiteSpace(dclassifier)) { dclassifier = "-Dclassifier=" + dclassifier; } else { dclassifier = string.Empty; } string cmdStr = string.Format(@" mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile={0} -DgroupId={1} -DartifactId={2} -Dversion={3} {4} -Dpackaging=jar -Durl=https://packages.aliyun.com/maven/repository/xxxxxxxxx -DrepositoryId=rdc-releases --settings D:\Java\maven-repository\settings.xml", dfile, dgroupId, dartifactId, dversion, dclassifier); cmdStr = cmdStr.Replace("\r\n", ""); bool result = runCmd(cmdStr); return result; } private bool runCmd(string cmdStr) { Process p = new Process(); //设置要启动的应用程序 p.StartInfo.FileName = "cmd.exe"; //是否使用操作系统shell启动 p.StartInfo.UseShellExecute = false; //接受来自调用程序的输入信息 p.StartInfo.RedirectStandardInput = true; //输出信息 p.StartInfo.RedirectStandardOutput = true; //输出错误 p.StartInfo.RedirectStandardError = true; //不显示程序窗口 p.StartInfo.CreateNoWindow = true; //启动程序 p.Start(); //向cmd窗口发送输入信息 p.StandardInput.WriteLine(cmdStr); p.StandardInput.WriteLine("exit"); p.StandardInput.AutoFlush = true; //获取输出信息 string strOuput = p.StandardOutput.ReadToEnd(); //等待程序执行完退出进程 p.WaitForExit(); p.Close(); if (strOuput.Contains("ERROR")) { Log(strOuput); return false; } else { return true; } }