Java操作SVN(實現文件的解鎖、檢出、更新及提交)---SVNKit學習示例


maven依賴:

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

基類:

package svnkit;
 
public class Constant {
 
    /**
     * SVN路徑
     */
    public static final String SvnPath = "https://***/svn";
 
    /**
     * SVN檢出到目標路徑
     */
    public static final String TargetPath = "E:/resources";
    // public static final String TargetPath = "/root/soft/resources";
 
    /**
     * SVN用戶名
     */
    public static final String SvnUserName = "zhangsan";
 
    /**
     * SVN用戶密碼
     */
    public static final String SvnPassWord = "123456";
 
}

java操作svn:

package svnkit;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
import org.tmatesoft.svn.core.SVNCommitInfo;
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.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatus;
import org.tmatesoft.svn.core.wc.SVNStatusType;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
 
public class SVNKit {
 
    // 更新狀態 true:沒有程序在執行更新,反之則反
    public static Boolean DoUpdateStatus = true;
 
    // 聲明SVN客戶端管理類
    private static SVNClientManager ourClientManager;
 
    /**
     * SVN檢出
     * 
     * @return Boolean
     */
    public static Boolean checkOut() {
        // 初始化庫。 必須先執行此操作。具體操作封裝在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(Constant.SvnPath);
        } catch (SVNException e) {
            e.printStackTrace();
            return false;
        }
 
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
 
        // 實例化客戶端管理類
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, Constant.SvnUserName, Constant.SvnPassWord);
 
        // 要把版本庫的內容check out到的目錄
        File wcDir = new File(Constant.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, false);
            } else {
                ourClientManager.getWCClient().doCleanup(wcDir);
                workingVersion = updateClient.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);
            }
        } 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
     * 
     * @return Boolean
     */
    public static Boolean doCleanup() {
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        // 實例化客戶端管理類
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, Constant.SvnUserName, Constant.SvnPassWord);
 
        // 要把版本庫的內容check out到的目錄
        File wcDir = new File(Constant.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有程序在占用更新)
     */
    public static int doUpdate() {
        if (!SVNKit.DoUpdateStatus) {
            System.out.println("更新程序已經在運行中,不能重復請求!");
            return 0;
        }
        SVNKit.DoUpdateStatus = false;
        /*
         * For using over http:// and https://
         */
        try {
            DAVRepositoryFactory.setup();
 
            ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
            // 實例化客戶端管理類
            ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, Constant.SvnUserName, Constant.SvnPassWord);
            // 要更新的文件
            File updateFile = new File(Constant.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提交 list.add("a.txt")也可直接添加單個文件; list.add("aaa")添加文件夾將添加夾子內所有的文件到svn,預添加文件必須先添加其所在的文件夾;
     * 
     * @param fileRelativePathList文件相對路徑
     * @return Boolean
     */
    public static Boolean doCommit(List<String> fileRelativePathList) {
        // 注意:執行此操作要先執行checkout操作。因為本地需要有工作副本此范例才能運行。
        // 初始化支持svn://協議的庫
        SVNRepositoryFactoryImpl.setup();
 
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        // 實例化客戶端管理類
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, Constant.SvnUserName, Constant.SvnPassWord);
        // 要提交的文件夾子
        File commitFile = new File(Constant.TargetPath);
        // 獲取此文件的狀態(是文件做了修改還是新添加的文件?)
        SVNStatus status = null;
        File addFile = null;
        String strPath = null;
        try {
            if (fileRelativePathList != null && fileRelativePathList.size() > 0) {
                for (int i = 0; i < fileRelativePathList.size(); i++) {
                    strPath = fileRelativePathList.get(i);
                    addFile = new File(Constant.TargetPath + "/" + strPath);
                    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;
    }
 
    /**
     * Svn提交
     * 
     * @param fileRelativePath文件相對路徑
     * @return Boolean
     */
    public static Boolean doCommit(String fileRelativePath) {
        // 注意:執行此操作要先執行checkout操作。因為本地需要有工作副本此范例才能運行。
        // 初始化支持svn://協議的庫
        SVNRepositoryFactoryImpl.setup();
 
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        // 實例化客戶端管理類
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, Constant.SvnUserName, Constant.SvnPassWord);
        // 要提交的文件夾子
        File commitFile = new File(Constant.TargetPath);
        // 獲取此文件的狀態(是文件做了修改還是新添加的文件?)
        SVNStatus status = null;
        File addFile = null;
        try {
            if (fileRelativePath != null && fileRelativePath.trim().length() > 0) {
                addFile = new File(Constant.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;
    }
 
    /**
     * 將文件導入並提交到svn 同路徑文件要是已經存在將會報錯
     * 
     * @param dirPath文件所在文件夾的路徑
     * @return Boolean
     */
    public static Boolean doImport(String dirPath) {
        /*
         * For using over http:// and https://
         */
        DAVRepositoryFactory.setup();
        // 相關變量賦值
        SVNURL repositoryURL = null;
        try {
            repositoryURL = SVNURL.parseURIEncoded(Constant.SvnPath);
        } catch (SVNException e) {
            e.printStackTrace();
        }
 
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
        // 實例化客戶端管理類
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, Constant.SvnUserName, Constant.SvnPassWord);
        // 要把此目錄中的內容導入到版本庫
        File impDir = new File(dirPath);
        // 執行導入操作
        SVNCommitInfo commitInfo = null;
        try {
            commitInfo = ourClientManager.getCommitClient().doImport(impDir, repositoryURL, "import operation!", null, false, false, SVNDepth.INFINITY);
        } catch (SVNException e) {
            e.printStackTrace();
            return false;
        }
        System.out.println(commitInfo.toString());
        return true;
    }
 
    public static void main(String[] args) {
        // System.out.println(checkOut());
        
        System.out.println(doCleanup());
        // System.out.println(doCleanup());
    }
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM