背景:筆者所在公司經歷了三次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 發布!
架構設計@工程設計@服務穩定性之路