Skip to content

Commit

Permalink
MiniMaven: fake execution of the buildnumber-maven-plugin
Browse files Browse the repository at this point in the history
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 <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Jul 7, 2012
1 parent c4c21da commit 249dfaf
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src-plugins/fake/src/main/java/fiji/build/MiniMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(...);
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -316,6 +320,7 @@ public class POM extends DefaultHandler implements Comparable<POM> {
protected List<Coordinate> dependencies = new ArrayList<Coordinate>();
protected Set<String> repositories = new TreeSet<String>();
protected String sourceVersion, targetVersion, mainClass;
protected boolean includeImplementationBuild;
protected String packaging = "jar";

// only used during parsing
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 249dfaf

Please sign in to comment.