SVNKit 应用

Java 的svn客户端调用示例

1.pom依赖

<dependency>  

    <groupId>org.tmatesoft.svnkit</groupId>  

    <artifactId>svnkit</artifactId>  

    <version>1.3.5</version>  

</dependency>  

2.java调用代码

import org.tmatesoft.svn.core.SVNDepth;  

import org.tmatesoft.svn.core.SVNException;  

import org.tmatesoft.svn.core.SVNURL;  

import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;  

import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;  

import org.tmatesoft.svn.core.wc.SVNClientManager;  

import org.tmatesoft.svn.core.wc.SVNLogClient;  

import org.tmatesoft.svn.core.wc.SVNRevision;  

import org.tmatesoft.svn.core.wc.SVNUpdateClient;  

  

public class SvnTest {  

  

    static {  

        DAVRepositoryFactory.setup();  

    }  

  

    private SVNClientManager manager;  

    private SVNURL repositoryBaseUrl;  

  

    public SvnTest() {  

        DefaultSVNOptions options = new DefaultSVNOptions();  

        manager = SVNClientManager.newInstance(options);  

        // manager = SVNClientManager.newInstance(options,   

        // "username","passwrod"); //如果需要用户名密码   

        try {  

            repositoryBaseUrl = SVNURL  

                    .parseURIDecoded("http://svn.apache.org/repos/asf/logging/log4j/trunk/src/main/java/org/apache/log4j/or"); // 传入svn地址  

        } catch (SVNException e) {  

            // TODO Auto-generated catch block   

            e.printStackTrace();  

        }  

  

    }  

  

    public void test() throws SVNException {  

        SVNLogClient logClient = manager.getLogClient();  

  

        // svn list   

        DirEntryHandler handler = new DirEntryHandler(); // 在svn   

                                                            // co时对每个文件目录的处理,实现ISVNDirEntryHandler接口   

        logClient.doList(repositoryBaseUrl, SVNRevision.HEAD, SVNRevision.HEAD,  

                false, true, handler); // 列出当前svn地址的目录,对每个文件进行处理   

  

        // svn co   

        UpdateEventHandler svnEventHandler = new UpdateEventHandler(); // svn  co时对每个文件的处理   

        SVNUpdateClient client = manager.getUpdateClient();  

        client.setIgnoreExternals(true);  

        client.setEventHandler(svnEventHandler);  

        File to = new File("e:\\log\\testsvn"); // co出來的文件存放目錄   

        client.doCheckout(repositoryBaseUrl, to, SVNRevision.HEAD,  

                SVNRevision.HEAD, SVNDepth.INFINITY, false);  

  

        // svn update   

        client.setIgnoreExternals(true);  

        client.setEventHandler(svnEventHandler);   

  

        client.doUpdate(to, SVNRevision.HEAD, SVNDepth.INFINITY,true, false);  

  

    }  

  

    public static void main(String[] args) throws SVNException {  

        SvnTest svntest = new SvnTest();  

        svntest.test();  

    }  

}  

 

import java.util.ArrayList;  

import java.util.List;  

  

import org.tmatesoft.svn.core.ISVNDirEntryHandler;  

import org.tmatesoft.svn.core.SVNDirEntry;  

import org.tmatesoft.svn.core.SVNException;  

import org.tmatesoft.svn.core.SVNNodeKind;  

  

import com.alibaba.tools.code.SearchConfig;  

  

public class DirEntryHandler implements ISVNDirEntryHandler {  

  

    @Override  

    public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {  

  

        System.out.println(dirEntry.getRelativePath() + "/" + dirEntry.getName());  

    }  

  

}  

 

import java.io.IOException;  

  

import org.apache.commons.io.FileUtils;  

import org.tmatesoft.svn.core.SVNCancelException;  

import org.tmatesoft.svn.core.SVNNodeKind;  

import org.tmatesoft.svn.core.wc.ISVNEventHandler;  

import org.tmatesoft.svn.core.wc.SVNEvent;  

import org.tmatesoft.svn.core.wc.SVNEventAction;  

  

public class UpdateEventHandler implements ISVNEventHandler {  

  

    public void handleEvent(SVNEvent event, double progress) {  

        SVNEventAction action = event.getAction();  

        SVNNodeKind nodeKind = event.getNodeKind();  

  

        if (SVNNodeKind.DIR.equals(nodeKind)) {  

            // folder   

                System.out.println(event.getFile().getName());  

  

        } else {  

            // treat as file for all other type   

            if (action == SVNEventAction.UPDATE_DELETE) {  

                try {  

                    System.out.println(event.getFile().getName() + "\t" + FileUtils.readFileToString(event.getFile()));  

                } catch (IOException e) {  

  

                }  

            } else if (action == SVNEventAction.UPDATE_ADD || action == SVNEventAction.UPDATE_UPDATE) {  

                try {  

                    System.out.println(event.getFile().getName() + "\t" + FileUtils.readFileToString(event.getFile()));  

                } catch (IOException e) {  

  

                }  

            }  

        }  

    }  

  

    public void checkCancelled() throws SVNCancelException {  

    }  

  

}  

输出:

/or  

jms/jms  

jms/MessageRenderer.java/MessageRenderer.java  

jms/package.html/package.html  

sax/sax  

sax/AttributesRenderer.java/AttributesRenderer.java  

sax/package.html/package.html  

DefaultRenderer.java/DefaultRenderer.java  

ObjectRenderer.java/ObjectRenderer.java  

RendererMap.java/RendererMap.java  

ThreadGroupRenderer.java/ThreadGroupRenderer.java  

package.html/package.html  

testsvn  

testsvn  

出处:http://www.linuxidc.com/Linux/2012-06/61781.htm

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值