[英]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.