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

后端 未结 2 1669
执念已碎
执念已碎 2021-01-14 21:07

I have a task to complete. I Want to connect to SVN repository and have to download all directories and files from svn to my local system using java code. I am new to this a

相关标签:
2条回答
  • 2021-01-14 21:29

    "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.

    0 讨论(0)
  • 2021-01-14 21:37

    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>
    
    0 讨论(0)
提交回复
热议问题