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;
/**
* api调用svn下载
*
* @date 2016-06-14
*
* 实现api调用svn下载功能,至少需要以下jar包:
* svnkit-1.8.12.jar
* svnkit-cli-1.8.12.jar
* antlr-runtime-3.4.jar
* sequence-library-1.0.3.jar
* sqljet-1.1.10.jar
*
*/
public class SVNUtil {
public static void main(String[] args) throws SVNException {
long checkoutResult= svnCheckout("https://192.168.0.1:8443/svn/svntest", "xxx", "123456", "D:/MySVNTest");
System.out.println(checkoutResult);
}
/**
* api调用svn下载,返回所下载项目版本的版本号
* @param SVNUrl svn地址
* @param userName svn用户名
* @param password svn密码
* @param savePath 下载后的项目存储路径
* @return
* @throws SVNException
*/
public static long svnCheckout(String SVNUrl,String userName,String password,String savePath) throws SVNException{
//版本号
long checkoutResult = 0;
SVNRepositoryFactoryImpl.setup();
SVNURL sVNURL = SVNURL
.parseURIEncoded(SVNUrl);
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager ourClientManager = SVNClientManager.newInstance(
(DefaultSVNOptions) options, userName, password);
File localFileDir = new File(savePath);
if (localFileDir.exists()) {
// System.err.println("copypath already exist");
} else {
localFileDir.mkdirs();
}
try {
SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
//返回的是所下载项目版本的版本号
checkoutResult = updateClient.doCheckout(sVNURL, localFileDir, SVNRevision.HEAD,SVNRevision.HEAD, SVNDepth.INFINITY, true);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return checkoutResult;
}