歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 的svn客戶端調用示例

Java 的svn客戶端調用示例

日期:2017/3/1 10:20:55   编辑:Linux編程

1.pom依賴

  1. <dependency>
  2. <groupId>org.tmatesoft.svnkit</groupId>
  3. <artifactId>svnkit</artifactId>
  4. <version>1.3.5</version>
  5. </dependency>
2.java調用代碼
  1. import org.tmatesoft.svn.core.SVNDepth;
  2. import org.tmatesoft.svn.core.SVNException;
  3. import org.tmatesoft.svn.core.SVNURL;
  4. import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
  5. import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
  6. import org.tmatesoft.svn.core.wc.SVNClientManager;
  7. import org.tmatesoft.svn.core.wc.SVNLogClient;
  8. import org.tmatesoft.svn.core.wc.SVNRevision;
  9. import org.tmatesoft.svn.core.wc.SVNUpdateClient;
  10. public class SvnTest {
  11. static {
  12. DAVRepositoryFactory.setup();
  13. }
  14. private SVNClientManager manager;
  15. private SVNURL repositoryBaseUrl;
  16. public SvnTest() {
  17. DefaultSVNOptions options = new DefaultSVNOptions();
  18. manager = SVNClientManager.newInstance(options);
  19. // manager = SVNClientManager.newInstance(options,
  20. // "username","passwrod"); //如果需要用戶名密碼
  21. try {
  22. repositoryBaseUrl = SVNURL
  23. .parseURIDecoded("http://svn.apache.org/repos/asf/logging/log4j/trunk/src/main/java/org/apache/log4j/or"); // 傳入svn地址
  24. } catch (SVNException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }
  28. }
  29. public void test() throws SVNException {
  30. SVNLogClient logClient = manager.getLogClient();
  31. // svn list
  32. DirEntryHandler handler = new DirEntryHandler(); // 在svn
  33. // co時對每個文件目錄的處理,實現ISVNDirEntryHandler接口
  34. logClient.doList(repositoryBaseUrl, SVNRevision.HEAD, SVNRevision.HEAD,
  35. false, true, handler); // 列出當前svn地址的目錄,對每個文件進行處理
  36. // svn co
  37. UpdateEventHandler svnEventHandler = new UpdateEventHandler(); // svn co時對每個文件的處理
  38. SVNUpdateClient client = manager.getUpdateClient();
  39. client.setIgnoreExternals(true);
  40. client.setEventHandler(svnEventHandler);
  41. File to = new File("e:\\log\\testsvn"); // co出來的文件存放目錄
  42. client.doCheckout(repositoryBaseUrl, to, SVNRevision.HEAD,
  43. SVNRevision.HEAD, SVNDepth.INFINITY, false);
  44. // svn update
  45. client.setIgnoreExternals(true);
  46. client.setEventHandler(svnEventHandler);
  47. client.doUpdate(to, SVNRevision.HEAD, SVNDepth.INFINITY,true, false);
  48. }
  49. public static void main(String[] args) throws SVNException {
  50. SvnTest svntest = new SvnTest();
  51. svntest.test();
  52. }
  53. }
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.tmatesoft.svn.core.ISVNDirEntryHandler;
  4. import org.tmatesoft.svn.core.SVNDirEntry;
  5. import org.tmatesoft.svn.core.SVNException;
  6. import org.tmatesoft.svn.core.SVNNodeKind;
  7. import com.alibaba.tools.code.SearchConfig;
  8. public class DirEntryHandler implements ISVNDirEntryHandler {
  9. @Override
  10. public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {
  11. System.out.println(dirEntry.getRelativePath() + "/" + dirEntry.getName());
  12. }
  13. }
  1. import java.io.IOException;
  2. import org.apache.commons.io.FileUtils;
  3. import org.tmatesoft.svn.core.SVNCancelException;
  4. import org.tmatesoft.svn.core.SVNNodeKind;
  5. import org.tmatesoft.svn.core.wc.ISVNEventHandler;
  6. import org.tmatesoft.svn.core.wc.SVNEvent;
  7. import org.tmatesoft.svn.core.wc.SVNEventAction;
  8. public class UpdateEventHandler implements ISVNEventHandler {
  9. public void handleEvent(SVNEvent event, double progress) {
  10. SVNEventAction action = event.getAction();
  11. SVNNodeKind nodeKind = event.getNodeKind();
  12. if (SVNNodeKind.DIR.equals(nodeKind)) {
  13. // folder
  14. System.out.println(event.getFile().getName());
  15. } else {
  16. // treat as file for all other type
  17. if (action == SVNEventAction.UPDATE_DELETE) {
  18. try {
  19. System.out.println(event.getFile().getName() + "\t" + FileUtils.readFileToString(event.getFile()));
  20. } catch (IOException e) {
  21. }
  22. } else if (action == SVNEventAction.UPDATE_ADD || action == SVNEventAction.UPDATE_UPDATE) {
  23. try {
  24. System.out.println(event.getFile().getName() + "\t" + FileUtils.readFileToString(event.getFile()));
  25. } catch (IOException e) {
  26. }
  27. }
  28. }
  29. }
  30. public void checkCancelled() throws SVNCancelException {
  31. }
  32. }

輸出:

  1. /or
  2. jms/jms
  3. jms/MessageRenderer.java/MessageRenderer.java
  4. jms/package.html/package.html
  5. sax/sax
  6. sax/AttributesRenderer.java/AttributesRenderer.java
  7. sax/package.html/package.html
  8. DefaultRenderer.java/DefaultRenderer.java
  9. ObjectRenderer.java/ObjectRenderer.java
  10. RendererMap.java/RendererMap.java
  11. ThreadGroupRenderer.java/ThreadGroupRenderer.java
  12. package.html/package.html
  13. testsvn
  14. testsvn

Copyright © Linux教程網 All Rights Reserved