zoukankan      html  css  js  c++  java
  • 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;
        }
    
    }
  • 相关阅读:
    LUA 数据比较BUG?????是不是BUG大佬帮看看
    win10 优化批处理
    android Studio 二维码扫一扫 使用精简过的zxing
    AppCompatActivity 去除标题栏
    java 继承 重写 重载 super关键字
    java构造方法私有化
    java代码块的理解
    java 理解main方法
    java面向对象基础static
    docker 安装 nginx
  • 原文地址:https://www.cnblogs.com/lyrongblog/p/14242995.html
Copyright © 2011-2022 走看看