java调用svnkit工具类上传本地文件到svn服务器
package org.jenkinsci.plugins.svn.upload.step;
import java.io.*;
import org.tmatesoft.svn.core.SVNCommitInfo;
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.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class SvnUpload {
private SVNClientManager clientManager;
private ISVNAuthenticationManager authManager;
private SVNRepository repository;
/**
*
* @param svnUrl svn地址
* @param svnUsername svn用户名称
* @param svnPasswd svn用户密码
* @throws SVNException 异常信息
*/
public SvnUpload(String svnUrl, String svnUsername, String svnPasswd)throws SVNException{
try {
this.createDefaultAuthenticationManager(svnUsername, svnPasswd);
this.authSvn(svnUrl);
} catch (SVNException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 通过不同的协议初始化版本库
*/
private void setupLibrary() {
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
}
/**
*
* @param username svn用户名称
* @param password svn用户密码
* @throws SVNException 异常信息
*/
private void createDefaultAuthenticationManager(String username, String password)throws SVNException{
try {
// 身份验证
authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
} catch (Exception e) {
throw new RuntimeException("SVN身份认证失败:" + e.getMessage());
}
}
/**
* 验证登录svn
* @param svnUrl 用户svn的仓库地址
* @throws SVNException 异常信息
*/
private void authSvn(String svnUrl) throws SVNException {
// 初始化版本库
setupLibrary();
try {
repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
} catch (SVNException e) {
throw new RuntimeException("SVN创建库连接失败:" + e.getMessage());
}
// 创建身份验证管理器
repository.setAuthenticationManager(authManager);
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
try {
//创建SVN实例管理器
clientManager = SVNClientManager.newInstance(options,authManager);
} catch (Exception e) {
throw new RuntimeException("SVN实例管理器创建失败:" + e.getMessage());
}
}
/**
* 添加文件和目录到版本控制下
* @param wcPath 工作区路径
* @throws SVNException 异常信息
*/
private void addEntry(File wcPath) throws SVNException{
try {
clientManager.getWCClient().doAdd(new File[] { wcPath }, true,
false, false, SVNDepth.INFINITY, false, false, true);
} catch (SVNException e) {
throw new RuntimeException("SVN添加文件到版本控制下失败:" + e.getMessage());
}
}
/**
* 将工作副本提交到svn
* @param wcPath 被提交的工作区路径
* @param keepLocks 是否在SVN仓库中打开或不打开文件
* @param commitMessage 提交信息
* @return 返回信息
* @throws SVNException 异常信息
*/
private SVNCommitInfo commit(File wcPath, boolean keepLocks, String commitMessage) throws SVNException {
try {
return clientManager.getCommitClient().doCommit(
new File[] { wcPath }, keepLocks, commitMessage, null,
null, false, false, SVNDepth.INFINITY);
} catch (SVNException e) {
throw new RuntimeException("SVN提交失败:" + e.getMessage());
}
}
/**
* 确定path是否是一个工作空间
* @param path 文件路径
* @return 返回信息
* @throws SVNException 异常信息
*/
private boolean isWorkingCopy(File path) throws SVNException{
if(!path.exists()){
return false;
}
try {
if(null == SVNWCUtil.getWorkingCopyRoot(path, false)){
return false;
}
} catch (SVNException e) {
throw new RuntimeException("确定path是否是一个工作空间 失败:" + e.getMessage());
}
return true;
}
/**
* 确定一个URL在SVN上是否存在
* @param url svn访问地址
* @return 返回信息
* @throws SVNException 异常信息
*/
private boolean isURLExist(SVNURL url) throws SVNException{
try {
SVNRepository svnRepository = SVNRepositoryFactory.create(url);
svnRepository.setAuthenticationManager(authManager);
SVNNodeKind nodeKind = svnRepository.checkPath("", -1);
return nodeKind == SVNNodeKind.NONE ? false : true;
} catch (SVNException e) {
throw new RuntimeException("确定一个URL在SVN上是否存在失败:" + e.getMessage());
}
}
/**
* 递归检查不在版本控制的文件,并add到svn
* @param wc 检查的文件
* @throws SVNException 异常信息
*/
private void checkVersiondDirectory(File wc) throws SVNException{
if(!SVNWCUtil.isVersionedDirectory(wc)){
this.addEntry(wc);
}
if(wc.isDirectory()){
for(File sub:wc.listFiles()){
if(sub.isDirectory() && sub.getName().equals(".svn")){
continue;
}
checkVersiondDirectory(sub);
}
}
}
/**
* 删除目录(文件夹)以及目录下的文件
* @param sPath 被删除目录的文件
* @return 目录删除成功返回true,否则返回false
*/
private boolean deleteDirectory(String sPath) {
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} //删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 删除单个文件
* @param sPath 被删除文件的文件
* @return 单个文件删除成功返回true,否则返回false
*/
private boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*@param sPath 要删除的目录或文件
*@return 删除成功返回 true,否则返回 false。
*/
private boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(file.getAbsolutePath());
} else { // 为目录时调用删除目录方法
return deleteDirectory(file.getAbsolutePath());
}
}
}
/**
* 更新SVN工作区
* @param wcPath 工作区路径
* @param updateToRevision 更新版本
* @param depth update的深度:目录、子目录、文件
* @return 返回信息
* @throws SVNException 异常信息
*/
private long update(File wcPath,SVNRevision updateToRevision, SVNDepth depth) throws SVNException{
SVNUpdateClient updateClient = clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
return updateClient.doUpdate(wcPath, updateToRevision,depth, false, false);
} catch (SVNException e) {
throw new RuntimeException("更新SVN工作区失败:" + e.getMessage());
}
}
/**
* SVN仓库文件检出
* @param url 文件url
* @param revision 检出版本
* @param destPath 目标路径
* @param depth checkout的深度,目录、子目录、文件
* @return 返回信息
* @throws SVNException 异常信息
*/
private long checkout(SVNURL url, SVNRevision revision, File destPath, SVNDepth depth) throws SVNException{
SVNUpdateClient updateClient = clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
return updateClient.doCheckout(url, destPath, revision, revision,depth, false);
} catch (SVNException e) {
throw new RuntimeException("检出SVN仓库失败:" + e.getMessage());
}
}
/**
* @param svnUrl svn地址
* @param workspace 工作区
* @param filepath 上传的文件地址
* @param filename 文件名称
* @throws SVNException 异常信息
*/
private void checkWorkCopy(String svnUrl,String workspace,String filepath,String filename)throws SVNException{
SVNURL repositoryURL = null;
try {
repositoryURL = SVNURL.parseURIEncoded(svnUrl);
} catch (SVNException e) {
throw new RuntimeException("解析svnUrl失败:" + e.getMessage());
}
String fPath = "";
if(filepath.indexOf("/") != -1) {
fPath = filepath.substring(0,filepath.lastIndexOf("/"));
}
File wc = new File(workspace+"/"+fPath);
File wc_project = new File( workspace + "/" + fPath);
SVNURL projectURL = null;
try {
projectURL = repositoryURL.appendPath(filename, false);
} catch (SVNException e) {
throw new RuntimeException("解析svnUrl文件失败:" + e.getMessage());
}
if(!this.isWorkingCopy(wc)){
if(!this.isURLExist(projectURL)){
this.checkout(repositoryURL, SVNRevision.HEAD, wc, SVNDepth.EMPTY);
}else{
this.checkout(projectURL, SVNRevision.HEAD, wc_project, SVNDepth.INFINITY);
}
}else{
this.update(wc, SVNRevision.HEAD, SVNDepth.INFINITY);
}
}
/**
* 循环删除.svn目录
* @param spath
*/
private void deletePointSVN(String spath){
File wc = new File(spath);
for(File sub:wc.listFiles()){
if(sub.isDirectory() && sub.getName().equals(".svn")){
this.deleteDirectory(sub.getAbsolutePath());
continue;
}
if(sub.isDirectory()){
deletePointSVN(sub.getAbsolutePath());
}
}
}
/**
*
* @param svnUrl svn地址
* @param workspace 工作区
* @param filepath 上传的文件地址
* @param filename 上传的文件名称
* @param isOverwrite 是否覆盖
* @throws SVNException 异常信息
*/
public void upload(String svnUrl,String workspace,String filepath,String filename,Boolean isOverwrite)throws SVNException{
String svnfilePath = svnUrl+"/"+filename;
//开始前删除以前的.svn文件目录
deletePointSVN(workspace);
boolean flag = this.isURLExist(SVNURL.parseURIDecoded(svnfilePath));
if(flag){
if(isOverwrite){
this.uploadFile(svnUrl, workspace, filepath,filename);
}
}else{
this.uploadFile(svnUrl, workspace, filepath,filename);
}
//结束后删除当前的.svn文件目录
deletePointSVN(workspace);
}
/**
*
* @param svnUrl svn地址
* @param workspace 工作区
* @param filepath 上传的文件地址
* @param filename 文件名称
* @throws SVNException 异常信息
*/
private void uploadFile(String svnUrl,String workspace,String filepath,String filename)throws SVNException{
this.checkWorkCopy(svnUrl, workspace, filepath,filename);
File file = new File(workspace+"/"+filepath);
this.checkVersiondDirectory(file);
this.commit(file, false, "commit file:"+file);
}
public static void main(String[] args) throws SVNException {
try {
String svnUrl = "http://192.168.18.200/svn/repository/hello";
String username = "admin";
String passwd = "admin";
String workspace = "C:\\Users\\kube\\Desktop\\test";
String upfile = "/q/w/a.txt,b.txt";
Boolean isOverwrite = true;
SvnUpload svnUpload = new SvnUpload(svnUrl,username, passwd);
String [] fileArray = upfile.split(",");
for(int i=0;i<fileArray.length;i++){
String filePath = fileArray[i];
String filename = "";
if (filePath.indexOf("/") != -1) {
filename = filePath.substring(filePath.lastIndexOf("/"));
} else {
filename = filePath;
}
svnUpload.upload(svnUrl, workspace, filePath, filename,isOverwrite);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java调用svnkit工具类上传本地文件到svn服务器的更多相关文章
- 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器
引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户后,我们就迫不及待的想用JAVA去操作Samba服务器了,我们找到了一个框架叫 jcifs,可以高效的完成我们工作. 实践: ...
- 上传本地文件到SVN
前言:今天按照自己的记忆上传本地文件夹到SVN,出现了点问题,重温了简单操作. https://blog.csdn.net/qq_35150366/article/details/81129847 参 ...
- 上传的文件放在SVN服务器的哪个目录下
SVN服务器版本库有两种格式,一种为FSFS,一种为BDB 把文件上传到SVN版本库后,上传的文件不再以文件原来的格式存储,而是被svn以它自定义的格式压缩成版本库数据,存放在版本库中. 如果是FSF ...
- git 上传本地文件到github
git 上传本地文件到github 1 git config --global user.name "Your Real Name" 2 git config --global u ...
- 两种方法上传本地文件到github
https://www.jianshu.com/p/c70ca3a02087 自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的 ...
- 两种方法上传本地文件到github(转)
自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...
- paramiko模块的安装和使用(含上传本地文件或文件夹到服务器,以及下载服务器文件到本地)
安装和使用分两步介绍: 介绍一下,本文的运行环境是win7 64位 和python 2.7 . 安装: WIN7_64位 安装python-ssh访问模块(paramiko)的安装教程,本人亲测下面 ...
- SSH 远程上传本地文件至服务器
使用SSH命令行传输文件到远程服务器 以前一直在windows下用SSH Secure Shell连接远程服务器,它自带了一个可视化的文件传输工具,跟ftp差不多 但是它也存在一个缺陷,不支持编码 ...
- 如何用一张图片代替 'input:file' 上传本地文件??
今天去面试,碰到了一道题,也许是因为紧张或者喝水喝多了,一时竟然没有转过弯来,回来之后一细想原来这么简单,哭笑不得,特此记录一下! 原题是这样的: 如何用一张图片代替 'input:file' 上传 ...
随机推荐
- swift - 动画学习
// // ViewController.swift // MapAnimation // // Created by su on 15/12/10. // Copyright © 2015年 ...
- 用U盘安装Ubuntu主系统
用U盘安装Ubuntu主系统 0. 学习Ubuntu系统的的动机 ubuntu系统的内核是linux系统,通过Ubuntu的学习,掌握lInux服务器搭建!!!! 硬件要求:闲置的笔记本 + U盘 ...
- [编译,报错以及其他] 有关C/C++中int不能用-2147483648当最小值的问题
这个取决于今早看耗子叔的微博: 这里说到了int的取值范围的问题,int的取值是-2147483648 ~ 2147483647,但是如果直接在编译器(VS2013)中使用-2147483648会报错 ...
- Android-进程理解/进程的优先级别
进程理解 Android系统最小的控制单元是:进程 process 应用/CPU最小的控制单元是:线程 thread 一个应用一个 process 进程 一个应用一个 package(包是唯一的) 一 ...
- 2.自己的Github试用过程
打开我个人的Github,我试着做些简单的试用.首先,经过简短描述,我成功创建了一个新的存储库
- 关于 Kafka offset
查询topic的offset的范围 用下面命令可以查询到topic:Mytopic broker:SparkMaster:9092的offset的最小值: bin/kafka-run-class.sh ...
- Linux Guard Service - 进程分裂与脱离
进程分裂更名 void set_ps_name(char *name) { prctl(PR_SET_NAME, name); } 修改进程长名称 备份进程环境变量空间 for (i = 1; i & ...
- R语言中Fisher判别的使用方法
最近编写了Fisher判别的相关代码时,需要与已有软件比照结果以确定自己代码的正确性,于是找到了安装方便且免费的R.这里把R中进行Fisher判别的方法记录下来. 1. 判别分析与Fisher判别 不 ...
- ASP.NET Core IdentityServer4 新手上路
OAuth2.0资料 今天看到一篇博主写了该系列文章,贴图和过程都比较详细,俗话说实践是检验真理的唯一标准(如果是按照参考文章复制粘贴,应该不会出现踩坑,但是我喜欢自己手动敲一遍),发现几个坑,因而总 ...
- 关于EF的一点小记录
今日新闻:朝鲜要改革开放了!!!比你牛逼的人都在努力,你还有理由懒惰吗? 宇宙强大IDE配套的EF问题记录 今天做数据添加时,Id我设置为int类型了,结果在做Add操作时报的错让我摸不着头脑,后来问 ...