import org.tmatesoft.svn.core.SVNErrorCode;
import org.tmatesoft.svn.core.SVNErrorMessage;
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.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class SvnUtil {
private static String STRING_EMPTY = "";
private static int NEGATIVE_ONE = -1;
/**
* 测试SVN地址是否正确
*
* @param svnUrl
* SVN地址
* @param svnUser
* SVN用户名
* @param strPassword
* SVN密码
* @throws SVNException
* SVNException
*/
public static boolean testConnection(String svnUrl, String svnUser,
String strPassword) throws SVNException {
DAVRepositoryFactory.setup(); // 对于通过使用 http:// 和 https:// 访问
SVNRepository repository = SVNRepositoryFactory.create(SVNURL
.parseURIEncoded(svnUrl));
ISVNAuthenticationManager auth = SVNWCUtil
.createDefaultAuthenticationManager(svnUser, strPassword);
repository.setAuthenticationManager(auth);
SVNNodeKind nodeKind = repository.checkPath(STRING_EMPTY, NEGATIVE_ONE);
if (nodeKind == SVNNodeKind.NONE) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.UNKNOWN,
"No project at URL");
throw new SVNException(err);
}
return true;
}
/**
* 测试SVN连接是否正常
*/
private static boolean testSvnConnection(String svnUrl, String svnUser,
String strPassword) {
try {
testConnection(svnUrl, svnUser, strPassword);
} catch (SVNException expection) {
if (expection.getMessage().startsWith(
"svn: Authentication required for")) {
System.out.println("用户名密码错误");
return false;
} else {
System.out.println("连接失败!");
return false;
}
}
System.out.println("连接成功!");
return true;
}
}
需要jar包:
svnkit-cli.jar
svnkit-javahl.jar
svnkit.jar
import org.tmatesoft.svn.core.SVNErrorMessage;
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.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class SvnUtil {
private static String STRING_EMPTY = "";
private static int NEGATIVE_ONE = -1;
/**
* 测试SVN地址是否正确
*
* @param svnUrl
* SVN地址
* @param svnUser
* SVN用户名
* @param strPassword
* SVN密码
* @throws SVNException
* SVNException
*/
public static boolean testConnection(String svnUrl, String svnUser,
String strPassword) throws SVNException {
DAVRepositoryFactory.setup(); // 对于通过使用 http:// 和 https:// 访问
SVNRepository repository = SVNRepositoryFactory.create(SVNURL
.parseURIEncoded(svnUrl));
ISVNAuthenticationManager auth = SVNWCUtil
.createDefaultAuthenticationManager(svnUser, strPassword);
repository.setAuthenticationManager(auth);
SVNNodeKind nodeKind = repository.checkPath(STRING_EMPTY, NEGATIVE_ONE);
if (nodeKind == SVNNodeKind.NONE) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.UNKNOWN,
"No project at URL");
throw new SVNException(err);
}
return true;
}
/**
* 测试SVN连接是否正常
*/
private static boolean testSvnConnection(String svnUrl, String svnUser,
String strPassword) {
try {
testConnection(svnUrl, svnUser, strPassword);
} catch (SVNException expection) {
if (expection.getMessage().startsWith(
"svn: Authentication required for")) {
System.out.println("用户名密码错误");
return false;
} else {
System.out.println("连接失败!");
return false;
}
}
System.out.println("连接成功!");
return true;
}
}
需要jar包:
svnkit-cli.jar
svnkit-javahl.jar
svnkit.jar