java获取svn中的数据

 

maven引用:

<dependency>
    <groupId>org.tmatesoft.svnkit</groupId>
    <artifactId>svnkit</artifactId>
    <version>1.9.3</version>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
</dependency>

java工具类:

import java.io.File;

import org.apache.log4j.Logger;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
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.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.ISVnoptions;
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 com.epoint.core.utils.file.FileManagerUtil;

public class SvnKitUtil
{
    private static final Logger log = Logger.getLogger(SvnKitUtil.class);

    private String repositoryUrl = "";
    private String userName = "";
    private String passWord = "";

    private SVNRepository repos = null;

    public SvnKitUtil(String svnUrl, String userName, String passWord) {
        this.repositoryUrl = svnUrl;
        this.userName = userName;
        this.passWord = passWord;

    }

    // 这边进行svb版本库的初始化
    public boolean connect() throws SVNException {
        SVNRepositoryFactoryImpl.setup();
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, passWord.tochararray());

        repos = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(repositoryUrl));
        repos.setAuthenticationManager(authManager);

        return repos != null;
    }

    // 获取svn版本库中的版本号
    public long getRevision() {

        long revision = 0L;
        try {
            SVNDirEntry entry = repos.info(".", -1);
            revision = entry.getRevision();
        }
        catch (SVNException e) {
            e.printstacktrace();
        }
        return revision;
    }

    // 判断是否成功连接到svn
    public boolean isConnected() {
        return repos != null;
    }

    /**
     * 检出代码
     * 
     * @param filePath
     * @return
     */
    public long checkOut(String filePath) {
        long revision = 0;

        // 相关变量赋值
        SVNURL repositoryURL = null;
        try {
            repositoryURL = SVNURL.parseURIEncoded(this.repositoryUrl);

            ISVnoptions options = SVNWCUtil.createDefaultOptions(true);
            // 实例化客户端管理类
            SVNClientManager ourClientManager = SVNClientManager.newInstance((DefaultSVnoptions) options, this.userName, this.passWord);
            // 要把版本库的内容check out到的目录
            File wcDir = FileManagerUtil.createFile(filePath);

            // 通过客户端管理类获得updateClient类的实例。
            SVNUpdateClient updateClient = ourClientManager.getUpdateClient();

            updateClient.setIgnoreExternals(false);

            // 执行check out 操作,返回工作副本的版本号。
            revision = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);

            log.debug("把版本:" + revision + " check out 到目录:" + wcDir + "中。");
        }
        catch (SVNException e) {
            log.warn("svn地址格式错误");
            e.printstacktrace();
        }

        return revision;
    }

    /**
     * 
     * @param filePath
     * @return
     * @throws Exception
     */
    public long update(String filePath) throws Exception {
        return update(FileManagerUtil.createFile(filePath));
    }

    /**
     * 更新代码
     * 
     * @param filePath
     * @return
     * @throws SVNException
     */
    public long update(File file) throws Exception {
        long revision = 0;

        DAVRepositoryFactory.setup();

        ISVnoptions options = SVNWCUtil.createDefaultOptions(true);
        // 实例化客户端管理类
        SVNClientManager ourClientManager = SVNClientManager.newInstance((DefaultSVnoptions) options, this.userName, this.passWord);
        // 获得updateClient的实例
        SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
        updateClient.setIgnoreExternals(false);
        // 执行更新操作
        revision = updateClient.doUpdate(file, SVNRevision.HEAD, SVNDepth.INFINITY, false, false);

        log.debug("工作副本更新后的版本:" + revision);

        return revision;
    }

    /**
     * 关闭
     */
    public void close() {
        if (repos != null) {
            repos.closeSession();
        }
    }

    public String getRepositoryUrl() {
        return repositoryUrl;
    }

    public void setRepositoryUrl(String repositoryUrl) {
        this.repositoryUrl = repositoryUrl;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getpassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public SVNRepository getRepos() {
        return repos;
    }

    public void setRepos(SVNRepository repos) {
        this.repos = repos;
    }

}

 

相关文章

首先介绍下什么是git和svnGIT(分布式版本控制系统)Git(读音...
注意点:系统环境:centos7,python,django,svn,jenkins首先安装...
  我使用过的版本控制工具有两种:早期的时候使用的是SVN,...
用好Git和SVN,轻松驾驭版本管理本文从Git与SVN的对比入手,...
01.jenkins安装jenkins网站:https://jenkins.io/安装:资料...
软件环境:centos7jdk1.8svn1.9maven3.5tomcat8jenkins2.80 ...