java中使用svnkit实现文件的版本管理
一、引入svnKit依赖
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.9.3</version>
</dependency>
二、初始化仓库工厂类
本文参考来自于svnkit官网
在使用svn客户端进行各种操作时第一步需要初始化仓库工厂
/*
* Initializes the library to work with a repository via
* different protocols.
*/
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();
}
二、使用svnkit创建本地存储仓库
try {
String tgtPath = "d:/modelFiles/repos";
SVNURL tgtURL = SVNRepositoryFactory.createLocalRepository( new File( tgtPath ), true , false );
} catch ( SVNException e ) {
//handle exception
}
执行完这一段代码之后,我们可以在本地目录下发现以下目录结构
三、svn基本原子操作
private static SVNClientManager ourClientManager;
private static ISVNEventHandler myCommitEventHandler;
private static ISVNEventHandler myUpdateEventHandler;
private static ISVNEventHandler myWCEventHandler;
/*
* Puts directories and files under version control scheduling them for addition
* to a repository. They will be added in a next commit. Like 'svn add PATH'
* command. It's done by invoking
*
* SVNWCClient.doAdd(File path, boolean force,
* boolean mkdir, boolean climbUnversionedParents, boolean recursive)
*
* which takes the following parameters:
*
* path - an entry to be scheduled for addition;
*
* force - set to true to force an addition of an entry anyway;
*
* mkdir - if true doAdd(..) creates an empty directory at path and schedules
* it for addition, like 'svn mkdir PATH' command;
*
* climbUnversionedParents - if true and the parent of the entry to be scheduled
* for addition is not under version control, then doAdd(..) automatically schedules
* the parent for addition, too;
*
* recursive - if true and an entry is a directory then doAdd(..) recursively
* schedules all its inner dir entries for addition as well.
*/
// 将文件纳入版本控制
private static void addEntry(File wcPath) throws SVNException {
ourClientManager.getWCClient().doAdd(wcPath, false, false, false, true);
}
/*
* Updates a working copy (brings changes from the repository into the working copy).
* Like 'svn update PATH' command; It's done by invoking
*
* SVNUpdateClient.doUpdate(File file, SVNRevision revision, boolean recursive)
*
* which takes the following parameters:
*
* file - a working copy entry that is to be updated;
*
* revision - a revision to which a working copy is to be updated;
*
* recursive - if true and an entry is a directory then doUpdate(..) recursively
* updates the entire directory, otherwise - only child entries of the directory;
*/
// 将本地文件更新到指定版本,前提是本地有该文件的其他版本
private static long update(File wcPath,
SVNRevision updateToRevision, boolean isRecursive)
throws SVNException {
SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
/*
* sets externals not to be ignored during the update
*/
updateClient.setIgnoreExternals(false);
/*
* returns the number of the revision wcPath was updated to
*/
return updateClient.doUpdate(wcPath, updateToRevision, isRecursive);
}
/*
* Checks out a working copy from a repository. Like 'svn checkout URL[@REV] PATH (-r..)'
* command; It's done by invoking
*
* SVNUpdateClient.doCheckout(SVNURL url, File dstPath, SVNRevision pegRevision,
* SVNRevision revision, boolean recursive)
*
* which takes the following parameters:
*
* url - a repository location from where a working copy is to be checked out;
*
* dstPath - a local path where the working copy will be fetched into;
*
* pegRevision - an SVNRevision representing a revision to concretize
* url (what exactly URL a user means and is sure of being the URL he needs); in other
* words that is the revision in which the URL is first looked up;
*
* revision - a revision at which a working copy being checked out is to be;
*
* recursive - if true and url corresponds to a directory then doCheckout(..) recursively
* fetches out the entire directory, otherwise - only child entries of the directory;
*/
// svn检出操作,即从svn仓库拉取指定版本的代码