SVNKit操作SVN
系统集成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
SVNKit操作SVN的更多相关文章
- SVNKIT操作SVN版本库的完整例子
Model: package com.wjy.model; public class RepositoryInfo { public static String storeUrl="http ...
- 命令行操作svn和git和git
前几天在写代码的时候电脑突然坏掉,老大交代的任务没完成,非常痛恨自己用svn或者git保存代码,相信很多程序员遇到过,硬盘坏掉,存在硬盘中的代码丢失,无法找回的问题,svn和git可谓程序员界的福音, ...
- SVNKit学习——svn二次开发背景和闲谈(一)
开发背景: 简述现有流程:代码的合并.提交是以任务为最小单元的.例如A和B两个同学开发不同的任务,那就是两个任务号.合并的时候可能会先合并A的代码,在合并B的代码. 需求:SVN合并程序开发——一款能 ...
- java操作svn【svnkit】实操
SVNKit中怎样使用不同的仓库访问协议? 当你下载了最新版的SVNKit二进制文件并且准备使用它时,一个问题出现了,要创建一个库需要做哪些初始化的步骤?直接与Subversion仓库交互已经在低级层 ...
- java svnkit实现svn提交,更新等操作
官网:https://svnkit.com/ api:https://svnkit.com/javadoc/org/tmatesoft/svn/core/io/SVNRepository.html w ...
- svnkit 用java 操作 svn
官网 https://svnkit.com/ https://blog.csdn.net/Hui_hai/article/details/80318518 https://blog.csdn.net/ ...
- java操作svn工具类SvnUtil
直接上代码,工作中使用的版本,记录下. public class SvnUtil { private static Logger logger = Logger.getLogger(SvnUtil.c ...
- TortoiseSVN下载,安装,配置,常用操作 svn教程
一. 首先在百度搜索并下载 TortoiseSVN 推荐从官网下载,软件分为32位和64位版本,下载时请根据自己的系统位数进行下载:
- mac 下终端 操作svn命令 以及出现证书错误的处理方法
首先,转载地址:http://hi.baidu.com/zhu410289616/item/eaaf160f60eb0dc62f4c6b0e 还有一个地址:http://www.cnblogs.com ...
- 用idea操作svn
使用SVN前提必须安装好服务端和客户端,或者知道服务端的url才能对服务器中的文件进行操作. 服务端:SVN service 客户端:TortoiseSVN 提交 第一步:确认SVN 服务器是否开启 ...
随机推荐
- EMQ X 系统调优和性能压测
前言 如果使用 EMQ 来承载百万级别的用户连接可以吗?毕竟在 MQTT 官方介绍上说 EMQ X 可以处理千万并发客户端,而 EMQ X 自己官方称 4.x 版本 MQTT 连接压力测试一台 8 核 ...
- Java后台校验手机号和邮箱
//true是手机号,false不是手机号 public static boolean checkPhone(String phone){ Pattern p = Pattern.compile(&q ...
- Vue 权限控制 使用自定义指令 代替v-if
参考了vue-element-admin 指令库的permission 使用Demo(demo.vue) <template> <p>admin组:<span v-per ...
- 项目管理 VS. 个人计划
注会考试临近,好好的安排一下自己的时间.做个小计划是很有必要的了. 突然想到一个软件也许可以帮到我的忙,那就微软提供的办公软件 Project,大学时候就知道Project软件的存在,但是始终没有机会 ...
- eslint规范在项目中的实现——vue项目举例
先附上参考链接: https://juejin.cn/post/7068573328914513928 https://juejin.cn/post/6857135010882387981 https ...
- 【vite+pinia】
vite 轻量快速热重载,开发环境中不需要打包操作,可以快速冷启动 按需编译,不需要等待整个应用编译完成 pinia 支持vue2和vue3,是vue专属的状态管理库,允许跨组件或者页面 共享状态 与 ...
- Spring的IOC源码分析
Spring IOC 容器源码分析 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不 ...
- redis底层数据结构之跳表(skiplist)
跳表(跳跃表, skiplist) 跳跃表(skiplist)是用于有序元素序列快速搜索查找的数据结构,跳表是一个随机化的数据结构,实质是一种可以进行二分查找的.具有层次结构的有序链表 跳表在原有的有 ...
- Linux Qt编译时出现has modification time int the future的解决方法
问题场景:我在window系统上合并完代码后,将代码通过TF卡拖到了Debian系统的开发板子上(为什么我不用Winscp或者xhttp传呢?因为网线被同事拿走了...),然后就报这个错. 网上查阅资 ...
- git如何把本地文件夹和远程仓库关联
场景: 1,有一个本地项目,没有上传到git过,你在远程新建了一个仓库,想把这个本地的代码推送到该仓库 2,直接想把本地代码推送到远程并创建该本地文件对应的仓库(这种情况不可以实现) 解决方法: 本地 ...