系统集成SVN(SVNKit操作SVN)

网址:https://svnkit.com/documentation.html

文档:https://svnkit.com/javadoc/index.html

依赖以及介绍

​ JavaHLL API:本机Subversion包括可通过JavaHL接口-SVNClientInterface使用的JNI绑定。SVNKit使用SVNClientImpl类实现它,这样您就可以在运行时在JavaHL和SVNKit之间切换。

1、maven依赖:

<!-- https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit -->
<!-- 目前最新版本1.10.8,可使用1.10.X版本-->
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.10.8</version>
</dependency>

2、API介绍

 High-Level API:主要使用SVNClientManager类来创建一些了的SVN*Client实现一系列对Working Copy的操作,比如创建一个SVNUpdateClient,可以执行checkout、update、switch、export等。

 Low-Level API:主要使用SVNRepository类与Repository仓库进行交互,支持的协议都是基于SVNRepositoryFactory抽象类的具体实现,协议和实现类的关系:(通过不同的类创建不同的上传协议)

protocol    SVNRepositoryFactory realization
svn:// SVNRepositoryFactoryImpl
http:// DAVRepositoryFactory
file:/// FSRepositoryFactory

开始使用SVNKit

使用High-Level API的操作步骤:

1、根据不同协议,初始化不同的仓库工厂。(工厂实现基于SVNRepositoryFactory抽象类)**

备注:一般性使用static void 方法进行初始化

//svn://,  svn+xxx:// (svn+ssh:// in particular)
SVNRepositoryFactoryImpl.setup();
//http:// and https://
DAVRepositoryFactory.setup();
//file:///
FSRepositoryFactory.setup();

2、创建一个驱动。(基于工厂),svnkit中repository所有的URL都基于SVNURL类生成,编码不是UTF-8的话,可以调用SVNURL的parseURIEncoded()方法。url可以是项目根目录、目录或文件。

SVNURL url = SVNURL.parseURIDecoded( "svn://host/path_to_repository_root/inner_path" );
SVNRepository repository = SVNRepositoryFactory.create( url, null );

这里的URL有两种表现形式:

①.不是以"/"开头,相对于驱动的绑定位置,即Repository的目录

②.以"/"开头,代表repository的根,相对于Repository的Root对应的目录

3、创建一个权限验证对象

SVN大多数操作都是具有权限访问控制的,大多数情况不支持匿名访问。

SVNKit使用ISVNAuthenticationManager接口来实现权限的控制。

SVNRepository repository=SVNRepositoryFactory.create(url);
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
repository.setAuthenticationManager(authManager);
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
return SVNClientManager.newInstance(options, authManager);

代码实例

