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 ...
随机推荐
- HDU1175 连连看(DFS)
Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经 ...
- css中的em用法
px:是相对于浏览器分辨率的一个度量单位 em是一个相对于父元素的font-size的大小的一个度量单位 1.浏览器的默认字体大小是16px 2.如果元素自身没有设置字体大小,那么元素自身上的所有属性 ...
- HTML5网页音乐播放器
1功能介绍 HTML5中推出了音视频标签,可以让我们不借助其他插件就可以直接播放音视频.下面我们就利用H5的audio标签及其相关属性和方法来制作一个简单的音乐播放器.主要包括以下几个功能: 1.播放 ...
- HTML DOM应用案例1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Windows系统下python3中安装pyMysql
python2和python3是不兼容的,在py2中,链接数据库使用的是mysqldb,但在py3中是不能用的. 解决办法就是在py3中数据库使用的模块是pyMysql. 在dos窗口中安装第三方库会 ...
- linux-less
linux-less less是more命令的进化版.拥有与more命令一样的向前向后向下向上的数据查看功能,同时less还可以在查看内容中进行快速查找,关于数据向上向下操作,可以看看这个http:/ ...
- form提交地址地址正确但是依旧报错404找不到路径
---恢复内容开始--- 我的jsp中保含了">="和"<="符号,form提交的时候会有个标签校验,如下: private static bool ...
- 淘宝轮播JS
taobao首页轮播原生js面对对象封装版 Author:wyf 2012/2/25
- nodejs a和b文件相互引用
//取自于node中文网 http://nodejs.cn/api/modules.html 当循环调用 require() 时,一个模块可能在未完成执行时被返回. 例如以下情况: a.js: con ...
- [转]oracle系统表v$session、v$sql字段说明
在本视图中,每一个连接到数据库实例中的 session都拥有一条记录.包括用户 session及后台进程如 DBWR, LGWR, arcchiver等等. V$SESSION中的常用列 V$SESS ...