From 249dfafc4258c669cdf11d8697eaa232807e4b38 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 6 Jul 2012 20:21:52 -0500 Subject: [PATCH] MiniMaven: fake execution of the buildnumber-maven-plugin MiniMaven will never execute Maven plugins. But we want the functionality of the buildnumber-maven-plugin, which records the current revision into the manifest under the key 'Implementation-Build'. Since we will only ever support Git in Fiji, we can cheat and only handle Git in our "buildnumber plugin". Signed-off-by: Johannes Schindelin --- .../src/main/java/fiji/build/MiniMaven.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src-plugins/fake/src/main/java/fiji/build/MiniMaven.java b/src-plugins/fake/src/main/java/fiji/build/MiniMaven.java index 5a9c11f209..b6bd95a157 100755 --- a/src-plugins/fake/src/main/java/fiji/build/MiniMaven.java +++ b/src-plugins/fake/src/main/java/fiji/build/MiniMaven.java @@ -126,8 +126,10 @@ public POM parse(File file, POM parent, String classifier) throws IOException, P File directory = file.getCanonicalFile().getParentFile(); POM pom = new POM(directory, parent); pom.coordinate.classifier = classifier; - if (parent != null) + if (parent != null) { pom.sourceDirectory = parent.sourceDirectory; + pom.includeImplementationBuild = parent.includeImplementationBuild; + } XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); reader.setContentHandler(pom); //reader.setXMLErrorHandler(...); @@ -182,6 +184,8 @@ public POM parse(File file, POM parent, String classifier) throws IOException, P if (pom.parent != null) { if (pom.parent.parent == pom) pom.parent.parent = null; + if (pom.parent.includeImplementationBuild) + pom.includeImplementationBuild = true; pom.parent.addChild(pom); } } @@ -316,6 +320,7 @@ public class POM extends DefaultHandler implements Comparable { protected List dependencies = new ArrayList(); protected Set repositories = new TreeSet(); protected String sourceVersion, targetVersion, mainClass; + protected boolean includeImplementationBuild; protected String packaging = "jar"; // only used during parsing @@ -343,6 +348,7 @@ protected POM(File directory, POM parent) { coordinate.groupId = parent.coordinate.groupId; coordinate.version = parent.coordinate.version; parentCoordinate = parent.coordinate; + includeImplementationBuild = parent.includeImplementationBuild; } } @@ -509,10 +515,14 @@ public void build(boolean makeJar) throws FakeException, IOException, ParserConf if (makeJar) { JarOutputStream out; - if (mainClass == null) + if (mainClass == null && !includeImplementationBuild) out = new JarOutputStream(new FileOutputStream(getTarget())); else { - String text = "Manifest-Version: 1.0\nMain-Class: " + mainClass + "\n"; + String text = "Manifest-Version: 1.0\n"; + if (mainClass != null) + text += "Main-Class: " + mainClass + "\n"; + if (includeImplementationBuild) + text += "Implementation-Build: " + getImplementationBuild(directory) + "\n"; InputStream input = new ByteArrayInputStream(text.getBytes()); Manifest manifest = null; try { @@ -1087,8 +1097,11 @@ else if (prefix.equals(">project>parent>version")) { checkParentTag("version", parentCoordinate.version, string); } } - else if (prefix.equals(">project>build>plugins>plugin>artifactId")) + else if (prefix.equals(">project>build>plugins>plugin>artifactId")) { currentPluginName = string; + if (string.equals("buildnumber-maven-plugin")) + includeImplementationBuild = true; + } else if (prefix.equals(">project>build>plugins>plugin>configuration>source") && "maven-compiler-plugin".equals(currentPluginName)) sourceVersion = string; else if (prefix.equals(">project>build>plugins>plugin>configuration>target") && "maven-compiler-plugin".equals(currentPluginName)) @@ -1471,6 +1484,23 @@ protected static void ensureIJDirIsSet() { System.setProperty("ij.dir", ijDir); } + protected String getImplementationBuild(File file) { + if (!file.isAbsolute()) try { + file = file.getCanonicalFile(); + } + catch (IOException e) { + file = file.getAbsoluteFile(); + } + for (;;) { + File gitDir = new File(file, ".git"); + if (gitDir.exists()) + return exec(gitDir, "git", "rev-parse", "HEAD"); + file = file.getParentFile(); + if (file == null) + return null; + } + } + protected String exec(File gitDir, String... args) { try { Process process = Runtime.getRuntime().exec(args, null, gitDir);