直接上代码,工作中使用的版本,记录下。

  1. public class SvnUtil {
  2.  
  3. private static Logger logger = Logger.getLogger(SvnUtil.class);
  4.  
  5. /**
  6. * 通过不同的协议初始化版本库
  7. */
  8. public static void setupLibrary() {
  9. DAVRepositoryFactory.setup();
  10. SVNRepositoryFactoryImpl.setup();
  11. FSRepositoryFactory.setup();
  12. }
  13.  
  14. /**
  15. * 验证登录svn
  16. */
  17. public static SVNClientManager authSvn(String svnRoot, String username,
  18. String password) {
  19. // 初始化版本库
  20. setupLibrary();
  21.  
  22. // 创建库连接
  23. SVNRepository repository = null;
  24. try {
  25. repository = SVNRepositoryFactory.create(SVNURL
  26. .parseURIEncoded(svnRoot));
  27. } catch (SVNException e) {
  28. logger.error(e.getErrorMessage(), e);
  29. return null;
  30. }
  31.  
  32. // 身份验证
  33. ISVNAuthenticationManager authManager = SVNWCUtil
  34.  
  35. .createDefaultAuthenticationManager(username, password);
  36.  
  37. // 创建身份验证管理器
  38. repository.setAuthenticationManager(authManager);
  39.  
  40. DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
  41. SVNClientManager clientManager = SVNClientManager.newInstance(options,
  42. authManager);
  43. return clientManager;
  44. }
  45.  
  46. /**
  47. * Make directory in svn repository
  48. * @param clientManager
  49. * @param url
  50. * eg: http://svn.ambow.com/wlpt/bsp/trunk
  51. * @param commitMessage
  52. * @return
  53. * @throws SVNException
  54. */
  55. public static SVNCommitInfo makeDirectory(SVNClientManager clientManager,
  56. SVNURL url, String commitMessage) {
  57. try {
  58. return clientManager.getCommitClient().doMkDir(
  59. new SVNURL[] { url }, commitMessage);
  60. } catch (SVNException e) {
  61. logger.error(e.getErrorMessage(), e);
  62. }
  63. return null;
  64. }
  65.  
  66. /**
  67. * Imports an unversioned directory into a repository location denoted by a
  68. * destination URL
  69. * @param clientManager
  70. * @param localPath
  71. * a local unversioned directory or singal file that will be imported into a
  72. * repository;
  73. * @param dstURL
  74. * a repository location where the local unversioned directory/file will be
  75. * imported into
  76. * @param commitMessage
  77. * @param isRecursive 递归
  78. * @return
  79. */
  80. public static SVNCommitInfo importDirectory(SVNClientManager clientManager,
  81. File localPath, SVNURL dstURL, String commitMessage,
  82. boolean isRecursive) {
  83. try {
  84. return clientManager.getCommitClient().doImport(localPath, dstURL,
  85. commitMessage, null, true, true,
  86. SVNDepth.fromRecurse(isRecursive));
  87. } catch (SVNException e) {
  88. logger.error(e.getErrorMessage(), e);
  89. }
  90. return null;
  91. }
  92.  
  93. /**
  94. * Puts directories and files under version control
  95. * @param clientManager
  96. * SVNClientManager
  97. * @param wcPath
  98. * work copy path
  99. */
  100. public static void addEntry(SVNClientManager clientManager, File wcPath) {
  101. try {
  102. clientManager.getWCClient().doAdd(new File[] { wcPath }, true,
  103. false, false, SVNDepth.INFINITY, false, false,
  104. true);
  105. } catch (SVNException e) {
  106. logger.error(e.getErrorMessage(), e);
  107. }
  108. }
  109.  
  110. /**
  111. * Collects status information on a single Working Copy item
  112. * @param clientManager
  113. * @param wcPath
  114. * local item's path
  115. * @param remote
  116. * true to check up the status of the item in the repository,
  117. * that will tell if the local item is out-of-date (like '-u' option in the SVN client's
  118. * 'svn status' command), otherwise false
  119. * @return
  120. * @throws SVNException
  121. */
  122. public static SVNStatus showStatus(SVNClientManager clientManager,
  123. File wcPath, boolean remote) {
  124. SVNStatus status = null;
  125. try {
  126. status = clientManager.getStatusClient().doStatus(wcPath, remote);
  127. } catch (SVNException e) {
  128. logger.error(e.getErrorMessage(), e);
  129. }
  130. return status;
  131. }
  132.  
  133. /**
  134. * Commit work copy's change to svn
  135. * @param clientManager
  136. * @param wcPath
  137. * working copy paths which changes are to be committed
  138. * @param keepLocks
  139. * whether to unlock or not files in the repository
  140. * @param commitMessage
  141. * commit log message
  142. * @return
  143. * @throws SVNException
  144. */
  145. public static SVNCommitInfo commit(SVNClientManager clientManager,
  146. File wcPath, boolean keepLocks, String commitMessage) {
  147. try {
  148. return clientManager.getCommitClient().doCommit(
  149. new File[] { wcPath }, keepLocks, commitMessage, null,
  150. null, false, false, SVNDepth.INFINITY);
  151. } catch (SVNException e) {
  152. logger.error(e.getErrorMessage(), e);
  153. }
  154. return null;
  155. }
  156.  
  157. /**
  158. * Updates a working copy (brings changes from the repository into the working copy).
  159. * @param clientManager
  160. * @param wcPath
  161. * working copy path
  162. * @param updateToRevision
  163. * revision to update to
  164. * @param depth
  165. * update的深度:目录、子目录、文件
  166. * @return
  167. * @throws SVNException
  168. */
  169. public static long update(SVNClientManager clientManager, File wcPath,
  170. SVNRevision updateToRevision, SVNDepth depth) {
  171. SVNUpdateClient updateClient = clientManager.getUpdateClient();
  172.  
  173. /*
  174. * sets externals not to be ignored during the update
  175. */
  176. updateClient.setIgnoreExternals(false);
  177.  
  178. /*
  179. * returns the number of the revision wcPath was updated to
  180. */
  181. try {
  182. return updateClient.doUpdate(wcPath, updateToRevision,depth, false, false);
  183. } catch (SVNException e) {
  184. logger.error(e.getErrorMessage(), e);
  185. }
  186. return 0;
  187. }
  188.  
  189. /**
  190. * recursively checks out a working copy from url into wcDir
  191. * @param clientManager
  192. * @param url
  193. * a repository location from where a Working Copy will be checked out
  194. * @param revision
  195. * the desired revision of the Working Copy to be checked out
  196. * @param destPath
  197. * the local path where the Working Copy will be placed
  198. * @param depth
  199. * checkout的深度,目录、子目录、文件
  200. * @return
  201. * @throws SVNException
  202. */
  203. public static long checkout(SVNClientManager clientManager, SVNURL url,
  204. SVNRevision revision, File destPath, SVNDepth depth) {
  205.  
  206. SVNUpdateClient updateClient = clientManager.getUpdateClient();
  207. /*
  208. * sets externals not to be ignored during the checkout
  209. */
  210. updateClient.setIgnoreExternals(false);
  211. /*
  212. * returns the number of the revision at which the working copy is
  213. */
  214. try {
  215. return updateClient.doCheckout(url, destPath, revision, revision,depth, false);
  216. } catch (SVNException e) {
  217. logger.error(e.getErrorMessage(), e);
  218. }
  219. return 0;
  220. }
  221.  
  222. /**
  223. * 确定path是否是一个工作空间
  224. * @param path
  225. * @return
  226. */
  227. public static boolean isWorkingCopy(File path){
  228. if(!path.exists()){
  229. logger.warn("'" + path + "' not exist!");
  230. return false;
  231. }
  232. try {
  233. if(null == SVNWCUtil.getWorkingCopyRoot(path, false)){
  234. return false;
  235. }
  236. } catch (SVNException e) {
  237. logger.error(e.getErrorMessage(), e);
  238. }
  239. return true;
  240. }
  241.  
  242. /**
  243. * 确定一个URL在SVN上是否存在
  244. * @param url
  245. * @return
  246. */
  247. public static boolean isURLExist(SVNURL url,String username,String password){
  248. try {
  249. SVNRepository svnRepository = SVNRepositoryFactory.create(url);
  250. ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
  251. svnRepository.setAuthenticationManager(authManager);
  252. SVNNodeKind nodeKind = svnRepository.checkPath("", -1);
  253. return nodeKind == SVNNodeKind.NONE ? false : true;
  254. } catch (SVNException e) {
  255. e.printStackTrace();
  256. }
  257. return false;
  258. }
  259.  
  260. public static SVNRepository getRepository(String url, String username, String password) {
  261. DAVRepositoryFactory.setup();
  262. SVNRepositoryFactoryImpl.setup();
  263. SVNRepository repository = null;
  264. SVNNodeKind nodeKind = null;
  265. try {
  266. repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
  267. ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
  268. repository.setAuthenticationManager(authManager);
  269. nodeKind = repository.checkPath("", -1);
  270. } catch (Exception e) {
  271. throw new RuntimeException(e);
  272. }
  273. if (nodeKind == SVNNodeKind.NONE) {
  274. throw new RuntimeException("There is no entry at '" + url + "'.");
  275. } else if (nodeKind == SVNNodeKind.FILE) {
  276. throw new RuntimeException("The entry at '" + url + "' is a file while a directory was expected.");
  277. }
  278. return repository;
  279. }
  280.  
  281. }

  

