如何使用java从SVN存储库获取所有文件和目录

我有一项任务要完成。 我想连接到SVN存储库,必须使用java代码将所有目录和文件从svn下载到我的本地系统。 我是新手,并尝试使用示例从http://svnkit.com/kb/dev-guide-commit-operation.html读取单个文件内容,但它在获取文件内容和属性时会出现exception错误: svn:E170001:“ https://netspurt.unfuddle.com:443 Unfuddle Subversion Repository”所需的身份validation。 请任何人都能解释一下细节

波纹管代码对我有用:

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

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

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

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

最后我想注意SVNClientManager是过时的一部分(虽然仍然支持)。 而不是像我的另一个答案那样喜欢SvnOperationFactory类,它也有setAuthenticationManager()setter。

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

解决方案基于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文件中的依赖:

   org.tmatesoft.svnkit svnkit 1.8.11