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

两盒软妹~` 提交于 2019-12-19 10:17: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 and trying to use example to read single file content from http://svnkit.com/kb/dev-guide-commit-operation.html but it is giving exception like error while fetching the file contents and properties: svn: E170001: Authentication required for 'https://netspurt.unfuddle.com:443 Unfuddle Subversion Repository'. Please anybody can explain it detail

bellow code worked for me:

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


    }

回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/16536149/how-to-get-all-files-and-directories-from-the-svn-repository-using-java

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