前段时间由于业务需要就写了这些代码,防止以后用得到就粘贴在这里了。
思路很简单。利用svnkit开发工具包就可以简单实现。以下为代码。
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.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.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
/**
* svn导出工具类
* @author Administrator
*
*/
public class SVNCheckOut {
@SuppressWarnings("static-access")
public static void main(String[] args){
SVNCheckOut test = new SVNCheckOut();
test. checkOut("svn://192.168.1.235/website/docs/db/","D:/a","xiaofeng","");
}
// 检出文件
/**
*
* @param url svn地址必须是以svn:开头的
* @param desPath 导出目录地址
* @param username svn用户名
* @param password svn密码
*/
public static void checkOut(String url,String desPath,String username,String password){
SVNClientManager ourClientManager;
//初始化支持svn://协议的库。 必须先执行此操作。
SVNRepositoryFactoryImpl.setup();
//相关变量赋值
SVNURL repositoryURL = null;
try {
repositoryURL = SVNURL.parseURIEncoded(url);
} catch (SVNException e) {
System.out.println("初始化svn地址失败"+url);
System.out.println(e);
}
FSRepositoryFactory.setup();
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
//实例化客户端管理类
ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options);//无密码
//ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options,username,password);
//要把版本库的内容check out到的目录
File desFile = new File(desPath);
//通过客户端管理类获得updateClient类的实例。
SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
//返回工作版本号
long workingVersion = updateClient.doCheckout(repositoryURL, desFile, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY,true);
System.out.println("把版本:"+workingVersion+" check out 到目录:"+desPath+"中");
} catch (SVNException e) {
System.out.println("检出代码出错:"+e);
}
}
}