背景:笔者所在公司经历了三次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

  1. 新建需要升级的项目工程的分支
  2. 构建升级jar包,放到项目空间目录中,与其他工程项目同级
  3. 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 发布!

架构设计@工程设计@服务稳定性之路

通过jgit一次性升级fastjson版本的更多相关文章

  1. 非关系型数据库来了,CRL快速开发框架升级到版本4

    轮子?,我很任性,我要造不一样的轮子,同时支持关系型和非关系型的框架有没有 新版数据查询作了些调整,抽象了LabmdaQueryy和DBExtend,升级到版本4,非关系数据库MongoDB被支持了! ...

  2. ubuntu下升级R版本

    ubuntu下升级R版本   在测试<机器学习 实用案例解析>一书的邮件分类代码时,windows系统下rstudio中无法读取特殊字符,在ubuntu下可以.在ubuntu虚拟机下安装t ...

  3. Mac中使用port升级gcc版本

    Mac OS中的gcc版本可能不会满足实际使用要求,需要对其升级. 这里介绍使用port方式来升级gcc版本.Macports是Mac OS中的软件包管理工具. 首先,安装Macports 这里提供O ...

  4. 如何升级Ceph版本及注意事项

    升级软件版本在日常运维中是一个常见操作. 本文分享一下Ceph版本升级的一些经验. 一般升级流程和注意如下: 1.  关注社区Release notes 和 ceph-user邮件订阅列表,获取社区发 ...

  5. Windows2000安装Winform Clickonce提示升级系统版本的解决方案

    Windows2000安装Winform Clickonce提示升级系统版本.只需要把所有应用的DLL的独立性设置为false就可以了.

  6. wdcp升级php版本到5.3,5.5

    官网省级方法 wget http://down.wdlinux.cn/in/php_up53.shsh php_up53.sh 看到"php update is OK"提示表示,顺 ...

  7. PHPNow升级PHP版本为5.3.5的方法

    在WIN上有时候需要测试一些PHP程序,又不会自行独立配置环境,那么PHPNow是非常好的选择,这篇文章主要为大家分享下如果将phpnow的php版本升级为5.3.5   在WIN上有时候需要测试一些 ...

  8. PHPNow升级PHP版本的方法

    在WIN上有时候需要测试一些PHP程序,又不会自行独立配置环境,那么PHPNow是非常好的选择. PHPNow自带的PHP版本为5.2.14,而最后一次更新在于2010-9-22,PHP5.2对于现在 ...

  9. Linux(Fedora)下NodeJs升级最新版本(制定版本)

    Linux(Fedora)下NodeJs升级最新版本(制定版本) 首先安装n模块: npm install -g n 升级node.js到最新稳定版 n stable 升级node.js到制定版本 n ...

随机推荐

  1. 拿到外包公司的offer,我要去么?

    引言: 前一阵子有一个帖子引起了非常广泛的讨论,描述的就是一个公司的外包工作人员,加班的时候因为吃了公司给员工准备的零食,被公司的HR当场批评!这个帖子一发出来,让现在测试行业日益新增的外包公司备受关 ...

  2. Label自适应高度的用法及设置倒角

    UILabel *label = [[UILabel alloc] init]; //根据内容动态计算label的高度 label.text = @"Sent when the applic ...

  3. 聊聊 Python 的内置电池

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/XzCqoCvcpFJt4A-E4WMqaA (一) 最近,我突然想到一 ...

  4. 记录一次创建.net core 项目 并且发布到docekr【完全新手入门】

    1]环境说明 操作系统:Window 10 专业版 开发工具 Vs2019专业版 Docker:  Docker for Windows  2]创建.net core项目并且发布 2.0先打开并且运行 ...

  5. 使用GDAL/GEOS求面特征的并集

    存在这样一个示例的矢量文件,包含了两个重叠的面特征: 一个很常见的需求是求取这个矢量中所有面元素的并集,通过GDAL/GEOS很容易实现这个功能,具体代码如下: #include <iostre ...

  6. iOS---OBJC_ASSOCIATION_ASSIGN可能引起的Crash

    //OBJC_ASSOCIATION_ASSIGN类似于我们常用的assign,assign策略的特点就是在对象释放以后,不会主动将应用的对象置为nil,这样会有访问僵尸对象导致应用崩溃的风险.为了解 ...

  7. DG重启之后主备数据不同步

    问题描述:本来配置好的DG第二天重启之后,发现主备库数据不能同步,在主库上执行日志切换以及创建表操作都传不到备库上,造成这种错误的原因是主库实例断掉后造成备库日志与主库无法实时接收 主库:orcl  ...

  8. Java_垃圾回收算法

    参考:<深入理解JAVA虚拟机>第二版 3.3 垃圾收集算法 由于垃圾收集算法的实现涉及大量的程序细节,而且各个平台的虚拟机操作内存的方法又各不相同,只是介绍几种算法的思想及其发展过程. ...

  9. CentOS 7上的进程管理

    一些杂乱的基础概念 程序是一种静态的文件,躺在磁盘上.而进程则是将程序运行起来放置于内存中.因此进程就是运行中的程序,是程序运行起来的一个实例.同一个程序可以运行为多个进程/实例. 进程之间有父子关系 ...

  10. 欧洲杯在即英超yabo055红单介绍!沃特福德vs曼彻斯特联 沃特福德雪上加霜

    北京时间12月22日22:00,2019-20赛季英超联赛第18轮打响,沃特福德主场迎战曼彻斯特联.本场曼联作客一步步得到支持,球队有望客场赢下比赛. [基本面分析] 1.2019-20赛季英超联赛第 ...