[英]How to get all files and directories from the SVN repository using java
我有一个任务要完成。 我想连接到SVN信息库,并且必须使用Java代码将所有目录和文件从svn下载到我的本地系统。 我对此并不陌生,并尝试使用示例从http://svnkit.com/kb/dev-guide-commit-operation.html读取单个文件内容,但在获取文件内容和属性时却出现类似错误的异常: svn:E170001:“ https://netspurt.unfuddle.com:443 Unfuddle Subversion Repository”需要身份验证。 请任何人都可以详细解释
下面的代码为我工作:
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();}
}
“需要身份验证”问题意味着服务器需要身份验证,但是您没有向SVNClientManager
提供正确的ISVNAuthenticationManager
实现。 SVNKit支持不同的身份验证方式。
如果您知道凭据是什么,并且它们是固定的,则可以使用BasicAuthenticationManager
实现。
如果要使用存储在~/.subversion
目录中的凭据,请使用DefaultSVNAuthenticationManager
实现(有一种方便的构造方法: SVNWCUtil.createDefaultAuthenticationManager()
但请注意,这对应于带有--non-interactive
选项的Subversion命令)。 如果您需要允许从控制台输入密码的身份验证管理器,请查看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文件中的依赖项:
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.8.11</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.