Java 操作svn 使用svnkit进行新增上传、更新、删除文件等操作
最近在做svn集成开发,所以记录一下。
用到了svnkit进行二次开发,达到项目需求。
官网:https://svnkit.com/
api:https://svnkit.com/javadoc/index.html
只写了以下几点:
1.新建文件夹
2.增加文件
3.修改文件
4.删除文件 或文件夹
有需要的可以根据自己的项目需求进行修改
下面直接上代码
package com.ruoyi.web.controller.demo.controller;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class SvnKitUtils {
private static SVNRepository svnRepository;
private static long revisionNo = -1; //指定版本号为最新版本
private static ISVNEditor editor;
/**
* 验证登录
* @param url svn仓库地址
* @param username 用户名称
* @param password 密码
* @throws SVNException
*/
public SvnKitUtils(String url,String username,String password) throws SVNException {
char[] pwd = password.toCharArray();
svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd);
svnRepository.setAuthenticationManager(isvnAuthenticationManager);
}
/**
* 新增文件
* @param revisionNo
* @param dirPath 文件路径
* @param fileName 文件名称
* @param data
* @return
* @throws SVNException
*/
private static SVNCommitInfo addFile(ISVNEditor editor,long revisionNo, String dirPath , String fileName , byte[] data) throws SVNException{
editor.openRoot(revisionNo);
editor.openDir(dirPath,revisionNo);
editor.addFile(fileName,null,revisionNo);
editor.applyTextDelta(fileName,null);
SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
String checksum = deltaGenerator.sendDelta( fileName , new ByteArrayInputStream( data ) , editor , true );
editor.closeFile(fileName, checksum);
editor.closeDir();
editor.closeDir();
return editor.closeEdit();
}
/**
* 新建文件夹
* @param editor
* @param revisionNo
* @param dirPath
* @param fileDirName
* @return
* @throws SVNException
*/
@SuppressWarnings("unused")
private static SVNCommitInfo addDir(ISVNEditor editor,long revisionNo, String dirPath , String fileDirName) throws SVNException{
editor.openRoot(revisionNo);
editor.openDir(dirPath,revisionNo);
editor.addDir(fileDirName, null, revisionNo);
editor.closeDir();
editor.closeDir();
return editor.closeEdit();
}
/**
* 修改文件
* @param dirPath 文件路径 /svn
* @param fileName 文件路径+文件名称 如/svn/测试.txt
* @param oldData
* @param newData
* @return
* @throws SVNException
*/
private static SVNCommitInfo modifyFile(ISVNEditor editor,String dirPath , String fileName , byte[] oldData , byte[] newData) throws SVNException {
editor.openRoot( -1 );
editor.openDir( dirPath , -1 );
editor.openFile( fileName , -1 );
editor.applyTextDelta( fileName , null );
SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator( );
String checksum = deltaGenerator.sendDelta( fileName , new ByteArrayInputStream( oldData ) , 0 , new ByteArrayInputStream( newData ) , editor , true );
editor.closeFile( fileName , checksum );
editor.closeDir( );
editor.closeDir( );
return editor.closeEdit( );
}
/**
* 删除文件
* @param editor
* @param dirPath 要删除的文件路径
* @param revisionNo
* @return
* @throws SVNException
*/
private static SVNCommitInfo deleteFile(ISVNEditor editor,String dirPath,long revisionNo) throws SVNException{
// 进入Root节点
editor.openRoot(revisionNo);
//4.3.删除文件
editor.deleteEntry(dirPath,revisionNo);
//操作完成要关闭编辑器,并返回操作结果
return editor.closeEdit();
}
public byte[] file2Byte(String filePath) {
byte[] fileBytes = null;
FileInputStream fis = null;
try {
File file = new File(filePath);
fis = new FileInputStream(file);
fileBytes = new byte[(int) file.length()];
fis.read(fileBytes);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileBytes;
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
String url = "http://localhost:81/repo/";
String username = "用户名称";
String password = "密码";
//1.新建文件夹
try {
SvnKitUtils svn = new SvnKitUtils(url, username, password);
ISVNEditor editor = svn.svnRepository.getCommitEditor("新建文件夹",null,true,null);
SVNCommitInfo svnCommitInfo = svn.deleteFile(editor, "/snv/开发类/a/itemC1.txt", revisionNo);
System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
} catch (SVNException e) {
e.printStackTrace();
}
//2.增加文件
// try {
// SvnKitUtils svn = new SvnKitUtils(url, username, password);
// byte[] newData = "测试-----测试-----22222222222".getBytes();
// ISVNEditor editor = svn.svnRepository.getCommitEditor("增加文件",null,true,null);
svn.addFile(editor,revisionNo, "/snv/开发类/a", "itemC1.txt", newData);
// System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
// } catch (SVNException e) {
// e.printStackTrace();
// }
//3.修改文件
// try {
// SvnKitUtils svn = new SvnKitUtils(url, username, password);
// byte[] oldData = "测试-----测试-----22222222222".getBytes();
// byte[] newData = "测试-----测试-----33333333333".getBytes();
// ISVNEditor editor = svn.svnRepository.getCommitEditor("修改文件",null,true,null);
svn.modifyFile(editor, "/snv/开发类/a", "/snv/开发类/a/itemC1.txt", oldData, newData);
// System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
// } catch (SVNException e) {
// e.printStackTrace();
// }
//4.删除文件 或文件夹
// try {
// SvnKitUtils svn = new SvnKitUtils(url, username, password);
// ISVNEditor editor = svn.svnRepository.getCommitEditor("删除文件",null,true,null);
svn.deleteFile(editor, "/snv/开发类/a/itemC1.txt", revisionNo);
// System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
// } catch (SVNException e) {
// e.printStackTrace();
// }
}
}