zoukankan      html  css  js  c++  java
  • SVNKit完成前台Tree列表

    SVNKit是JAVA操作SVN的一个jar包,里面提供了各种丰富的方法,看了很多大神的博客,了解了SVNKit的结构,这里不再细说。

    简单介绍下前台查看SVN Tree的一种后台实现方式。这里并没有采用递归方式。

    这里简单思考下我们应该设计什么样的接口来满足前段的需求,上图只是小乌龟的界面,前台树状图也差不多是这个形状,姑且先这样理解前台界面,

    每次当前台点击△传递给我们一个当前的SVN URL,我们返回前台下层目录或文件以及URL,这样后台的接口基本上可以确定,我们需要的参数一定有URL,我们操作小乌龟的时候会用到用户名和密码,这样简单的参数我们就锁定了,

    分别是,URL,用户名,密码。

    public static List<SVNKitInfoDto> getSVNTreeInfo(String url, String userName, String passWord);
    

    现在我们来看下代码实现

    package com.thc.tenantcenter.util;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.util.StringUtils;
    import org.tmatesoft.svn.core.SVNDirEntry;
    import org.tmatesoft.svn.core.SVNException;
    import org.tmatesoft.svn.core.SVNLogEntry;
    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.io.SVNRepository;
    import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
    import org.tmatesoft.svn.core.wc.SVNWCUtil;
    
    import com.thc.tenantcenter.constant.TenantcentConstant;
    import com.thc.tenantcenter.dto.SVNKitInfoDto;
    
    /**  
     * @Description: 利用SVNKit进行SVN 提交 修改 查询 删除等操作
     *
     ********************************************************
     * @author                 @date             @version       
     * author            2017年11月15日                         1.0.0
     ********************************************************
     * @update 
     */
    public class SVNKitUtils {
    
    	private static final Log LOG = LogFactory.getLog(SVNKitUtils.class);
    	
    	private static final String HOME_SVN_URL = "http://svn.everjiankang.com/svn/healthcare";
    	
    	private static final Integer LOG_NUM = 5;
    	
    	/**
    	 * 
    	 * @Description: 获取SVNTree
    	 * @param url svn
    	 * @param userName
    	 * @param passWord
    	 * @return
    	 *
    	 * @author: author
    	 * @date: 2017年11月15日
    	 * @version: 1.0.0
    	 */
    	public static List<SVNKitInfoDto> getSVNTreeInfo(String url, String userName, String passWord) {
    		if (StringUtils.isEmpty(url)) {
    			url = HOME_SVN_URL;
    		}
    		return getSVNTreeInfos(getSVNRepository(url, userName, passWord));
    	}
    	
    	/**
    	 * 
    	 * @Description: 获取SVN版本号集合
    	 * @param url
    	 * @param userName
    	 * @param passWord
    	 * @param startDate
    	 * @return
    	 *
    	 * @author: author
    	 * @date: 2017年11月15日
    	 * @version: 1.0.0
    	 */
    	public static SVNKitInfoDto getSVNVersions(String url, String userName, String passWord, Date startDate) {
    		SVNKitInfoDto sVNKitInfoDto = new SVNKitInfoDto();
    		try {
    			sVNKitInfoDto.setFileUrl(url);
    			sVNKitInfoDto.setVersionNos(getSVNVersions(getSVNRepository(url, userName, passWord), startDate));
    		} catch (SVNException e) {
    			LOG.error("SVNKitUtils getSVNVersions is error", e);
    		}
    		return sVNKitInfoDto;
    	}
    	
    	/**
    	 * @Description: 获取SVNRepository
    	 * @return sVNRepository
    	 *
    	 * @author: author
    	 * @date: 2017年11月15日
    	 * @version: 1.0.0
    	 */
    	private static SVNRepository getSVNRepository(String url, String userName, String passWord) {
    		//1.根据访问协议初始化工厂
            DAVRepositoryFactory.setup();;
            //2.初始化仓库
            SVNRepository svnRepository = null;
    		try {
    			svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
    		} catch (SVNException e) {
    			LOG.error("SVNKitUtils getSVNRepository is error", e);
    		}
            //3.创建一个访问的权限
            char[] pwd = passWord.toCharArray();
            ISVNAuthenticationManager authenticationManager = SVNWCUtil.createDefaultAuthenticationManager(userName, pwd);
            svnRepository.setAuthenticationManager(authenticationManager);
    		return svnRepository;
    	}
    	
    	/**
    	 * @Description: 获取路径以及历史版本号
    	 * @param svnRepository
    	 *
    	 * @author: author
    	 * @date: 2017年11月15日
    	 * @version: 1.0.0
    	 */
    	@SuppressWarnings("unchecked")
    	private static List<SVNKitInfoDto> getSVNTreeInfos(SVNRepository svnRepository){
    		List<SVNKitInfoDto> svnDtoInfos = new ArrayList<>();
    		try {
    			Collection<SVNDirEntry> entry = svnRepository.getDir(TenantcentConstant.EMPTY, -1 ,null,(Collection<SVNDirEntry>)null);
    			for (SVNDirEntry svnDirEntry : entry) {
    				SVNKitInfoDto svnDtoInfo = new SVNKitInfoDto();
    				svnDtoInfo.setFileName(svnDirEntry.getName());
    				svnDtoInfo.setFileUrl(svnDirEntry.getURL().toString());
    				svnDtoInfos.add(svnDtoInfo);
    			}
    		} catch (SVNException e) {
    			LOG.error("SVNKitUtils getSVNRepository is error", e);
    		}
    		return svnDtoInfos;
    	}
    
    	/**
    	 * 
    	 * @Description: 获取SVN历史
    	 * @param svnRepository
    	 * @throws SVNException
    	 *
    	 * @author: author
    	 * @date: 2017年11月15日
    	 * @version: 1.0.0
    	 * @param startDate 
    	 */
    	@SuppressWarnings("rawtypes")
    	private static List<Long> getSVNVersions(SVNRepository svnRepository, Date startDate) throws SVNException {
    		List<Long> versions = new ArrayList<>();
    		Collection logEntries = svnRepository.log(new String[]{""}, null, svnRepository.getDatedRevision(startDate), svnRepository.getLatestRevision(), true, true);
    		Iterator it = logEntries.iterator();
            while (it.hasNext()){
            	SVNLogEntry svnLogEntry = (SVNLogEntry)it.next();
            	versions.add(svnLogEntry.getRevision());
            }
            return getLimitLogNum(versions);
    	}
    
    	/**
    	 * 
    	 * @Description: 只取最新的版本号中的五条
    	 * @param versions 所有版本号集合
    	 * @return
    	 *
    	 * @author: author
    	 * @date: 2017年11月15日
    	 * @version: 1.0.0
    	 */
    	private static List<Long> getLimitLogNum(List<Long> versions) {
    		if (versions.size() <= LOG_NUM) {
    			return versions;
    		}
    		List<Long> limitVersions = new ArrayList<>();
    		for (int i = 0; i < versions.size(); i++) {
    			if (i > versions.size() - 1 - LOG_NUM) {
    				limitVersions.add(versions.get(i));
    			}
    		}
    		return limitVersions;
    	}
    }
    

    首先拿到SVNRepository这里需要做的事情要给定URL和用户名还有密码,接着调用getDir方法获取目录结构,到这里目录结构或文件我们已经获取完毕。封装成一个DTO返回给前台,这样任务就算简单的完成。这里这是给的SVNKitUtil,接口还是要自己去写

    以上代码还提供了查看文件的版本号,当然我用到的是公司系统自动化构建,可能小伙伴不太需要,简单的说下实现思路。

    第一步:拿到SVNRepository

    第二步:取开始时间的版本号

    第三步:调用log方法

    第四步:返回版本号开始时间到最新版本号集合

    当然这里是可以扩展成时间段的版本号等。

    以上只是简单的例子,具体参数含义查看官方api:https://svnkit.com/javadoc/index.html

    希望能帮助童鞋们,如果有什么地方不是很清楚的,欢迎留言!

  • 相关阅读:
    linux目录结构介绍
    Linux下安装Redis
    SpringBoot启动器详解pom.xml
    Linux(CentOS6.X)安装mysql
    CentOS 7 安装 MySQL
    GIT和SVN的区别
    SourceTree安装教程
    Git版本控制软件结合GitHub从入门到精通常用命令学习手册
    WIN下Git GUI 教程
    把文件(项目)上传到Git@OSC
  • 原文地址:https://www.cnblogs.com/zhuxiansheng/p/7840261.html
Copyright © 2011-2022 走看看