Auslesen der SVN-Keywords mit Java
Für ein Projekt wollte ich alle Java-Sourcen aus einem Verzeichnisbaum prüfen ob deren SVN-Keywords gesetzt sind.
1. Lösung:
Ich schreibe ein Konsolen-Skript, das mittels dem SVN-Command den Verzeichnisbaum scannt und einen Report erzeugt.
Mit dem folgendem Consolen-Command kann man die SVN-Keywords einer einzelnen Datei abfragen:
svn propget svn:keywords <file-name>
Nachteil: Das Skript wäre nicht OS unabhängig —-> also kein Skript erstellen
2. Lösung:
Ich schreibe ein Java-Programm, das mittels der Open Source Bibliotheken SVNKit und Apache Commons-IO den Verzeichnisbaum scannt und einen Report erzeugt.
ValueSVNPropertyHandler.java :
import org.tmatesoft.svn.core.wc.ISVNPropertyHandler; public interface ValueSVNPropertyHandler extends ISVNPropertyHandler { public String getValue(); }
SVNProp.java :
import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.io.FileUtils; import org.tmatesoft.svn.core.SVNDepth; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNPropertyValue; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.util.SVNHashSet; import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions; import org.tmatesoft.svn.core.internal.wc.SVNPath; import org.tmatesoft.svn.core.wc.SVNPropertyData; import org.tmatesoft.svn.core.wc.SVNRevision; import org.tmatesoft.svn.core.wc.SVNWCClient; import org.tmatesoft.svn.core.wc.SVNWCUtil; public class SVNProp { public static void main(String[] args) throws SVNException { // example for call of getSVNKeywords String targetPath = " <PATH-TO-DIR>"; String svnKeywords = getSVNKeywords(targetPath); String x = "Keywords of %s = %s"; System.out.println(String.format(x, targetPath, svnKeywords)); // example for call of getFilesWithoutSvnKeywords File root = new File(" <PATH-TO-FILE>"); List allFilesWithoutSvnKeywords = getFilesWithoutSvnKeywords( root, new String[] { "java" }); System.out.println("List of svn-files with no svn:keywords"); System.out.println("--------------------------------------"); for (String fileWithNoSVNKeyword : allFilesWithoutSvnKeywords) { System.out.println(fileWithNoSVNKeyword); } String found = "found: %d"; System.out.println(String.format(found,allFilesWithoutSvnKeywords.size())); } @SuppressWarnings("unchecked") public static List getFilesWithoutSvnKeywords(File root, String[] extensions) { boolean recursive = true; // Finds files within a root directory and optionally its // subdirectories which match an array of extensions. Collection files = FileUtils.listFiles(root, extensions, recursive); List listOfFilesWithoutKeywords = new ArrayList(); for (File file : files) { String absolutePath = file.getAbsolutePath(); String keyw = null; try { keyw = SVNProp.getSVNKeywords(absolutePath); } catch (SVNException e) { continue; } if (keyw == null || "".equals(keyw)) { listOfFilesWithoutKeywords.add(absolutePath); } } return listOfFilesWithoutKeywords; } public static String getSVNKeywords(String targetPath) throws SVNException { // desired property-name String propertyName = "svn:keywords"; SVNDepth depth = SVNDepth.EMPTY; SVNHashSet changeLists = new SVNHashSet(); SVNRevision undefined = SVNRevision.UNDEFINED; DefaultSVNOptions options = new DefaultSVNOptions(); // default auth (local filesytem) ISVNAuthenticationManager defaultAuthenticationManager = SVNWCUtil .createDefaultAuthenticationManager(); SVNWCClient client = new SVNWCClient(defaultAuthenticationManager, options); // use given targetPath SVNPath target = new SVNPath(targetPath, true); SVNRevision pegRevision = target.getPegRevision(); // Handler for result-data ValueSVNPropertyHandler handler = new ValueSVNPropertyHandler() { private String value; public void handleProperty(long arg0, SVNPropertyData data) throws SVNException { this.value = getValueStr(data); } public void handleProperty(SVNURL url, SVNPropertyData data) throws SVNException { this.value = getValueStr(data); } public void handleProperty(File f, SVNPropertyData data) throws SVNException { this.value = getValueStr(data); } private String getValueStr(SVNPropertyData data) { if (data == null) return null; SVNPropertyValue svnValue = data.getValue(); if (svnValue == null) return null; return svnValue.toString(); } public String getValue() { return value; } }; // fetching data from subversion client.doGetProperty(target.getFile(), propertyName, pegRevision, undefined, depth, handler, changeLists); // value of svn:keywords return handler.getValue(); } }