【问题标题】:How to get all files and directories from the SVN repository using java如何使用 java 从 SVN 存储库中获取所有文件和目录
【发布时间】:2013-05-14 06:02:14
【问题描述】:

我有一个任务要完成。我想连接到 SVN 存储库,并且必须使用 java 代码将所有目录和文件从 svn 下载到我的本地系统。我对此并不陌生,并试图使用示例从http://svnkit.com/kb/dev-guide-commit-operation.html 读取单个文件内容,但在获取文件内容和属性时它给出了类似错误的异常:svn: E170001: 'https://netspurt.unfuddle.com:443 Unfuddle Subversion Repository' required Authentication。谁能详细解释一下

下面的代码对我有用:

private static SVNClientManager ourClientManager;
     public void DownloadWorkingCopy(String svnLocation,String svnUserName,String svnPassword){

String locationForSVNProj="C:\\SVNLOC";

        DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
        ourClientManager = SVNClientManager.newInstance(options, svnUserName, svnPassword);
        SVNUpdateClient updateClient = ourClientManager.getUpdateClient( );
        updateClient.setIgnoreExternals( false );
        SVNRevision rev=SVNRevision.HEAD;
        File file=new File(locationForSVNProj);
        try{
             long revision1=updateClient.doCheckout( SVNURL.parseURIEncoded(svnLocation) ,file , rev , rev , true);

        }catch(Exception e){e.printStackTrace();}


    }

【问题讨论】:

  • 请提供代码,当您不共享代码时,您希望人们如何帮助您编写代码。
  • 您能否向我们展示您至少设置服务器身份验证的代码?
  • 对不起,我忘了在这里更新这很容易。我更新了上面的工作代码。感谢您的帮助

标签: java svnkit


【解决方案1】:

“需要身份验证”问题意味着服务器需要身份验证,但您没有向SVNClientManager 提供正确的ISVNAuthenticationManager 实现。 SVNKit 支持不同的身份验证方式。

如果您知道您的凭据是什么,并且它们是固定的,您可以使用BasicAuthenticationManager 实现。

如果您想使用存储在~/.subversion 目录中的凭据,请使用DefaultSVNAuthenticationManager 实现(有一种方便的方法来构造它:SVNWCUtil.createDefaultAuthenticationManager() 但请注意,这对应于带有--non-interactive 选项的Subversion 命令)。如果您需要允许从控制台输入密码的身份验证管理器,请查看 SVNCommandEnvironment#createClientAuthenticationManager() 实现。

最后我想注意到 SVNClientManager 是过时的一部分(尽管仍然受支持)。相反,我更喜欢 SvnOperationFactory 类,就像在我的另一个 answer 中一样,它也有 setAuthenticationManager() 设置器。

如果这很重要,我是 SVNKit 开发人员之一。

【讨论】:

【解决方案2】:

基于 Dmitry Pavlenko 回答的解决方案:

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.File;

private static void obtenerCodigoFuenteSVN() {        
    final String url = "http://IPSVN:PORT/svn/PROJECT";
    final String destPath = "PATH_DIR_DOWNLOAD";

    final String user = "XXXX";
    final String password = "XXXX";

    SVNRepository repository = null;

    try {
        //initiate the reporitory from the url
        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        //create authentication data
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(user, password.toCharArray());
        repository.setAuthenticationManager(authManager);
        //output some data to verify connection
        System.out.println("Repository Root: " + repository.getRepositoryRoot(true));
        System.out.println("Repository UUID: " + repository.getRepositoryUUID(true));
        //need to identify latest revision
        long latestRevision = repository.getLatestRevision();
        System.out.println("Repository Latest Revision: " + latestRevision);

        //create client manager and set authentication
        SVNClientManager ourClientManager = SVNClientManager.newInstance();
        ourClientManager.setAuthenticationManager(authManager);
        //use SVNUpdateClient to do the export
        SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
        updateClient.setIgnoreExternals(false);
        updateClient.doExport(repository.getLocation(), new File(destPath),
                SVNRevision.create(latestRevision), SVNRevision.create(latestRevision),
                null, true, SVNDepth.INFINITY);

    } catch (SVNException e) {
        System.out.println("ERROR TO ACCESS REPOSITORY");
        e.printStackTrace();
    } finally {
        System.out.println("Done");
    }
}

这是maven POM文件中的依赖:

   <dependency>
          <groupId>org.tmatesoft.svnkit</groupId>
          <artifactId>svnkit</artifactId>
          <version>1.8.11</version>
   </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2011-04-07
    相关资源
    最近更新 更多