首先实现svn 权限验证:
/**
* 验证登录svn
*/
public static SVNClientManager authSvn(String svnRoot, String username,
String password) {
// 初始化版本库
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
// 创建库连接
SVNRepository repository;
try {
repository = SVNRepositoryFactory.create(SVNURL
.parseURIEncoded(svnRoot));
} catch (SVNException e) {
logger.error(e.getMessage(), e);
return null;
}
// 身份验证
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
// 创建身份验证管理器
repository.setAuthenticationManager(authManager);
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options,
authManager);
return clientManager;
}
1. 新增SVN 目录
/**
* Make directory in svn repository
* @param clientManager
* @param url
* eg: http://svn.ambow.com/wlpt/bsp/trunk
* @param commitMessage
* @return
* @throws SVNException
*/
public static SVNCommitInfo makeDirectory(SVNClientManager clientManager, String url, String commitMessage) {
try {
SVNURL svnurl = SVNURL.parseURIEncoded(url);
SVNCommitClient svnCommitClient = clientManager.getCommitClient();
return svnCommitClient.doMkDir( new SVNURL[] { svnurl }, commitMessage, null, true);
} catch (SVNException e) {
logger.error(e.getMessage(), e);
}
return null;
}
2.删除SVN目录
/**
* 删除模型
*/
public static void deleteModel(String url,String message,String defaultSvnRoot,String username,String password) throws Exception{
// 权限验证
SVNClientManager ourClientManager = authSvn(defaultSvnRoot, username,password);
SVNCommitClient commitClient=ourClientManager.getCommitClient();
commitClient.setIgnoreExternals(false);
SVNURL repositoryOptUrl=SVNURL.parseURIEncoded(url);
SVNURL deleteUrls[]=new SVNURL[1];
deleteUrls[0]=repositoryOptUrl;
commitClient.doDelete(deleteUrls, message);
}
3. 修改svn 目录
/**
* 修改svn 目录 , 将 oldUrl 修改为 newUrl
* @param buffUrl oldUrl
* @param storeUrl newUrl
* @param message
* @param defaultSvnRoot SvnRoot
* @param username
* @param password
* @return
*/
public static SVNCommitInfo modifyModel(String buffUrl, String storeUrl, String message, String defaultSvnRoot, String username, String password) {
// 权限验证
SVNClientManager ourClientManager = authSvn(defaultSvnRoot, username, password);
SVNCopyClient copyClient = ourClientManager.getCopyClient();
copyClient.setIgnoreExternals(false);
try {
SVNURL oldUrl = SVNURL.parseURIEncoded(defaultSvnRoot + buffUrl);
SVNURL newUrl = SVNURL.parseURIEncoded(defaultSvnRoot+storeUrl);
SVNCopySource[] modifySources = new SVNCopySource[1];
modifySources[0] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, oldUrl);
return copyClient.doCopy(modifySources, newUrl, true, true, true, message, null);
} catch (SVNException e) {
e.printStackTrace();
}
return null;
}
4. 复制svn文件(将A目录下的文件复制到B 目录下)
/**
* 复制 A目录(子文件)到 B 目录
*/
public static SVNCommitInfo copyModel(String buffUrl,String storeUrl,String message,String revision,String defaultSvnRoot,String username,String password) {
SVNClientManager ourClientManager = authSvn(defaultSvnRoot, username, password);
SVNCopyClient copyClient = ourClientManager.getCopyClient();
copyClient.setIgnoreExternals(false);
try {
// 根目录
SVNURL root = SVNURL.parseURIEncoded(defaultSvnRoot);
// 被复制目录
SVNURL repositoryOptUrl = SVNURL.parseURIEncoded(defaultSvnRoot + buffUrl);
// 目的目录
SVNURL destUrl = SVNURL.parseURIEncoded(defaultSvnRoot + storeUrl);
SVNRepository repository = ourClientManager.createRepository(root, true);
// 检测 目的 url目录是否存在; -1 代表当前最新版本
SVNNodeKind svnNodeKind = repository.checkPath(storeUrl, -1);
SVNCopySource[] copySources;
// 如果目标目录B存在, 获取复制路径下A子目录到目标地址; 否则直接复制A 到B
if (svnNodeKind == SVNNodeKind.DIR) {
String basePath = defaultSvnRoot + buffUrl;
repository.setLocation(repositoryOptUrl, true);
List urlPaths = listEntries(repository, "",Long.valueOf(revision));
copySources = new SVNCopySource[urlPaths.size()];
for (int i = 0; i < urlPaths.size(); i++) {
copySources[i] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, SVNURL.parseURIEncoded(basePath +urlPaths.get(i)));
}
} else {
copySources = new SVNCopySource[1];
if ("-1".equals(revision)) {
copySources[0] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, repositoryOptUrl);
} else {
copySources[0] = new SVNCopySource(SVNRevision.create(Long.valueOf(revision)), SVNRevision.create(Long.valueOf(revision)), repositoryOptUrl);
}
}
return copyClient.doCopy(copySources, destUrl, false, true, false, message, null, false, null);
} catch (SVNException e) {
e.printStackTrace();
}
return null;
}
/**
* 此函数递归的获取版本库中某一目录下的条目。
* @param repository
* @param path
* @throws SVNException
*/
public static List listEntries(SVNRepository repository, String path, long revision)
throws SVNException {
List<String> urlPaths =new ArrayList();
//获取版本库的path目录下的所有条目。参数-1表示是最新版本。
Collection entries = repository.getDir(path, revision, null,(Collection) null);
Iterator iterator = entries.iterator();
while (iterator.hasNext()) {
SVNDirEntry entry = (SVNDirEntry) iterator.next();
logger.info("/" + (path.equals("") ? "" : path + "/") + entry.getName());
urlPaths.add("/"+entry.getName());
// // 如果时目录递归查询获取目录下面的文件信息
// if (entry.getKind() == SVNNodeKind.DIR) {
// listEntries(repository, (path.equals("")) ? entry.getName(): path + "/" + entry.getName());
// }
}
return urlPaths;
}