简述:
每一次发布版本全量升级太慢,Java实现一个全量pom版本替换的程序
输入参数:
- 原项目的根路径
- 老的tag版本
- 新的tag版本
注意点:
使用内存从文件读取字符串,然后老-新tag替换,在替换前需要清空源文件
这里使用FileChannel的成员方法truncate(0), 清空文件
fc.truncate(0);
代码:
ReplaceRobot.java
package com.anialy.test.pomreplace;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
public class ReplaceRobot {
public static void main(String[] args) {
new ReplaceRobot(
/*原文件夹*/ "C:/Users/Administrator/Desktop/workspace_tca_web/tca-v2"
/*原tag*/ ,"20140926H"
/*新tag*/ ,"20140926I-SNAPSHOT"
).process();
}
private String basePath;
private String prevVersion;
private String curVersion;
private String replaceFileName = "pom.xml";
private List<String> relativeFiles = new ArrayList<String>();
public ReplaceRobot(String basePath,
String prevVersion, String curVersion) {
this.basePath = basePath;
this.prevVersion = prevVersion;
this.curVersion = curVersion;
// 相对路径存放
relativeFiles.add("");
relativeFiles.add("/components");
relativeFiles.add("/plugins");
relativeFiles.add("/infrastucture");
relativeFiles.add("/server");
relativeFiles.add("/components/circle");
relativeFiles.add("/components/component-system");
relativeFiles.add("/components/core");
relativeFiles.add("/components/invoker-service");
relativeFiles.add("/components/mail-service");
relativeFiles.add("/plugins/plugin-common");
relativeFiles.add("/plugins/plugin-operation");
relativeFiles.add("/plugins/plugin-resource");
relativeFiles.add("/plugins/plugin-topic");
relativeFiles.add("/plugins/plugin-train");
}
public void process(){
for(String filePath : relativeFiles){
// pom文件的上级目录
File file = new File(basePath + filePath);
// 抓取所有pom文件
File[] files = file.listFiles(new FileFilter() {
public boolean accept(File subFile) {
String fileName = subFile.getAbsolutePath();
// 处理pom文件
if(subFile.getName().contains(replaceFileName)){
try {
processReplaceFile(fileName);
} catch (Exception e) {}
return true;
}
return false;
}
});
}
}
/**
* 文件读入内存后替换字符串prevVersion -> curVersion
* @param filePath
* @throws Exception
*/
private void processReplaceFile(String filePath) throws Exception{
File file = new File(filePath);
StringBuilder sbd = new StringBuilder();
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String line;
while ((line = br.readLine()) != null) {
sbd.append(line + "\n");
}
String rawText = sbd.substring(0, sbd.length()-1);
String newText = rawText.replace(prevVersion, curVersion);
outputText(newText, file);
}
/**
* 从内存将修改过的文件回写到磁盘
* @param text
* @param file
* @throws IOException
* @throws ClassNotFoundException
*/
private void outputText(String text, File file)
throws IOException, ClassNotFoundException{
FileChannel fc = new RandomAccessFile(file.getAbsoluteFile(), "rw")
.getChannel();
// 清空文件
fc.truncate(0);
// 从内存写回文件
byte[] outputBytes = text.getBytes("UTF-8");
long bytesNum = outputBytes.length;
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_WRITE, 0, bytesNum);
bb.put(outputBytes);
fc.close();
}
}