一:如何实现SVN导出某段时间修改过的文件
有时候修改代码需要增量部署,只替换某段时间修改过的文件,改代码时记录也行,问题是你能保证不遗漏,保证头不晕,还喜欢重复这种动作吗,部署服务器你也知道部错一个文件得来回启,来回找?那怎么做比较好呢?其实用SVN是可以导出自己某段时间自己修改过的文件的,只需要java简单处理就能得到文件路径了。(其实要进一步的话,可以让这种增量部署自动化也是有可能的)希望也能分享您的方法。
步骤:
1.右键整个项目根,用SVN显示资源历史记录,会发现会显示所有开发者提交的记录信息。默认应该是按时间逆序排列的。
2.然后点击查找历史进入条件匹配。
3.查询条件包括作者,注释,起止日期,版本号等,比如我按提交者账号是“gongxinglin”的账号查询&&按提交日期是17-3-1至17-3-16期间日期的。
4.确定查询后得到的结果就都是该账号的记录了。
5.全选所有你需要的日志记录右键就会提示导出svn相关的日志记录了。
6.导出的文件格式默认名称是:changeLog.txt
7.发现文件格式还是比较有规律的,然后用java读取处理就可能得到不重复的文件路径了。
下面是一个测试的java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;
public class svn_log_test {
public static void main(String[] agrs){
Set<String> pathSet = new HashSet<String>();
try {
String changeLogPath = "d://changeLog.txt";
FileReader reader = new FileReader(changeLogPath);
BufferedReader br = new BufferedReader(reader);
String str = null;
StringBuffer sb= new StringBuffer();
while((str = br.readLine()) != null) {
sb.append(str+"/n");
System.out.println(str);
if(str.indexOf("M /") > 0){
pathSet.add(str);
}
}
br.close();
reader.close();
}catch (Exception e) {
System.out.println(e);
}
System.out.println("影响的路径集合是:");
for(String path : pathSet){
System.out.println(path);
}
}
}
下面是实际使用的java类:支持排除某些用户的重复文件
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class svn_log_test {
/**
* 1.由svn路径找到要部署的jsp和class文件。
* 2.按部署的目录结构复制需要部署替换的文件。
* 3.其他文件按照原来的目录结构进行组织处理,其实除了java文件外,其他应该都可以按其他文件处理。
* 使用时直接拖拽整个目录到对应的路径即可。
*
* @param agrs
*/
public static void main(String[] agrs){
String excluChangeLogPath = "d://auto_class_file/changeLog_mjl.txt";//排出的svn日志信息
Set<String> exclupathSet = getSvnLogPathSet(excluChangeLogPath);
String changeLogPath = "d://auto_class_file/changeLog.txt";
Set<String> finalPathSet = new HashSet<String>();//最终文件集合
Set<String> pathSet = getSvnLogPathSet(changeLogPath);
System.out.println("原文件数:"+pathSet.size());
Iterator<String> pathIt = pathSet.iterator();
while (pathIt.hasNext()) {
String path = pathIt.next();
if(exclupathSet.contains(path)){
System.out.println("排除的冲突文件:"+path);
}
finalPathSet.add(path);
}
System.out.println("排除后文件数:"+finalPathSet.size());
extractClassFile(finalPathSet);
}
public static Set<String> getSvnLogPathSet(String changeLogPath){
Set<String> pathSet = new HashSet<String>();
try {
FileReader reader = new FileReader(changeLogPath);
BufferedReader br = new BufferedReader(reader);
String str = null;
while((str = br.readLine()) != null) {
if(str.indexOf("M /") > 0){
pathSet.add(str);
}
}
br.close();
reader.close();
}catch (Exception e) {
System.out.println(e);
}
return pathSet;
}
public static final String deployDestPath = "d:/auto_class_file/";//部署目录
public static final String PROJECT_ROOT = "C:/VenusTools2010/workspace/BOSP_2v2";//项目根路径
public static final String svnRoot = "M /trunk/bosp_3";//svn公共路径
public static final String classPath = PROJECT_ROOT +"/bosp/WEB-INF/classes/";//class文件起始目录
public static final String svn_classPath = svnRoot +"/src/";//解析svn文件java路径
public static void extractClassFile(Set<String> pathSet){
String projectRoot = "bosp";//项目root名称
int copyFileNum = 0;//处理文件数
int copyClassFileNum = 0;
int otherFileNum = 0;
for(String path : pathSet){
if(path.indexOf(svn_classPath) > -1){
String destPath = classPath + path.replaceAll(svn_classPath, "").replaceAll("java", "class").trim();//class文件
if(null != destPath && destPath.length() > 0){
File classFile = new File(destPath);
if(classFile.exists()){
String classFileName = classFile.getName().replaceAll(".class", "");
File parentFile = classFile.getParentFile();
if(parentFile.exists() && parentFile.isDirectory()){
File[] fileList = parentFile.listFiles();
if(null != fileList && fileList.length > 0){
for(File fileTemp :fileList){
if(fileTemp.getName().indexOf(classFileName) > -1){
//找到所有的class文件及class相关的内部类文件
//复制到指定目录
String toClassFile = deployDestPath + fileTemp.getAbsolutePath().substring(fileTemp.getAbsolutePath().indexOf(projectRoot));
copyFileNum++;
System.out.println(copyFileNum +"*"+fileTemp.getName().substring(fileTemp.getName().lastIndexOf(".")));
copyFile(new File(toClassFile),fileTemp);
copyClassFileNum ++;
}
}
}
}
}
}
}else{
path = path.replaceAll(svnRoot, "").trim();
String destPath = PROJECT_ROOT + path;
File otherFile = new File(destPath);
if(otherFile.exists()){
String toOtherFile = null;
if(otherFile.getAbsolutePath().indexOf("WEB-INF") > -1){
//放到classes目录下
toOtherFile = deployDestPath + projectRoot + File.separator + otherFile.getAbsolutePath().substring(otherFile.getAbsolutePath().indexOf("WEB-INF"));//TODO 到指定目录。
}else{
toOtherFile = deployDestPath + projectRoot + File.separator + otherFile.getAbsolutePath().substring(otherFile.getAbsolutePath().indexOf(projectRoot));//TODO 到指定目录。
}
copyFile(new File(toOtherFile),otherFile);
copyFileNum ++;
otherFileNum ++;
}
System.out.println("*其他文件:"+path);
}
}
System.out.println("执行完毕。总源文件数:"+pathSet.size()+" 部署的class文件数:"+copyClassFileNum+" 部署的其他文件数:"+otherFileNum + " 部署的文件总数:"+copyFileNum);
}
/**
* 覆盖复制文件到指定目录
* @param toFile
* @param fromFile
*/
public static void copyFile(File toFile, File fromFile) {
createFile(toFile, true);
System.out.println("复制文件" + fromFile.getAbsolutePath() + "到"
+ toFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(fromFile);
FileOutputStream fos = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
fos.write(buffer);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建复制的文件目录或文件
* @param file
* @param isFile
*/
public static void createFile(File file, boolean isFile) {
if ((!file.exists()) && (isFile)) {
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二:将整理后的class增量文件上传到服务器指定目录。
如果使用pscp上传文件是比较方便的,下面封装了相应的自动上传方法:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
public class pscp_test {
/**
* @param args
*/
public static void main(String[] args) {
//上传
/* String localFilePath = "D:/auto_class_file/changeLog.txt";//上传的文件路径
String remoteDestAbsPath = "/root";//linux文件目标目标绝对路径
uploadFile(localFilePath,remoteDestAbsPath);*/
//下载
String remoteFilePath = "/root/hadoop-2.7.3.tar.gz";
String fileType = "";
String localPath = "D:/auto_class_file/";
getFile(remoteFilePath,fileType,localPath);
}
/**
* 上传windows本地文件或目录(已支持目录)
*
* @param localFilePath
* @param remoteDestAbsPath
*/
public static void uploadFile(String localFilePath,String remoteDestAbsPath){
if(null != localFilePath && null != remoteDestAbsPath){
File localFile = new File(localFilePath);
String pscpPath = "D:/auto_class_file/pscp.exe";//pscp.exe命令路径
String commandStr = null;
if(localFile.exists()){
if(localFile.isFile()){
commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin "+localFilePath+" root@192.168.56.100:"+remoteDestAbsPath;
}else{
commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin -r "+localFilePath+" root@192.168.56.100:"+remoteDestAbsPath;
}
execCommand(commandStr);
}
}
}
/**
* 下载远程服务器文件
* @param remoteFilePath
* @param fileType
* @param localPath
*/
public static void getFile(String remoteFilePath,String fileType,String localPath){
if(null != remoteFilePath && null != localPath){
File localFile = new File(localPath);
String pscpPath = "D:/auto_class_file/pscp.exe";//pscp.exe命令路径
String commandStr = null;
if(localFile.exists()){
if(localFile.isDirectory()){
if(fileType.equals("d")){
commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin -r root@192.168.56.100:"+remoteFilePath+" "+localPath;
}else{
commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin root@192.168.56.100:"+remoteFilePath+" "+localPath;
}
execCommand(commandStr);
}else{
System.out.println("本地路径为非目录。");
}
}
}
}
//执行命令
public static void execCommand(String commandStr){
if(null != commandStr && commandStr.length() > 0){
try {
Runtime run = Runtime.getRuntime();
Process process = run.exec(commandStr);
InputStream input = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String szline;
while ((szline = reader.readLine()) != null) {
System.out.println(szline);
}
reader.close();
process.waitFor();
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("执行完毕。");
}
}