纯java操作SVN,使⽤svnkit做commit,update提交,更新操作public class SVNUtil {
private static Logger logger = Logger(SVNUtil.class);
/**
* 通过不同的协议初始化版本库
*/
public static void setupLibrary() {
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
}
/**
* 验证登录svn
*/
public static SVNClientManager authSvn(String svnRoot, String username,
String password) {
// 初始化版本库
setupLibrary();
// 创建库连接
SVNRepository repository = null;
try {
repository = ate(SVNURL
.parseURIEncoded(svnRoot));
} catch (SVNException e) {
<(e.getErrorMessage(), e);
return null;
}
// ⾝份验证
ISVNAuthenticationManager authManager = SVNWCUtil
.createDefaultAuthenticationManager(username, password);
// 创建⾝份验证管理器
repository.setAuthenticationManager(authManager);
DefaultSVNOptions options = ateDefaultOptions(true);
SVNClientManager clientManager = wInstance(options,
authManager);
return clientManager;
}
/**
* Make directory in svn repository
* @param clientManager
* @param url
* eg: svn.ambow/wlpt/bsp/trunk
* @param commitMessage
* @return
* @throws SVNException
*/
public static SVNCommitInfo makeDirectory(SVNClientManager clientManager,
SVNURL url, String commitMessage) {
try {
CommitClient().doMkDir(
new SVNURL[] { url }, commitMessage);
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
}
return null;
}
/**
* Imports an unversioned directory into a repository location denoted by a
* destination URL
* @param clientManager
* @param localPath
* a local unversioned directory or singal file that will be imported into a
* repository;
* @param dstURL
* a repository location where the local unversioned directory/file will be
* imported into
* @param commitMessage
* @param isRecursive 递归
* @return
*/
public static SVNCommitInfo importDirectory(SVNClientManager clientManager, File localPath, SVNURL dstURL, String commitMessage,
boolean isRecursive) {
try {
CommitClient().doImport(localPath, dstURL,
commitMessage, null, true, true,
SVNDepth.fromRecurse(isRecursive));
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
return null;
}
/**
* Puts directories and files under version control
* @param clientManager
* SVNClientManager
* @param wcPath
* work copy path
*/
public static void addEntry(SVNClientManager clientManager, File wcPath) {
try {
false, false, SVNDepth.fromRecurse(true), false, false,
true);
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
}
/**
* Collects status information on a single Working Copy item
* @param clientManager
* @param wcPath
* local item's path
* @param remote
* true to check up the status of the item in the repository,
* that will tell if the local item is out-of-date (like '-u' option in the SVN client's * 'svn status' command), otherwise false
* @return
* @throws SVNException
*/
public static SVNStatus showStatus(SVNClientManager clientManager,
File wcPath, boolean remote) {
SVNStatus status = null;
try {
status = StatusClient().doStatus(wcPath, remote);
} catch (SVNException e) {
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
return status;
}
/**
* Commit work copy's change to svn
* @param clientManager
* @param wcPath
* working copy paths which changes are to be committed
* @param keepLocks
* whether to unlock or not files in the repository
* @param commitMessage
* commit log message
* @return
* @throws SVNException
*/
public static SVNCommitInfo commit(SVNClientManager clientManager,
File wcPath, boolean keepLocks, String commitMessage) {
try {
CommitClient().doCommit(
new File[] { wcPath }, keepLocks, commitMessage, null,
null, false, false, SVNDepth.fromRecurse(true));
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
return null;
}
/**
* Updates a working copy (brings changes from the repository into the working copy). * @param clientManager
* @param wcPath
* working copy path
* @param updateToRevision
* revision to update to
* @param depth
* update的深度:⽬录、⼦⽬录、⽂件
* @return
* @throws SVNException
*/
public static long update(SVNClientManager clientManager, File wcPath,
SVNRevision updateToRevision, SVNDepth depth) {
SVNUpdateClient updateClient = UpdateClient();
/*
* sets externals not to be ignored during the update
*/
updateClient.setIgnoreExternals(false);
/*
* returns the number of the revision wcPath was updated to
*/
try {
return updateClient.doUpdate(wcPath, updateToRevision,depth, false, false);
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
return 0;
}
/**
* recursively checks out a working copy from url into wcDir
* @param clientManager
* @param url
* @param url
* a repository location from where a Working Copy will be checked out
* @param revision
* the desired revision of the Working Copy to be checked out
* @param destPath
* the local path where the Working Copy will be placed
* @param depth
* checkout的深度,⽬录、⼦⽬录、⽂件
* @return
* @throws SVNException
*/
public static long checkout(SVNClientManager clientManager, SVNURL url,
SVNRevision revision, File destPath, SVNDepth depth) {
SVNUpdateClient updateClient = UpdateClient();
/*
* sets externals not to be ignored during the checkout
*/
updateClient.setIgnoreExternals(false);
/*error parse new
* returns the number of the revision at which the working copy is
*/
try {
return updateClient.doCheckout(url, destPath, revision, revision,depth, false); } catch (SVNException e) {
<(e.getErrorMessage(), e);
}
return 0;
}
/**
* 确定path是否是⼀个⼯作空间
* @param path
* @return
*/
public static boolean isWorkingCopy(File path){
if(!ists()){
logger.warn("'" + path + "' not exist!");
return false;
}
try {
if(null == WorkingCopyRoot(path, false)){
return false;
}
} catch (SVNException e) {
<(e.getErrorMessage(), e);
}
return true;
}
}
public class SvnProjectService {
private Logger logger = Logger(SvnProjectService.class);
// 项⽬的存放位置
private String workspace = null;
private String templete = null;
private ResourceBundle rb = Bundle("application");
private String username = null;
private String password = null;
private void init(){
String webapp = ClassLoader().getResource("").toString().substring(6);
webapp = webapp.substring(0, webapp.String("project.webapp")) + 10);
username = rb.getString("svn.username");
password = rb.getString("svn.password");
workspace = rb.getString("project.svn.path");
templete = webapp + File.separator + rb.getString("plate");
}
public SvnProjectService(){
super();
init();
}
/**
* 从SVN更新项⽬到work copy
* @param project
* Project
* @return
*/
@Override
public boolean updateProjectFromSvn(Project project) {
if(null == project || null == SvnUrl()){
return false;
}
SVNClientManager clientManager = SVNUtil.SvnUrl(), username, password);
if (null == clientManager) {
<("SVN login error! >>> url:" + SvnUrl()
+ " username:" + username + " password:" + password);
return false;
}
// 注册⼀个更新事件处理器
SVNURL repositoryURL = null;
try {
// eg: svn.ambow/wlpt/bsp
repositoryURL = SVNURL.SvnUrl()).appendPath("trunk/"+Name(), false);
} catch (SVNException e) {
<(e.getMessage(),e);
return false;
}
File ws = new File(new File(workspace), Name());
if(!SVNWCUtil.isVersionedDirectory(ws)){
SVNUtil.checkout(clientManager, repositoryURL, SVNRevision.HEAD, new File(workspace), SVNDepth.INFINITY); }else{
SVNUtil.update(clientManager, ws, SVNRevision.HEAD, SVNDepth.INFINITY);
}
return true;
}
/**
* 提交项⽬到SVN
* @param project
* Project
* @return
*/
@Override
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论