Java 的svn客户端调用示例


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

输出:

  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  

 

相关内容