How to get all files and directories from the SVN repository using java

爱⌒轻易说出口 提交于 2019-12-01 11:00:47
Dmitry Pavlenko

"Authentication required" problem means that the server requires authentication but you didn't provide correct ISVNAuthenticationManager implementation to SVNClientManager. SVNKit supports different ways of authentication.

If you know what your credentials are, and they are fixed, you can use BasicAuthenticationManager implementation.

If you want to use credentials, stored in your ~/.subversion directory, use DefaultSVNAuthenticationManager implementation (there's a convenient method to construct it:SVNWCUtil.createDefaultAuthenticationManager() but note that this corresponds to Subversion commands with --non-interactive option). Look at SVNCommandEnvironment#createClientAuthenticationManager() implementation if you need authentication manager that would allow to enter password from console.

And finally I'd like to notice that SVNClientManager is a part of obsolete (though still supported). Instead prefer SvnOperationFactory class like in my another answer, it also has setAuthenticationManager() setter.

I'm one of SVNKit developers if this matters.

Solution based on Dmitry Pavlenko's answer:

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");
    }
}

And this is the dependency in maven POM file:

   <dependency>
          <groupId>org.tmatesoft.svnkit</groupId>
          <artifactId>svnkit</artifactId>
          <version>1.8.11</version>
   </dependency>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!