使用示例:

  1. public String Checkout(Model model) throws Exception {
  2. //初始化支持svn://协议的库。 必须先执行此操作。
  3.  
  4. SVNRepositoryFactoryImpl.setup();
  5.  
  6. //相关变量赋值
  7. SVNURL repositoryURL = null;
  8. try {
  9. repositoryURL = SVNURL.parseURIEncoded(svnurl);
  10. } catch (Exception e) {
  11. //
  12. }
  13. String name = "test";//用户名
  14. String password = "123456";//密码
  15. ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
  16.  
  17. //实例化客户端管理类
  18. ourClientManager = SVNClientManager.newInstance(
  19. (DefaultSVNOptions) options, name, password);
  20.  
  21. //要把版本库的内容check out到的目录
  22. File wcDir = new File(localurl);
  23.  
  24. long workingVersion = -1;
  25. try {
  26. workingVersion = SvnUtil.checkout(ourClientManager, repositoryURL, SVNRevision.HEAD, wcDir, SVNDepth.INFINITY);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. model.addAttribute("msg", "把版本:" + workingVersion + " check out 到目录:" + wcDir + "中。");
  31. System.out.println("把版本:" + workingVersion + " check out 到目录:" + wcDir + "中。");
  32. return "msg";
  33. }

  Maven:

  1. <dependency>
  2. <groupId>log4j</groupId>
  3. <artifactId>log4j</artifactId>
  4. <version>1.2.7</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.tmatesoft.svnkit</groupId>
  8. <artifactId>svnkit</artifactId>
  9. <version>1.8.2</version>
  10. </dependency>

  

java操作svn工具类SvnUtil的更多相关文章

  1. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  2. Java操作Excel工具类(poi)

    分享一个自己做的poi工具类,写不是很完全,足够我自己当前使用,有兴趣的可以自行扩展 1 import org.apache.commons.lang3.exception.ExceptionUtil ...

  3. java操作excel 工具类

    java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...

  4. Java操作FTP工具类(实例详解)

    这里使用Apache的FTP jar 包 没有使用Java自带的FTPjar包  工具类 package com.zit.ftp; import java.io.File; import java.i ...

  5. java操作mongodb工具类

    新建maven项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  6. JAVA 操作Excel工具类

    Bean转Excel对象 /* * 文件名:BeanToExcel.java */ import java.util.ArrayList; import java.util.List; import ...

  7. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  8. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  9. java中文件操作的工具类

    代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...

随机推荐

  1. CodeForces - 468A

    Little X used to play a card game called "24 Game", but recently he has found it too easy. ...

  2. BottomNavigationBarItem fixed

    BottomNavigationBar( type: BottomNavigationBarType.fixed, onTap: (value){ if more then 3 items,, use ...

  3. Java线程池ThreadPoolExecutor原理和用法

    1.ThreadPoolExecutor构造方法 public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAli ...

  4. java如何编写多线程

    1.如何实现多线程 1.1实现Runnable接口,实现run()方法. public class Main4 implements Runnable { public static void mai ...

  5. 关于html中的 script标签中的 代码写法有效性? easyui tabs的href不能载入内容页面

    script标签, 即 html中的 js脚本区域中: 它其实就是一个 普通的 html标签, 在 html 渲染器 parser 看来, 它跟其他任何的普通 的 html标签 , 比如 p 标签, ...

  6. 利用Java手写简单的httpserver

    前言: 在看完尚学堂JAVA300中讲解如何实现一个最简单的httpserver部分的视频之后, 一.前置知识 1.HTTP协议 当前互联网网页访问主要采用了B/S的模式,既一个浏览器,一个服务器,浏 ...

  7. BalkanOI 2018 Parentrises(贪心+基础DP)

    题意 https://loj.ac/problem/2713 思路 对于 \(\text{P1}\) 的档,首先可以看出 \(O(n^3)\) 的方法,即用 \(O(n^3)\) 的 \(\text{ ...

  8. CAS工程用redis集群存储票据ticket Spring整合

    maven jar包版本: <dependency> <groupId>redis.clients</groupId> <artifactId>jedi ...

  9. Latex 仅使用 hyperref 包中 \href 的方法

    参考: How to ask hyperref works only with href Latex 仅使用 hyperref 包中 \href 的方法 在 .tex 文件的开头使用如下方法引用 hy ...

  10. ios中关键词weak,assign,copy.strong等的区别

    虽然开发IOS好多年了.但是这几个关键词总是深深困扰着我.加上IOS开发从mRC到ARC的过渡,这些概念更为困扰我了. 先说weak与assign.weak只能修饰对象,不能修饰基本数据类型.而ass ...