微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
    }

}

 

原文地址:https://www.cnblogs.com/lyrongblog/p/14242995.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