Thursday, October 31, 2013

Download a file from repository using SVNKit

SVNKit is an opensource java SVN library. You can perform all the SVN tasks from Java program using this library.
I had to download a specific file from the repo but SVNKit didn't support file checkout. I googled a lot but couldn't find any specific solution (code snippet :D ). So, I tried writing it myself.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;


public class subversion {
public static void main(String[] args) throws SVNException, IOException{
 File newfile=new File("/home/everest/pom.xml"); 
 if (!newfile.exists()) {
  newfile.createNewFile();
 }
 FileOutputStream fop=new FileOutputStream(newfile);
 SVNURL url=SVNURL.parseURIEncoded("http://svn.svnkit.com/repos/svnkit/branches/1.1.x/");
 SVNRepository repository = SVNRepositoryFactory.create(url);
 repository.getFile("pom.xml", -1, null, fop);
 fop.flush();
 fop.close();
 System.out.println("Done!");
   }
}

I wasted my time searching the code. If I've written it then why shall you waste your time for the same reason ?? :P