`

java集成svnkit

 
阅读更多
http://beisicao.iteye.com/blog/1112843
一、SVN快速入门
本文主要介绍在windows下使用的方式,对于UNIX环境下,区别并不大。
介绍的具体步骤如下:
 软件下载
 服务器和客户端安装
 建立版本库(Repository)
 配置用户和权限
 运行独立服务器
 初始化导入
 基本客户端操作
1、软件下载
下载Subversion服务器程序
到官方网站的下载二进制安装文件,来到二进制包下载部分,找到 Windows NT, 2000, XP and 2003部分,然后选择" this directory ",这样我们可以看到许多下载的内容,目前可以下载 svn-1.4.0-setup.exe 。
下载Subversion的Windows客户端TortoiseSVN
TortoiseSVN是扩展Windows Shell的一套工具,可以看作Windows资源管理器的插件,安装之后Windows就可以识别Subversion的工作目录。
官方网站是TortoiseSVN ,下载方式和前面的svn服务器类似,在Download页面的我们可以选择下载的版本,目前的最高稳定版本的安装文件为TortoiseSVN-1.4.0.7501-win32-svn-1.4.0.msi。
另外,subversion有相应的eclipse插件,通过此插件可以在eclipse集成开发环境中对开发项目进行版本管理。在这里暂不做介绍。
2、服务器和客户端安装
服务器安装,直接运行svn-1.4.0-setup.exe ,根据提示安装即可,这样我们就有了一套服务器可以运行的环境。
安装TortoiseSVN,同样直接运行TortoiseSVN-1.4.0.7501-win32-svn-1.4.0.msi按照提示安装即可,不过最后完成后会提示是否重启,其实重启只是使svn工作拷贝在windows中的特殊样式生效,与所有的实际功能无关,这里为了立刻看到好的效果,还是重新启动机器。
 
3、建立版本库(Repository)
运行Subversion服务器需要首先要建立一个版本库(Repository),可以看作服务器上存放数据的数据库,在安装了Subversion服务器之后,可以直接运行,如:
svnadmin create E:\svndemo\repository
就会在目录E:\svndemo\repository下创建一个版本库。
我们也可以使用TortoiseSVN图形化的完成这一步:
在目录E:\svndemo\repository下"右键->TortoiseSVN->Create Repository here...“, 然后可以选择版本库模式, 这里使用默认即可, 然后就创建了一系列目录和文件。
4、配置用户和权限
来到E:\svndemo\repository\conf目录,修改svnserve.conf:
# [general]
# password-db = passwd
改为:
[general]
password-db = passwd
然后修改同目录的passwd文件,去掉下面三行的注释:
# [users]
# harry = harryssecret
# sally = sallyssecret
最后变成:
[users]
harry = harryssecret
sally = sallyssecret

5、运行独立服务器
在任意目录下运行:
svnserve -d -r E:\svndemo\repository 我们的服务器程序就已经启动了。注意不要关闭命令行窗口,关闭窗口也会把svnserve停止。
也可以把其设置为windows服务。
6、初始化导入
来到我们想要导入的项目根目录,在这个例子里是E:\svndemo\initproject,目录下有一个readme.txt文件:
右键->TortoiseSVN->Import...
URL of repository输入“svn://localhost/”
ok
完成之后目录没有任何变化,如果没有报错,数据就已经全部导入到了我们刚才定义的版本库中。
需要注意的是,这一步操作可以完全在另一台安装了TortoiseSVN的主机上进行。例如运行svnserve的主机的IP是133.96.121.22,则URL部分输入的内容就是“svn://133.96.121.22/”。
7、基本客户端操作
取出版本库到一个工作拷贝:
来到任意空目录下,在本例中是E:\svndemo\wc1,运行右键->Checkout,在URL of repository中输入svn://localhost/,这样我们就得到了一份工作拷贝。
在工作拷贝中作出修改并提交:
打开readme.txt,作出修改,然后右键->Commit...,这样我们就把修改提交到了版本库,我们可以运行。
察看所作的修改:
readme.txt上右键->TortoiseSVN->Show Log,这样我们就可以看到我们对这个文件所有的提交。在版本1上右键->Compare with working copy,我们可以比较工作拷贝的文件和版本1的区别。


二、SVNKit开发环境
1、 开发环境准备
SVN服务器版本我们选择比较稳定的版本Subversion1.4,安装在windows操作系统上。
SVNKit我们选择1.3.0版本,此版本支持Subversion1.6以下的所有版本。
2、开发环境配置
在开发环境中创建好项目后,在类路径中加上SVNKit的jar包即可开始对subversion进行相关的操作。Jar包有trilead.jar,svnkit-javahl.jar,svnkit-cli.jar,svnkit.jar和jna.jar。
三、SVNKit的结构
SVNKit是一个纯java的subversion客户端库,使用SVNKit无需安装任何subversion客户端,支持各种操作系统。



四、SVNKit类关系图

SVNKit的API主要分为两类:High Level API和Low Level API。
通常情况下,我们使用High Level API即可完成工作任务。High Level API通过封装、使用Low Level API,使开发工作变得相对简单、容易。在此我们重点介绍High Level API。

High Level API介绍:
在High Level API中,我们通过SVNClientManager类即可访问很多接口,这些接口几乎允许subversion用户执行可能需要的任何工作。这些工作包括:checking out、更新、提交、获取历史版本、比较版本间的差异、浏览存储库等等。类图如下图所示:




通过类图我们可以看到,通过SVNClientManager类可以获得各种client操作类的引用,进而可以执行很多操作。
接下来,对client操作类进行分别介绍:
SVNLogClient:
通过此类可以获得版本修订历史记录、浏览存储库条目、文件内容注释。
doLog(…) 用来获取版本的修订历史
doList(…) 用来获取存储库条目树
doAnnotate(…) 用来获取文件内容注释
SVNLogClient的方法和SVN命令行客户端的命令的对应关系。
SVNKit Subversion
doLog() 'svn log'
doList() 'svn list'
doAnnotate() 'svn blame'

SVNUpdateClient:
通过此类可以check out、更新、切换工作副本,也可以从存储库中导出目录或文件。
doCheckOut(…) 从存储库中检出工作副本。
doUpdate(…)把工作副本更新为最新版本或某个指定版本。
doSwitch(…)把工作副本更新为同一个存储库的不同分支上的版本。
doExport(..) 从存储库中导出目录或文件。
doRelocate()把工作副本更新为不同的存储库中的版本。

SVNUpdateClient的方法和SVN命令行客户端的命令的对应关系。 
SVNKit Subversion
doCheckout() 'svn checkout'
doUpdate() 'svn update'
doSwitch() 'svn switch'
doRelocate() 'svn switch --relocate oldURL newURL'
doExport() 'svn export'

SVNWCClient:
此类提供了许多和本地工作副本相关的操作,同时也能访问存储库。
doAdd(…)添加文件、目录到工作副本并且预定添加到存储库。它们会在下次提交上传并添加到存储库中。
doDelete(…)从工作副本中删除一个文件或目录。它们会在下次提交上传并添加到存储库中。
doCleanup(…)递归清理工作副本,删除未完成的工作副本锁定,并恢复未完成的操作。
doInfo(…)获取一个工作副本条目的信息。
doLock(…)锁定工作副本或存储库中的条目,使其他用户不能对条目进行修改。
doUnlock(…)解锁工作副本或存储库中的条目。
doSetProperty(…)对工作副本或存储库中的条目设置属性名和属性值。
doSetrevisionProperty(…)对修订版本的条目设置属性名和属性值。
doGetProperty(…)获得工作副本或存储库中条目的属性值。
doGetRevisionProperty(…)获得修订版本中的条目的属性值。
doRevert(…)取消所有本地编辑。

SVNWCClient的方法和SVN命令行客户端的命令的对应关系。

SVNKit Subversion
doAdd() 'svn add'
doDelete() 'svn delete'
doCleanup() 'svn cleanup'
doInfo() 'svn info'
doLock() 'svn lock'
doUnlock() 'svn unlock'
doSetProperty() 'svn propset PROPNAME PROPVAL PATH'
'svn propdel PROPNAME PATH'
'svn propedit PROPNAME PATH'
doSetRevisionProperty() 'svn propset PROPNAME --revprop –r REV PROPVAL [URL]'
'svn propdel PROPNAME --revprop -r REV [URL]'
'svn propedit PROPNAME --revprop -r REV [URL]'
doGetProperty() 'svn propget PROPNAME PATH'
'svn proplist PATH'
doGetRevisionProperty() 'svn propget PROPNAME --revprop –r REV [URL]'
'svn proplist --revprop -r REV [URL]'
doResolve() 'svn resolved'
doRevert() 'svn revert'

SVNStatusClient:
此类用来获取工作副本条目(文件或目录)的状态信息。
doStatus(…)获得一个工作副本条目的状态。
SVNStatusClient的方法和SVN命令行客户端的命令的对应关系。
SVNKit Subversion
doStatus() 'svn status'


SVNCommitClient:
此类提供了把改变提交到存储库上的一些操作。
doCommit(…)将修改从工作副本提交到存储库。
doImport(…)递归提交一个路径(本地目录)到存储库。
doDelete(…)从存储库中删除一个条目。
doMkDir(…)在存储库中创建一个目录。
SVNCommitClient的方法和SVN命令行客户端的命令的对应关系。
SVNKit Subversion
doCommit() 'svn commit'
doImport() 'svn import'
doDelete() 'svn delete URL'
doMkDir() 'svn mkdir URL'

SVNMoveClient:
此类提供文件在工作副本内移动、取消移动等操作。
doMove(…)把源条目移动到目的条目。
undoMove(…)取消上次的移动操作。
doVirtualCopy(…)复制或移动源文件的版本控制信息到目的文件。

SVNCopyClient:
此类可提供SVN支持的任何复制和移动操作。
doCopy(…)

SVNDiffClient:
此类提供比较不同版本间的差异和合并差异的方法。
doDiff(…)获取两个版本间的差异。
doMerge(…)合并两组文件间的差异。
SVNDiffClient的方法和SVN命令行客户端的命令的对应关系。
SVNKit Subversion
doDiff() 'svn diff'
doMerge() 'svn merge'

五、程序框架
首先新建java项目,把SVNKit的jar包放到项目的类路径下面。
Jar包有trilead.jar,svnkit-javahl.jar,svnkit-cli.jar,svnkit.jar和jna.jar。

程序框架如下所示:
/*第一步: 
*导入可能用到的类 
*/ 
import java.io.*; 
import org.tmatesoft.svn.core.*; 
import org.tmatesoft.svn.core.wc.*; 
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.util.SVNPathUtil; 

public class Demo { 
/*第二步: 
*声明客户端管理类SVNClientManager。 
*/ 
    private static SVNClientManager ourClientManager; 
        
    public static void main(String[] args) throws SVNException { 
        /*第三步: 
         * 对版本库进行初始化操作 (在用版本库进行其他操作前必须进行初始化) 
* 对于通过使用 http:// 和 https:// 访问,执行DAVRepositoryFactory.setup(); 
* 对于通过使用svn:// 和 svn+xxx://访问,执行SVNRepositoryFactoryImpl.setup(); 
* 对于通过使用file:///访问,执行FSRepositoryFactory.setup(); 
* 本程序框架用svn://来访问 
         */ 
       
  SVNRepositoryFactoryImpl.setup();        

        /*第四步: 
         * 要访问版本库的相关变量设置 
         */ 
        //版本库的URL地址 
        SVNURL repositoryURL = null; 
        try { 
            repositoryURL = SVNURL.parseURIEncoded("svn://localhost/testRep"); 
        } catch (SVNException e) { 
            // 
        } 
//版本库的用户名 
        String name = "userName"; 
//版本库的用户名密码 
        String password = "userPassword"; 
//工作副本目录 
        String myWorkingCopyPath = "D:/MyWorkingCopy"; 
//驱动选项 
        ISVNOptions options = SVNWCUtil.createDefaultOptions(true); 
        
        /*第五步: 
         * 创建SVNClientManager的实例。提供认证信息(用户名,密码) 
         * 和驱动选项。 
         */ 
        ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions)options, name, password); 
        
        /*第六步: 
         * 通过SVNClientManager的实例获取要进行操作的client实例(如             *  SVNUpdateClient) 
         * 通过client实例来执行相关的操作。 
* 此框架以check out操作来进行说明,其他操作类似。 
         */ 
       
/*工作副本目录创建*/ 
        File wcDir = new File(myWorkingCopyPath); 
        if (wcDir.exists()) { 
            error("the destination directory '" 
                    + wcDir.getAbsolutePath() + "' already exists!", null); 
        } 
        wcDir.mkdirs(); 

        
        try { 
            /* 
             * 递归的把工作副本从repositoryURL check out 到 wcDir目录。 
             * SVNRevision.HEAD 意味着把最新的版本checked out出来。 
             */ 
           
SVNUpdateClient updateClient = ourClientManager.getUpdateClient(); 
            updateClient.setIgnoreExternals(false); 
updateClient.doCheckout(repositoryURL,wcDir,SVNRevision.HEAD, SVNRevision.HEAD, true); 

        } catch (SVNException svne) { 
            // 
        } 
        
}



六、典型功能实现的范例

环境创建

按快速入门中介绍的方法创建版本库。添加用户test,密码也为test。
启动版本库服务器。访问地址:svn://localhost/。
创建目录:E:\svntest\impDir (把此目录中的内容导入到版本库中)
\impProject
\juniper_config.txt
\cisco_config.txt
E:\svntest\wc     (此目录是工作副本目录)

1、Import操作范例
package demo.wc; 
import java.io.File; 
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.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.SVNWCUtil; 
/*此类执行的操作是把本地目录下的内容上传到版本库中。*/ 
public class DoImport { 
//声明SVN客户端管理类 
private static SVNClientManager ourClientManager; 
public static void main(String[] args) throws Exception { 
//初始化支持svn://协议的库。 必须先执行此操作。 
SVNRepositoryFactoryImpl.setup(); 
//相关变量赋值 
SVNURL repositoryURL = null; 
try { 
repositoryURL = SVNURL.parseURIEncoded("svn://localhost/"); 
} catch (SVNException e) { 
// 
} 
String name = "test"; 
String password = "test"; 
ISVNOptions options = SVNWCUtil.createDefaultOptions(true); 
//实例化客户端管理类 
ourClientManager = SVNClientManager.newInstance( 
(DefaultSVNOptions) options, name, password); 
//要把此目录中的内容导入到版本库 
File impDir = new File("e:/svntest/impDir"); 
//执行导入操作 
SVNCommitInfo commitInfo=ourClientManager.getCommitClient().doImport(impDir, repositoryURL, 
"import operation!",null, false,false,SVNDepth.INFINITY); 
System.out.println(commitInfo.toString()); 

} 



} 




2、checkout操作范例
package demo.wc; 

import java.io.File; 
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.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.SVNUpdateClient; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 

/*此类执行的操作是把版本库中的内容check out到本地目录中*/ 
public class CheckOut { 
//声明SVN客户端管理类 
private static SVNClientManager ourClientManager; 


public static void main(String[] args) throws Exception { 

//初始化支持svn://协议的库。 必须先执行此操作。	

SVNRepositoryFactoryImpl.setup(); 

//相关变量赋值 
SVNURL repositoryURL = null; 
try { 
repositoryURL = SVNURL.parseURIEncoded("svn://localhost/"); 
} catch (SVNException e) { 
// 
} 
String name = "test"; 
String password = "test";	
ISVNOptions options = SVNWCUtil.createDefaultOptions(true); 

//实例化客户端管理类 
ourClientManager = SVNClientManager.newInstance( 
(DefaultSVNOptions) options, name, password); 

//要把版本库的内容check out到的目录 
File wcDir = new File("e:/svntest/wc"); 

//通过客户端管理类获得updateClient类的实例。 
SVNUpdateClient updateClient = ourClientManager.getUpdateClient(); 
/* 
* sets externals not to be ignored during the checkout 
*/ 
updateClient.setIgnoreExternals(false); 

//执行check out 操作,返回工作副本的版本号。 
long workingVersion= updateClient 
.doCheckout(repositoryURL, wcDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY,false); 

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

} 


}



3、commit操作范例
注意:执行此操作要先
执行checkout操作。因为本地需要有工作副本此范例才能运行。
package demo.wc; 

import java.io.File; 

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.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.SVNStatus; 
import org.tmatesoft.svn.core.wc.SVNStatusType; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 
/*此类的操作是把工作副本的某个文件提交到版本库中*/ 
public class DoCommit { 
//	声明SVN客户端管理类 
private static SVNClientManager ourClientManager; 

public static void main(String[] args) throws Exception { 
//初始化支持svn://协议的库。 必须先执行此操作。 
SVNRepositoryFactoryImpl.setup(); 
//相关变量赋值 
SVNURL repositoryURL = null; 
try { 
repositoryURL = SVNURL.parseURIEncoded("svn://localhost/"); 
} catch (SVNException e) { 
// 
} 
String name = "test"; 
String password = "test"; 
ISVNOptions options = SVNWCUtil.createDefaultOptions(true); 
//实例化客户端管理类 
ourClientManager = SVNClientManager.newInstance( 
(DefaultSVNOptions) options, name, password); 
//要提交的文件 
File commitFile=new File("e:/svntest/wc/impProject/juniper_config.txt"); 
//获取此文件的状态(是文件做了修改还是新添加的文件?) 
SVNStatus status=ourClientManager.getStatusClient().doStatus(commitFile, true); 
//如果此文件是新增加的则先把此文件添加到版本库,然后提交。 
if(status.getContentsStatus()==SVNStatusType.STATUS_UNVERSIONED){ 
//把此文件增加到版本库中 
ourClientManager.getWCClient().doAdd(commitFile, false, false, false, SVNDepth.INFINITY,false,false); 
//提交此文件 
ourClientManager.getCommitClient().doCommit( 
new File[] { commitFile }, true, "",null,null,true, false, SVNDepth.INFINITY); 
System.out.println("add"); 
} 
//如果此文件不是新增加的,直接提交。 
else{ 
//	ourClientManager.getCommitClient().doCommit( 
//	new File[] { commitFile }, true, "", false, true); 
ourClientManager.getCommitClient().doCommit( 
new File[] { commitFile }, true, "",null,null,true, false, SVNDepth.INFINITY); 
System.out.println("commit"); 
} 
System.out.println(status.getContentsStatus()); 

} 



}


4、update操作范例
注意:执行此操作要先执行checkout操作。因为本地需要有工作副本此范例才能运行。
package demo.wc; 

import java.io.File; 
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.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.SVNUpdateClient; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 

/*此类用来把版本库中文件的某个版本更新到工作副本中*/ 
public class DoUpdate { 
//声明SVN客户端管理类 
private static SVNClientManager ourClientManager; 

public static void main(String[] args) throws Exception { 
//初始化支持svn://协议的库。 必须先执行此操作。 
SVNRepositoryFactoryImpl.setup(); 
//相关变量赋值 
SVNURL repositoryURL = null; 
try { 
repositoryURL = SVNURL.parseURIEncoded("svn://localhost"); 
} catch (SVNException e) { 
// 
} 
String name = "test"; 
String password = "test"; 

ISVNOptions options = SVNWCUtil.createDefaultOptions(true); 
//实例化客户端管理类 
ourClientManager = SVNClientManager.newInstance( 
(DefaultSVNOptions) options, name, password); 
//要更新的文件 
File updateFile=new File("e:/svntest/wc/impProject/juniper_config.txt"); 
//获得updateClient的实例 
SVNUpdateClient updateClient = ourClientManager.getUpdateClient(); 
updateClient.setIgnoreExternals(false); 
//执行更新操作 
long versionNum= updateClient.doUpdate(updateFile, SVNRevision.HEAD, SVNDepth.INFINITY,false,false); 
System.out.println("工作副本更新后的版本:"+versionNum); 

} 


} 



5、版本差异比较操作范例
注意:执行此操作要先执行checkout操作。因为本地需要有工作副本此范例才能运行。
package demo.wc; 

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 

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.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.SVNDiffClient; 
import org.tmatesoft.svn.core.wc.SVNRevision; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 
/*此类用来比较某个文件两个版本的差异*/ 
public class DoDiff { 
//声明SVN客户端管理类 
private static SVNClientManager ourClientManager; 

public static void main(String[] args) throws Exception { 
//初始化支持svn://协议的库。 必须先执行此操作。 
SVNRepositoryFactoryImpl.setup(); 
//相关变量赋值 
SVNURL repositoryURL = null;//在此例中用不上。 
try { 
repositoryURL = SVNURL.parseURIEncoded("svn://localhost"); 
} catch (SVNException e) { 
// 
} 
String name = "test"; 
String password = "test"; 
ISVNOptions options = SVNWCUtil.createDefaultOptions(true); 
//实例化客户端管理类 
ourClientManager = SVNClientManager.newInstance( 
(DefaultSVNOptions) options, name, password); 
//要比较的文件 
File compFile = new File("e:/svntest/wc/impProject/juniper_config.txt"); 
//获得SVNDiffClient类的实例。 
SVNDiffClient diff=ourClientManager.getDiffClient(); 
//保存比较结果的输出流 
BufferedOutputStream result =new BufferedOutputStream(new FileOutputStream("E:/result.txt")); 
//比较compFile文件的SVNRevision.WORKING版本和	SVNRevision.HEAD版本的差异,结果保存在E:/result.txt文件中。 
//SVNRevision.WORKING版本指工作副本中当前内容的版本,SVNRevision.HEAD版本指的是版本库中最新的版本。 
diff.doDiff(compFile, SVNRevision.HEAD, SVNRevision.WORKING, SVNRevision.HEAD, SVNDepth.INFINITY, true, result,null); 
result.close(); 
System.out.println("比较的结果保存在E:/result.txt文件中!"); 

} 


} 


6、浏览版本库操作范例
此范例用了底层API.和高层 API的程序框架有所不同。
package demo.wc; 

import java.util.Collection; 
import java.util.Iterator; 

import org.tmatesoft.svn.core.SVNDirEntry; 
import org.tmatesoft.svn.core.SVNException; 
import org.tmatesoft.svn.core.SVNNodeKind; 
import org.tmatesoft.svn.core.SVNURL; 
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; 
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; 
import org.tmatesoft.svn.core.io.SVNRepository; 
import org.tmatesoft.svn.core.io.SVNRepositoryFactory; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 

/* 
* 此类用来显示版本库的树状结构。 
* 此类用底层API(Low Level API)直接访问版本库。 
* 此程序框架于1-5的示例(High Level API)稍有不同。 
* */ 
public class DisplayRepositoryTree { 
    
    public static void main(String[] args) { 
       

//    初始化支持svn://协议的库。 必须先执行此操作。	
SVNRepositoryFactoryImpl.setup(); 
        
        /* 
         * 相关变量赋值 
         */ 
        String url = "svn://localhost"; 
        String name = "test"; 
        String password = "test"; 
        //定义svn版本库的URL。 
        SVNURL repositoryURL = null; 
        //定义版本库。 
        SVNRepository repository = null; 
        /* 
         * 实例化版本库类 
         * */ 
        try { 
        //获取SVN的URL。 
        repositoryURL=SVNURL.parseURIEncoded(url); 
        //根据URL实例化SVN版本库。 
            repository = SVNRepositoryFactory.create(repositoryURL); 
        } catch (SVNException svne) { 
            /* 
             * 打印版本库实例创建失败的异常。 
             */ 
            System.err 
                    .println("创建版本库实例时失败,版本库的URL是 '" 
                            + url + "': " + svne.getMessage()); 
            System.exit(1); 
        } 

        /* 
         * 对版本库设置认证信息。 
         */ 
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password); 
        repository.setAuthenticationManager(authManager); 

        /* 
         * 上面的代码基本上是固定的操作。 
         * 下面的部门根据任务不同,执行不同的操作。 
         * */ 
        try { 

            //打印版本库的根 
            System.out.println("Repository Root: " + repository.getRepositoryRoot(true)); 
            //打印出版本库的UUID 
            System.out.println("Repository UUID: " + repository.getRepositoryUUID(true)); 
            System.out.println(""); 
            //打印版本库的目录树结构 
            listEntries(repository, ""); 
        } catch (SVNException svne) { 
            System.err.println("打印版本树时发生错误: " 
                    + svne.getMessage()); 
            System.exit(1); 
        } 
        /* 
         * 获得版本库的最新版本树 
         */ 
        long latestRevision = -1; 
        try { 
            latestRevision = repository.getLatestRevision(); 
        } catch (SVNException svne) { 
            System.err 
                    .println("获取最新版本号时出错: " 
                            + svne.getMessage()); 
            System.exit(1); 
        } 
        System.out.println(""); 
        System.out.println("---------------------------------------------"); 
        System.out.println("版本库的最新版本是: " + latestRevision); 
        System.exit(0); 
    } 

   
    /* 
     * 此函数递归的获取版本库中某一目录下的所有条目。 
     */ 
    public static void listEntries(SVNRepository repository, String path) 
            throws SVNException { 
        //获取版本库的path目录下的所有条目。参数-1表示是最新版本。 
        Collection entries = repository.getDir(path, -1, null, 
                (Collection) null); 
        Iterator iterator = entries.iterator(); 
        while (iterator.hasNext()) { 
            SVNDirEntry entry = (SVNDirEntry) iterator.next(); 
            System.out.println("/" + (path.equals("") ? "" : path + "/") 
                    + entry.getName() + " (author: '" + entry.getAuthor() 
                    + "'; revision: " + entry.getRevision() + "; date: " + entry.getDate() + ")"); 
            /* 
             * 检查此条目是否为目录,如果为目录递归执行 
             */ 
            if (entry.getKind() == SVNNodeKind.DIR) { 
                listEntries(repository, (path.equals("")) ? entry.getName() 
                        : path + "/" + entry.getName()); 
            } 
        } 
    } 
} 


7、显示文件内容操作范例
此范例用了底层API.和高层 API的程序框架稍有不同。
package demo.wc; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.util.Iterator; 
import org.tmatesoft.svn.core.SVNException; 
import org.tmatesoft.svn.core.SVNNodeKind; 
import org.tmatesoft.svn.core.SVNProperties; 
import org.tmatesoft.svn.core.SVNProperty; 
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.fs.FSRepositoryFactory; 
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; 
import org.tmatesoft.svn.core.io.SVNRepository; 
import org.tmatesoft.svn.core.io.SVNRepositoryFactory; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 

/*此类用来显示版本库中文件的内容。 
* 此类用底层API(Low Level API)直接访问版本库。 
* 此程序框架与1-5的示例稍有不同。 
* */ 
public class DisplayFile { 
    public static void main(String[] args) { 
        
    //初始化库。 必须先执行此操作。具体操作封装在setupLibrary方法中。 
        setupLibrary(); 
       
        /* 
         * 相关变量赋值 
         */ 
        String url = "svn://localhost"; 
        String name = "test"; 
        String password = "test"; 
        String filePath = "impProject/juniper_config.txt"; 
        //定义svn版本库的URL。 
        SVNURL repositoryURL = null; 
        //定义版本库。 
        SVNRepository repository = null; 
        try { 
        //获取SVN的URL。 
        repositoryURL=SVNURL.parseURIEncoded(url); 
        //根据URL实例化SVN版本库。 
            repository = SVNRepositoryFactory.create(repositoryURL); 
        } catch (SVNException svne) { 
            /* 
             * 打印版本库实例创建失败的异常。 
             */ 
            System.err 
                    .println("创建版本库实例时失败,版本库的URL是 '" 
                            + url + "': " + svne.getMessage()); 
            System.exit(1); 
        } 

        /* 
         * 对版本库设置认证信息。 
         */ 
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password); 
        repository.setAuthenticationManager(authManager); 

        //此变量用来存放要查看的文件的属性名/属性值列表。 
        SVNProperties fileProperties = new SVNProperties(); 
        //此输出流用来存放要查看的文件的内容。 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

        try { 
            //获得版本库中文件的类型状态(是否存在、是目录还是文件),参数-1表示是版本库中的最新版本。 
            SVNNodeKind nodeKind = repository.checkPath(filePath, -1); 
            
            if (nodeKind == SVNNodeKind.NONE) { 
                System.err.println("要查看的文件在 '" + url + "'中不存在."); 
                System.exit(1); 
            } else if (nodeKind == SVNNodeKind.DIR) { 
                System.err.println("要查看对应版本的条目在 '" + url 
                        + "'中是一个目录."); 
                System.exit(1); 
            } 
            //获取要查看文件的内容和属性,结果保存在baos和fileProperties变量中。 
            repository.getFile(filePath, -1, fileProperties, baos); 

        } catch (SVNException svne) { 
            System.err.println("在获取文件内容和属性时发生错误: " + svne.getMessage()); 
            System.exit(1); 
        } 

        //获取文件的mime-type 
        String mimeType = fileProperties.getStringValue(SVNProperty.MIME_TYPE); 
        //判断此文件是否是文本文件        
        boolean isTextType = SVNProperty.isTextMimeType(mimeType); 
        /* 
         * 显示文件的所有属性 
         */ 
        Iterator iterator = fileProperties.nameSet().iterator(); 
        while (iterator.hasNext()) { 
            String propertyName = (String) iterator.next(); 
            String propertyValue = fileProperties.getStringValue(propertyName); 
            System.out.println("文件的属性: " + propertyName + "=" 
                    + propertyValue); 
        } 
        /* 
         * 如果文件是文本类型,则把文件的内容显示到控制台。 
         */ 
        if (isTextType) { 
            System.out.println("File contents:"); 
            System.out.println(); 
            try { 
                baos.writeTo(System.out); 
            } catch (IOException ioe) { 
                ioe.printStackTrace(); 
            } 
        } else { 
            System.out 
                    .println("因为文件不是文本文件,无法显示!"); 
        } 
        /* 
         * 获得版本库的最新版本号。 
         */ 
        long latestRevision = -1; 
        try { 
            latestRevision = repository.getLatestRevision(); 
        } catch (SVNException svne) { 
            System.err.println("获取最新版本号时出错: " + svne.getMessage()); 
            System.exit(1); 
        } 
        System.out.println(""); 
        System.out.println("---------------------------------------------"); 
        System.out.println("版本库的最新版本号: " + latestRevision); 
        System.exit(0); 
    } 

    /* 
     * 初始化库 
     */ 
    private static void setupLibrary() { 
        /* 
         * For using over http:// and https:// 
         */ 
        DAVRepositoryFactory.setup(); 
        /* 
         * For using over svn:// and svn+xxx:// 
         */ 
        SVNRepositoryFactoryImpl.setup(); 
        
        /* 
         * For using over file:/// 
         */ 
        FSRepositoryFactory.setup(); 
    } 
}



七、参考资源
http://www.svnkit.com/documentation.html :里面有SVNKit的介绍及详细的示例代码。

SVNKit工具包中的javadoc文档:里面有详细的类及类方法的介绍。

SVNKit下载地址:http://www.svnkit.com
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics