[英]How to get all files and directories from the SVN repository using java
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.
我想连接到SVN信息库,并且必须使用Java代码将所有目录和文件从svn下载到我的本地系统。 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'.
我对此并不陌生,并尝试使用示例从http://svnkit.com/kb/dev-guide-commit-operation.html读取单个文件内容,但在获取文件内容和属性时却出现类似错误的异常: svn:E170001:“ 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();}
}
"Authentication required" problem means that the server requires authentication but you didn't provide correct ISVNAuthenticationManager
implementation to SVNClientManager
. “需要身份验证”问题意味着服务器需要身份验证,但是您没有向
SVNClientManager
提供正确的ISVNAuthenticationManager
实现。 SVNKit supports different ways of authentication. SVNKit支持不同的身份验证方式。
If you know what your credentials are, and they are fixed, you can use BasicAuthenticationManager
implementation. 如果您知道凭据是什么,并且它们是固定的,则可以使用
BasicAuthenticationManager
实现。
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). 如果要使用存储在
~/.subversion
目录中的凭据,请使用DefaultSVNAuthenticationManager
实现(有一种方便的构造方法: SVNWCUtil.createDefaultAuthenticationManager()
但请注意,这对应于带有--non-interactive
选项的Subversion命令)。 Look at SVNCommandEnvironment#createClientAuthenticationManager() implementation if you need authentication manager that would allow to enter password from console. 如果您需要允许从控制台输入密码的身份验证管理器,请查看SVNCommandEnvironment#createClientAuthenticationManager()实现。
And finally I'd like to notice that SVNClientManager is a part of obsolete (though still supported). 最后,我想注意到SVNClientManager已过时(尽管仍受支持)。 Instead prefer SvnOperationFactory class like in my another answer , it also has setAuthenticationManager() setter.
相反,像在我的另一个答案中那样喜欢SvnOperationFactory类,它也具有setAuthenticationManager()setter。
I'm one of SVNKit developers if this matters. 如果这很重要,我就是SVNKit开发人员之一。
Solution based on Dmitry Pavlenko's answer: 基于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");
}
}
And this is the dependency in maven POM file: 这是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.