jfinal+SVNKit 关联svn服务器进行文件检出、更新、上传


感谢两位博主
参考文章1: https://blog.csdn.net/dai_haijiao/article/details/80221660
参考文章2: https://blog.csdn.net/bfhx1314/article/details/17072517

1、maven项目使用依赖

	<dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.9.2</version>
	</dependency>

2、根据将所需的文件进行配置放置在配置文件下

	#svn账号密码
	SvnUserName = test
	SvnPassWord = test
	
	#项目在svn服务器端的地址
	SvnPath = 

jfinal如何获取配置文件的值,请参考jfinal社区:http://www.jfinal.com/doc/2-9

3、SVNRelation类

其中xx.getConfig(“SvnUserName”) 可直接使用字符串代替,对应为账号与密码

在本方法中进行测试时SVNUpdateClient.doCheckout();方法中参数allowUnversionedObstructions 设置为false时,检出svn文件到本地会报错误;改为true时能够正常检出到本地。

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
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.wc.*;

import java.io.File;
import java.util.List;

public class SVNRelation {

    // 更新状态 true:没有程序在执行更新,反之则反
    public static Boolean DoUpdateStatus = true;

    // 声明SVN客户端管理类
    private static SVNClientManager ourClientManager;

    /**
     *  SVN检出
     * @param svnPath svn服务器端地址
     * @param targetPath  检出到的本地地址(绝对路径)
     * @return
     */
    public static Boolean checkOut(String svnPath,String targetPath) {
        // 初始化库。 必须先执行此操作。具体操作封装在setupLibrary方法中。
        /*
         * For using over http:// and https://
         */
        DAVRepositoryFactory.setup();
        /*
         * For using over svn:// and svn+xxx://
         */
        SVNRepositoryFactoryImpl.setup();

        /*
         * For using over file:///
         */
        FSRepositoryFactory.setup();

        // 相关变量赋值
        SVNURL repositoryURL = null;
        try {
            repositoryURL = SVNURL.parseURIEncoded(svnPath);
        } catch (SVNException e) {
            e.printStackTrace();
            return false;
        }

        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);

        // 实例化客户端管理类
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));

        // 要把版本库的内容check out到的目录
        File wcDir = new File(targetPath);

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

        updateClient.setIgnoreExternals(false);

        // 执行check out 操作,返回工作副本的版本号。
        long workingVersion = -1;
        try {
            if (!wcDir.exists()) {
                workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);
            } else {
//                ourClientManager.getWCClient().doCleanup(wcDir);
                workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);
            }
        } catch (SVNException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        System.out.println("把版本:" + workingVersion + " check out 到目录:" + wcDir + "中。");
        return true;

    }

    /**
     * 解除svn Luck
     * @param targetPath 检出到的本地地址
     * @return
     */
    public static Boolean doCleanup(String targetPath) {
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        // 实例化客户端管理类
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));

        // 要把版本库的内容check out到的目录
        File wcDir = new File(targetPath);
        if (wcDir.exists()) {
            try {
                ourClientManager.getWCClient().doCleanup(wcDir);
            } catch (SVNException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * 更新svn
     *
     * @return int(-1更新失败,1成功,0有程序在占用更新)
     */
    /**
     *  更新svn
     *
     * @param targetPath  项目对应的本地路径
     * @return
     */
    public static int doUpdate(String targetPath) {
        if (!SVNRelation.DoUpdateStatus) {
            System.out.println("更新程序已经在运行中,不能重复请求!");
            return 0;
        }
        SVNRelation.DoUpdateStatus = false;
        /*
         * For using over http:// and https://
         */
        try {
            DAVRepositoryFactory.setup();

            ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
            // 实例化客户端管理类
            ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options,  xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));
            // 要更新的文件
            File updateFile = new File(targetPath);
            // 获得updateClient的实例
            SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
            updateClient.setIgnoreExternals(false);
            // 执行更新操作
            long versionNum = updateClient.doUpdate(updateFile, SVNRevision.HEAD, SVNDepth.INFINITY, false, false);
            System.out.println("工作副本更新后的版本:" + versionNum);
            DoUpdateStatus = true;
            return 1;
        } catch (SVNException e) {
            DoUpdateStatus = true;
            e.printStackTrace();
            return -1;
        }
    }


    /**
     * Svn提交
     *
     *  *说明:副文本路径+相对路径  = 文件在服务器的完整路径。
     * @param fileRelativePath 文件相对路径(待提交的文件)
     * @param targetPath  项目对应的svn副本路径(本地路径)
     * @return
     */
    public static Boolean doCommit(String fileRelativePath,String targetPath) {
        // 注意:执行此操作要先执行checkout操作。因为本地需要有工作副本此范例才能运行。
        // 初始化支持svn://协议的库
        SVNRepositoryFactoryImpl.setup();

        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        // 实例化客户端管理类
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options,  xx.getConfig("SvnUserName") ,xx.getConfig("SvnPassWord"));
        // 要提交的文件夹子
        File commitFile = new File(targetPath);
        // 获取此文件的状态(是文件做了修改还是新添加的文件?)
        SVNStatus status = null;
        File addFile = null;
        try {
            if (fileRelativePath != null && fileRelativePath.trim().length() > 0) {
                addFile = new File(targetPath + "/" + fileRelativePath);
                status = ourClientManager.getStatusClient().doStatus(addFile, true);
                // 如果此文件是新增加的则先把此文件添加到版本库,然后提交。
                if (null == status || status.getContentsStatus() == SVNStatusType.STATUS_UNVERSIONED) {
                    // 把此文件增加到版本库中
                    ourClientManager.getWCClient().doAdd(addFile, false, false, false, SVNDepth.INFINITY, false, false);
                    System.out.println("add");
                }
                // 提交此文件
                ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
                System.out.println("commit");
            }
            // 如果此文件不是新增加的,直接提交。
            else {
                ourClientManager.getCommitClient().doCommit(new File[] { commitFile }, true, "", null, null, true, false, SVNDepth.INFINITY);
                System.out.println("commit");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        // System.out.println(status.getContentsStatus());
        return true;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值