Java代码操作SVN
package com.leadbank.oprPlatform.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SvnUtil{
private Logger logger = LoggerFactory.getLogger(SvnUtil.class);
// @Inject(instance = PropertyUtil.class)
// private PropertyUtil propertyUtil;
static {
DAVRepositoryFactory.setup();
}
private SVNClientManager manager;
private SVNUpdateClient updateClient;
private String url;
private String userName;
private String passwd;
public SvnUtil(String userName, String passwd) {
init(userName, passwd);
}
public SvnUtil(String userName, String passwd, String url){
this(userName,passwd);
this.url=url;
}
private void init(String userName,String passwd){
DefaultSVNOptions options = new DefaultSVNOptions();
manager = SVNClientManager.newInstance(options);
manager = SVNClientManager.newInstance(options,userName,passwd);
updateClient = manager.getUpdateClient();
updateClient.setIgnoreExternals(false);
}
/**获取文档内容
* @param url
* @return
*/
public String checkoutFileToString(String url) throws SVNException {//"", -1, null
SVNRepository repository = createRepository(url);
SVNDirEntry entry = repository.getDir("", -1, false, null);
int size = (int)entry.getSize();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(size);
SVNProperties properties = new SVNProperties();
repository.getFile("", -1, properties, outputStream);
String doc = new String(outputStream.toByteArray(), Charset.forName("utf-8"));
return doc;
}
public boolean toParantFolder(){
if(url!=null){
StringBuffer sb = new StringBuffer(url);
if(url.endsWith("/")){
sb.deleteCharAt(sb.length()-1);
}
int index = sb.lastIndexOf("/");
url=sb.substring(0, index);
return true;
}
return false;
}
/**进入子目录
* @param folder
* @return
*/
public boolean toChildFolder(String folder){
if(url!=null){
StringBuffer sb = new StringBuffer(url);
boolean a = url.endsWith("/");
boolean b = folder.startsWith("/");
if(a^b){
sb.append(folder);
}else if(a&b){
sb.deleteCharAt(sb.length()-1);
sb.append(folder);
}else{
sb.append('/').append(folder);
}
if(checkPath(sb.toString())==1){
this.url=sb.toString();
return true;
}
}
return false;
}
/**获取当前目录下的子目录和文件
* @return
* @throws SVNException
*/
public List<SVNDirEntry> listFolder() throws SVNException {
return listFolder(url);
}
/**列出指定SVN 地址目录下的子目录
* @param url
* @return
* @throws SVNException
*/
public List<SVNDirEntry> listFolder(String url){
if(checkPath(url)==1){
SVNRepository repository = createRepository(url);
try {
Collection<SVNDirEntry> list = repository.getDir("", -1, null, (List<SVNDirEntry>)null);
List<SVNDirEntry> dirs = new ArrayList<SVNDirEntry>(list.size());
dirs.addAll(list);
return dirs;
} catch (SVNException e) {
logger.error("listFolder error",e);
}
}
return null;
}
private SVNRepository createRepository(String url){
try {
return manager.createRepository(SVNURL.parseURIEncoded(url), true);
} catch (SVNException e) {
logger.error("createRepository error",e);
}
return null;
}
/**检查路径是否存在
* @param url
* @return 1:存在 0:不存在 -1:出错
*/
public int checkPath(String url){
SVNRepository repository = createRepository(url);
SVNNodeKind nodeKind;
try {
nodeKind = repository.checkPath("", -1);
boolean result = nodeKind == SVNNodeKind.NONE ? false : true;
if(result) return 1;
} catch (SVNException e) {
logger.error("checkPath error",e);
return -1;
}
return 0;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public static void main(String[] args) throws SVNException {
String url = "http://10.1.1.135/svn/cloudPlatform/oprPlatform/trunk/oprPlatform-web/src/main/webapp/WEB-INF/web.xml";
SvnUtil svn = new SvnUtil("xxx", "xxx");
String xml = svn.checkoutFileToString(url);
System.out.print(xml);
}
}
Java代码操作SVN的更多相关文章
- Java代码操作HDFS测试类
1.Java代码操作HDFS需要用到Jar包和Java类 Jar包: hadoop-common-2.6.0.jar和hadoop-hdfs-2.6.0.jar Java类: java.net.URL ...
- 使用java代码操作Redis
1导入pom.xml依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...
- java代码操作Redis
1.导入需要的pom依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...
- Java代码操作zookeeper
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- 分享知识-快乐自己:java代码 操作 solr
POM 文件: <!-- solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> < ...
- 大数据之路week07--day01(HDFS学习,Java代码操作HDFS,将HDFS文件内容存入到Mysql)
一.HDFS概述 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管理系统 ...
- java:nginx(java代码操作ftp服务器)
1.检查是否安装了vsftpd [root@linux01 ~]# rpm -qa|grep vsftpd 2.安装vsftpd [root@linux01 ~]# yum -y install vs ...
- Java代码操作HDFS(在/user/root/下面創建目錄)
1.创建HDFS目录并打成jar包 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.h ...
- kerberos下JAVA代码操作hbase的方式(客户端方式,应用程序方式)
(一)如果部署JAVA 代码的主机用户获取了kerberos权限情况下 假设主机名是:client su - client 登录主机后 kinit -kt /keytab的路径/client.keyt ...
随机推荐
- #define is unsafe
#define is unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Dapper.Contrib——更加优雅地使用Dapper进行增删改查
简介 Dapper是介于Entity framework与ADO的折中选择.既满足手写查询的高性能需求,又简化了数据库对象映射为内存对象的繁杂工作.Dapper.Contrib是对Dapper的进一步 ...
- 利用HTML5新特性改变浏览器地址后不刷新页面
原文:http://www.cnblogs.com/xuchengzone/archive/2013/04/18/html5-history-pushstate.html 作为一个程序员,上Git ...
- java_IO流读取本地文件
package com.ht.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoun ...
- J-Link驱动下载和Hex程序下载
J-LINK驱动下载 1.首先拥有硬件J-Link硬件. 2.安装J-Link驱动程序SEGGER 下载地址如下 https://www.segger.com/downloads/jlink/JLin ...
- C#多线程和线程同步总结
Thread 没有参数的线程启动 Thread newThread = new Thread(new ThreadStart(DoWork)); newThread.Start(); 有参数的线程启动 ...
- sql分区文件删不的可能解决方法
删除数据库分区的时候报错如下: ALTER DATABASE [ITMP2] remove FILE F20170427Msg 5042, Level 16, State 1, Line 1The f ...
- linux操作系统基础篇(一)
1.什么是linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序 ...
- 软件测试博客日记Day02-11.16日 —— 赵天宇 —— 禅道的使用和配置
禅道 1. 安装 1. 进入禅道的官方下载地址:http://www.zentao.net/download/80053.html 2. 下载禅道开源版本. 3. 正常安装,注意一定要放在根目录下. ...
- 了解Python列表的一些方法
首先定义一个名字列表,然后使用print() BIF在屏幕上显示这个列表. 接下来,使用len() BIF得出列表中有多少个数据项,然后再访问并显示第2个数据项的值: 创建了列表之后,可以使用列表方法 ...