Java 上传文件到Svn
1、在pom.xml文件中添加需要配置的svnkit版本号,然后通过maven进行更新下载配置文件
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.10.1</version>
</dependency>
2,、配置svn路径和用户名密码
文件名:svnConfig.properties
svn.svnUrl =https://xxx/xxx
svn.username =admin
svn.password =admin
3、关于Svn提交、删除目录和文件的方法
package com.ascom.svn;
import org.springframework.util.StringUtils;
import org.tmatesoft.svn.core.*;
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;
import java.io.File;
import java.util.ResourceBundle;
/**
* @ClassName SvnUtils
* @Description svn公共类
* @Author wangyuxin
* @Date 2020/6/8
* @Version 1.0
*/
public class SvnUpload {
private SVNClientManager clientManager;
private ISVNAuthenticationManager authManager;
private SVNRepository repository;
/** 读取配置文件 只能读取properties类型的文件, 读取的时候只需要文件名, 不需要后缀 */
private static ResourceBundle rb = ResourceBundle.getBundle("svnConfig");
/** svn地址 */
private static String svnUrl = rb.getString("svn.svnUrl");
private static String svnUsername = rb.getString("svn.username");
private static String svnPassword = rb.getString("svn.password");
/**
* 启动svn连接
* @throws SVNException 异常信息
*/
public SvnUpload()throws SVNException {
try {
this.createDefaultAuthenticationManager(svnUsername, svnPassword);
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.toCharArray());
} 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.