package com.xxx.patchgen.svn;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.ISVNDiffStatusHandler;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNDiffStatus;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatusType;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil; public class SVNOperator {
private String userName;
private String passwd;
private SVNClientManager ourClientManager; private SVNWCClient wcClient;
private SVNDiffClient diffClient;
private SVNUpdateClient updateClient;
private List<SVNDiffStatus> changedFilePathsList = new ArrayList<SVNDiffStatus>(); public SVNOperator(String userName, String passwd) {
//setup
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
//create clientManager
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, passwd);
this.userName = userName;
this.passwd = passwd;
//generate client
wcClient = ourClientManager.getWCClient();
diffClient = ourClientManager.getDiffClient();
updateClient = ourClientManager.getUpdateClient();
} /**
* 获取指定版本的文件并转换为字符串
*/
public String getRevisionFileContent(String url,SVNRevision revision) throws Exception {
SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
OutputStream contentStream = new ByteArrayOutputStream();
wcClient.doGetFileContents(repositoryUrl, SVNRevision.HEAD, revision, false, contentStream);
return contentStream.toString();
}
/**
* 获取指定版本的文件
*/
public File getRevisionFile(String url,String version) throws Exception {
SVNRevision revision = SVNRevision.create(Long.parseLong(version));
File file = File.createTempFile("patch-", ".tmp");
SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
OutputStream contentStream = new FileOutputStream(file);
wcClient.doGetFileContents(repositoryUrl, SVNRevision.HEAD, revision, false, contentStream);
return file;
} /**
* 获取版本间差异文件
*/
public void getDiffFiles(String url,String startVersion,String endVersion) throws Exception{
changedFilePathsList.clear();
SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
// authentication(repositoryUrl);
SVNRevision start = SVNRevision.create(Long.parseLong(startVersion));
SVNRevision end = SVNRevision.create(Long.parseLong(endVersion));
diffClient.doDiffStatus(repositoryUrl,start,repositoryUrl,end,SVNDepth.INFINITY,false,new ISVNDiffStatusHandler() {
public void handleDiffStatus(SVNDiffStatus status) throws SVNException {
if(status.getKind() == SVNNodeKind.FILE
&& (status.getModificationType() == SVNStatusType.STATUS_ADDED
|| status.getModificationType() == SVNStatusType.STATUS_MODIFIED)){
changedFilePathsList.add(status);
System.out.println(status.getPath());
}
}
});
} /**
* 导出指定版本间差异文件
*/
public void doExport(String endVersion,String exportDir) throws Exception{
SVNRevision end = SVNRevision.create(Long.parseLong(endVersion));
for(SVNDiffStatus status: changedFilePathsList){
File destination = new File(exportDir + "/" +status.getPath());
updateClient.doExport(status.getURL(), destination, end, end, null, true, SVNDepth.getInfinityOrEmptyDepth(true));
}
} /**
* 导出最新版本文件
*/
public void doExportHead(String url,String exportDir) throws Exception{
SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
updateClient.doExport(repositoryUrl, new File(exportDir), SVNRevision.HEAD, SVNRevision.HEAD, null, true, SVNDepth.getInfinityOrEmptyDepth(true));
} /**
* 认证
*/
public void authentication(SVNURL url) throws Exception{
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, passwd);
SVNRepository repository = SVNRepositoryFactory.create(url);
repository.setAuthenticationManager(authManager);
} }

备注:具体代码实例操作可查看官网。

High Level API:https://wiki.svnkit.com/Managing_A_Working_Copy

Low Level API:https://wiki.svnkit.com/Managing_Repository_With_SVNKit

最新文章

  1. Js零散知识点笔记
  2. 简单配置webpack自动刷新浏览器
  3. bzoj2200: [Usaco2011 Jan]道路和航线
  4. Android Keycode详解
  5. SQL Server翻译目录
  6. FastDfs点滴
  7. ntpdate server时出错原因及解决
  8. SlickOne -- 基于Dapper, Mvc和WebAPI 的快速开发框架
  9. 学习ajax 总结
  10. 201621123062《java程序设计》第十周作业总结
  11. 测者的测试技术手册:Java中的null类型是测试不可超越的鸿沟
  12. 使用AngularJS中的filterFilter函数进行过滤
  13. 第五章&#160;二叉树(e1)先序遍历
  14. js/jq动态创建表格的行与列
  15. Eclipse的tomcat插件
  16. minicom 十六进制(hex)显示接收数据
  17. HDU 1717 小数化分数2(最大公约数)
  18. CentOS 7 yum 安装 Nginx
  19. 『HTML5挑战经典』是英雄就下100层-开源讲座(一)从天而降的英雄
  20. Atitit.常用分区api的attilax总结

热门文章

  1. Vue中如何实现在线预览word文件、excel文件
  2. Vulnhub:ReconForce-01.1靶机
  3. hdu-2544 最短路(SPFA)
  4. Servlet简介和ServletContext
  5. LinuxK8S集群搭建一(Master节点部署)
  6. WDA学习(27):RoadMap使用
  7. PID名词解析
  8. npm升级报错,没有权限.ERRERR!The operation was rejected by your operating system. npm ERR!Error: EPERM: operation not permitted, rename
  9. springboot上传图片
  10. Neuropsychological Assessment 5th