通过jgit一次性升级fastjson版本
程序员文章站
2022-10-25 23:07:04
背景:笔者所在公司经历了三次fastjson的升级,由于集群,工程数量众多,每次升级都很麻烦。因此开发了一个java的升级工具。 功能介绍: 功能介绍:一个jar文件,通过java jar命令,输入用户名,密码,所负责的git项目主目录,即可对所负责的本地工作区目录下的项目工程中的pom.xml文件 ......
背景:笔者所在公司经历了三次fastjson的升级,由于集群,工程数量众多,每次升级都很麻烦。因此开发了一个java的升级工具。
功能介绍:
功能介绍:一个jar文件,通过java -jar命令,输入用户名,密码,所负责的git项目主目录,即可对所负责的本地工作区目录下的项目工程中的pom.xml文件进行检测,然后对其依赖的低版本fastjson直接升级到最新版本。
pom依赖:
<dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.61</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupid>org.eclipse.jgit</groupid> <artifactid>org.eclipse.jgit</artifactid> <version>5.1.3.201810200350-r</version> <optional>true</optional> </dependency> <dependency> <groupid>org.eclipse.jgit</groupid> <artifactid>org.eclipse.jgit.archive</artifactid> <version>4.11.0.201803080745-r</version> </dependency> <dependency> <groupid>com.jcraft</groupid> <artifactid>jsch</artifactid> <version>0.1.54</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <plugin> <artifactid> maven-assembly-plugin </artifactid> <configuration> <descriptorrefs> <descriptorref>jar-with-dependencies</descriptorref> </descriptorrefs> <archive> <manifest> <mainclass>com.daojia.qypt.mvnpom.up.gitutils</mainclass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
工具包:
package com.qy.mvnpom.up; import org.eclipse.jgit.api.git; import org.eclipse.jgit.api.listbranchcommand; import org.eclipse.jgit.lib.ref; import org.eclipse.jgit.transport.credentialsprovider; import org.eclipse.jgit.transport.httpconfig; import org.eclipse.jgit.transport.usernamepasswordcredentialsprovider; import java.io.file; import java.nio.charset.charset; import java.nio.file.files; import java.nio.file.paths; import java.util.arraylist; import java.util.hashset; import java.util.list; import java.util.set; /** * @author fanchunshuai * @date 2019/6/27 14 * @description: */ public class gitutils { public static git getgit(string uri, credentialsprovider credentialsprovider, string localdir) throws exception { git git = null; if (new file(localdir).exists()) { git = git.open(new file(localdir)); } else { git = git.clonerepository().setcredentialsprovider(credentialsprovider).seturi(uri) .setdirectory(new file(localdir)).call(); } //设置一下post内存,否则可能会报错error writing request body to server git.getrepository().getconfig().setint(httpconfig.http, null, httpconfig.post_buffer_key, 512 * 1024 * 1024); return git; } public static credentialsprovider getcredentialsprovider(string username, string password) { return new usernamepasswordcredentialsprovider(username, password); } //切换分支 public static void checkoutbranch(git git, string localpath, string branchname) { string projecturl = localpath + "\\.git"; try { git.checkout().setcreatebranch(true).setname(branchname).call(); git.pull().call(); system.out.println("切换分支成功"); } catch (exception e) { system.out.println("已是当前分支,不需要切换!"); } finally { if (git != null) { git.close(); } } } public static void main(string[] args) throws exception { if(args.length<2){ system.out.println("请输入git用户名&密码!"); return; } //获取输入参数 string username = args[0]; string password = args[1]; //远程项目的git用户组,如http://github.com/fanchunshuai/ 用于从远程下载切换分支代码 string uri = args[2]; system.out.println("用户名:"+username+",密码:*********"); credentialsprovider credentialsprovider = getcredentialsprovider(username, password); //将此工具类的jar文件放到本地所有项目的目录下,与项目代码目录平级 string localdir = system.getproperty("user.dir"); system.out.println("当前项目空间:"+localdir); file file = new file(localdir); set<string> checkset = new hashset<>(); for (file childfile : file.listfiles()){ //检查当前项目空中是否有git文件 file [] filex = childfile.listfiles(); if(filex == null || filex.length == 0){ continue; } boolean b = false; for (file file1 : filex){ if(file1.getname().endswith("git")){ b = true; } } if(!b){ string url = uri+childfile.getname()+".git"; system.out.println("url = "+url+" 不在远程git项目里,忽略"); continue; } string url = uri+childfile.getname()+".git"; git git = getgit(url, credentialsprovider, childfile.getabsolutepath()); list<ref> call = git.branchlist().setlistmode(listbranchcommand.listmode.all).call(); int max = 0; string maxbranchs = ""; //分支格式校验 for (ref ref : call) { string branchname = ref.getname(); if (branchname.contains("-") && branchname.contains("refs/remotes/origin") && branchname.contains("_8-")) { string[] brancharray = branchname.split("_"); if(brancharray.length < 3){ system.out.println(branchname+"不是标准的分支格式"); continue; } if(brancharray[2].split("-").length < 3){ system.out.println(branchname+"不是标准的分支格式"); continue; } string lastnumber = brancharray[2].split("-")[2]; if (max < integer.parseint(lastnumber)) { max = integer.parseint(lastnumber); maxbranchs = branchname; } } } if(maxbranchs.equals("") || maxbranchs == null){ maxbranchs = "refs/remotes/origin/dev/"+childfile.getname()+"_8-0-0"; } string newbranch = maxbranchs.replace("refs/remotes/origin/", ""); system.out.println("需要修复的分支是:" + newbranch); checkoutbranch(git, localdir, newbranch); for (file childfile2 : childfile.listfiles()) { string path; if (childfile2.isdirectory()) { path = getpompath(childfile2.getabsolutepath()); if (path == null || path.equals("")) { continue; } } else if (childfile2.isfile() && childfile2.getname().equals("pom.xml")) { path = childfile2.getabsolutepath(); } else { continue; } list<string> lines; try { lines = files.readalllines(paths.get(path), charset.forname("utf-8")); system.out.println("pom.xml文件格式为utf-8........"); }catch (exception e){ lines = files.readalllines(paths.get(path), charset.forname("gbk")); system.out.println("pom.xml文件格式为gbk........"); } if(lines.size()<=10){ continue; } int i; stringbuilder builder = new stringbuilder(); string newversionline = ""; int newindex = 0; boolean haveto = false; for (i = 0; i < lines.size(); i++) { string line = lines.get(i); if (line.contains("groupid") && line.contains("com.alibaba")) { string artifactidline = lines.get(i + 1); builder.append(line + "\n"); if (artifactidline.contains("artifactid") && artifactidline.contains("fastjson")) { string versionline = lines.get(i + 2); if (versionline.contains("version")) { string[] linearry = versionline.split("\\."); //此处进行替换 newversionline = linearry[0] + "." + linearry[1] + ".60</version>"; newindex = i + 2; haveto = true; } } } else { if (newindex > 0 && newindex == i) { builder.append(newversionline + "\n"); newindex = 0; continue; } else { if (i == lines.size() - 1) { builder.append(line); } else { builder.append(line + "\n"); } } } } if(!haveto){ checkset.add(newbranch); continue; } files.write(paths.get(path), builder.tostring().getbytes()); git.add().addfilepattern("pom.xml").call(); //提交 git.commit().setmessage("update fastjson to 1.2.60 auto by qypomup.jar").call(); //推送到远程 //推送 git.push().setcredentialsprovider(credentialsprovider).call(); system.out.println("恭喜,自助升级fastjson&代码提交完成!升级分支为:"+newbranch); } } if(checkset.size()>0){ checkset.foreach(str->{ system.out.println("当前分支["+str+"]中的pom.xml文件中没有包含fastjson依赖,请手工检查!"); }); } } private static string getpompath(string localpath) { file file = new file(localpath); file[] filearray = file.listfiles(); if (filearray == null || filearray.length == 0) { return ""; } for (file file1 : filearray) { if (file1.isdirectory()) { getpompath(file1.getabsolutepath()); } else { if (file1.isfile() && file1.getname().equals("pom.xml")) { return file1.getabsolutepath(); } } } return ""; } }
通过pom依赖和工具类构件maven jar
- 新建需要升级的项目工程的分支
- 构建升级jar包,放到项目空间目录中,与其他工程项目同级
- cmd打开命令行cd到项目空间目录中,执行命令
java -jar \pomup.jar username password http://github.com/username/xxxx
说明:pomup.jar:构建的jar包名称
username,password :git的用户名,密码
http://github.com/username/xxxx:整体的git项目空间
本文由博客一文多发平台 openwrite 发布!
架构设计@工程设计@服务稳定性之路
上一篇: 随机输入两位数,并将其交换位置输出
下一篇: 水果罐头怎么开,让你成为女汉子