SVNKit (JavaSVN) 是一個純 Java 的 SVN 用戶端庫,使用 SVNKit 無需安裝任何 SVN 的用戶端,支援各種作業系統。
這是一款商業軟體,非商業可免費使用。
下載svnkit.
重要步驟:
1、串連指定的svn庫
Java代碼
- String url = "http://svn.svnkit.com/repos/svnkit/trunk/doc";
- String name = "anonymous";
- String password = "anonymous";
-
- SVNRepository repository = null;
- try {
- repository = SVNRepositoryFactory.create( SVNURL.parseURIDecoded( url ) );
- ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( name , password );
- repository.setAuthenticationManager( authManager );
- } catch ( SVNException svne ) {
- //handle exception
- }
2、擷取指定路徑的目錄和檔案清單
Java代碼
- public static void listEntries( SVNRepository repository, String path ) throws SVNException {
- Collection entries = repository.getDir( path, -1 , null , (Collection) null );
- Iterator iterator = entries.iterator( );
- while ( iterator.hasNext( ) ) {
- SVNDirEntry entry = ( SVNDirEntry ) iterator.next( );
- System.out.println( "/" + (path.equals( "" ) ? "" : path + "/" ) + entry.getName( ) +
- " ( author: '" + entry.getAuthor( ) + "'; revision: " + entry.getRevision( ) +
- "; date: " + entry.getDate( ) + ")" );
- if ( entry.getKind() == SVNNodeKind.DIR ) {
- listEntries( repository, ( path.equals( "" ) ) ? entry.getName( ) : path + "/" + entry.getName( ) );
- }
- }
- }