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

后端 未结 2 1671
执念已碎
执念已碎 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: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:

       
              org.tmatesoft.svnkit
              svnkit
              1.8.11
       
    

提交回复
热议问题