diff --git a/src-plugins/Script_Editor/src/main/java/fiji/scripting/FileFunctions.java b/src-plugins/Script_Editor/src/main/java/fiji/scripting/FileFunctions.java index 045e83499d..87f78cb613 100644 --- a/src-plugins/Script_Editor/src/main/java/fiji/scripting/FileFunctions.java +++ b/src-plugins/Script_Editor/src/main/java/fiji/scripting/FileFunctions.java @@ -3,10 +3,10 @@ import fiji.SimpleExecuter; import fiji.build.Fake; -import fiji.build.POM; import fiji.build.Parser; import fiji.build.Rule; import fiji.build.SubFake; +import fiji.build.minimaven.POM; import ij.IJ; diff --git a/src-plugins/fake/src/main/java/fiji/build/Fake.java b/src-plugins/fake/src/main/java/fiji/build/Fake.java index fe7a99432d..e6f9b69e5f 100644 --- a/src-plugins/fake/src/main/java/fiji/build/Fake.java +++ b/src-plugins/fake/src/main/java/fiji/build/Fake.java @@ -40,10 +40,15 @@ import java.util.zip.ZipException; +import fiji.build.minimaven.JarClassLoader; +import fiji.build.minimaven.JavaCompiler; +import fiji.build.minimaven.JavaCompiler.CompileError; + public class Fake { protected static String fijiBuildJar; protected static long mtimeFijiBuild; public PrintStream out = System.out, err = System.err; + protected JavaCompiler javac; public static void main(String[] args) { checkObsoleteLauncher(); @@ -571,10 +576,12 @@ protected List compileJavas(List javas, File cwd, File buildDir, err.println(output); } + if (javac == null) + javac = new JavaCompiler(err, out); try { - new JavaCompiler(err, out).call(args, verbose); - } catch (FakeException e) { - throw e; + javac.call(args, verbose); + } catch (CompileError e) { + throw new FakeException(e.getMessage(), e); } catch (Exception e) { e.printStackTrace(); throw new FakeException("Compile error: " + e); diff --git a/src-plugins/fake/src/main/java/fiji/build/FakeException.java b/src-plugins/fake/src/main/java/fiji/build/FakeException.java index 3fa06ab5d6..7236f9b53c 100644 --- a/src-plugins/fake/src/main/java/fiji/build/FakeException.java +++ b/src-plugins/fake/src/main/java/fiji/build/FakeException.java @@ -8,6 +8,10 @@ public FakeException(String message) { super(message); } + public FakeException(String message, Throwable cause) { + super(message, cause); + } + public String toString() { return getMessage(); } 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 4f798fa5a8..0c47b2966a 100755 --- a/src-plugins/fake/src/main/java/fiji/build/MiniMaven.java +++ b/src-plugins/fake/src/main/java/fiji/build/MiniMaven.java @@ -1,5 +1,9 @@ package fiji.build; +import fiji.build.minimaven.BuildEnvironment; +import fiji.build.minimaven.Coordinate; +import fiji.build.minimaven.POM; + import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; @@ -78,12 +82,13 @@ public static void ensureIJDirIsSet() { public static void main(String[] args) throws Exception { ensureIJDirIsSet(); - BuildEnvironment miniMaven = new BuildEnvironment(System.err, "true".equals(getSystemProperty("minimaven.verbose", "false")), false); - POM root = miniMaven.parse(new File("pom.xml"), null); + PrintStream err = System.err; + BuildEnvironment env = new BuildEnvironment(err, false, "true".equals(getSystemProperty("minimaven.verbose", "false")), false); + POM root = env.parse(new File("pom.xml"), null); String command = args.length == 0 ? "compile-and-run" : args[0]; - String artifactId = getSystemProperty("artifactId", root.coordinate.artifactId.equals("pom-ij-base") ? "ij-app" : root.coordinate.artifactId); + String artifactId = getSystemProperty("artifactId", root.getArtifactId().equals("pom-ij-base") ? "ij-app" : root.getArtifactId()); - POM pom = root.findPOM(new Coordinate(null, artifactId, null), false, miniMaven.downloadAutomatically); + POM pom = root.findPOM(new Coordinate(null, artifactId, null), false, env.getDownloadAutomatically()); if (pom == null) pom = root; if (command.equals("compile") || command.equals("build") || command.equals("compile-and-run")) { @@ -94,13 +99,13 @@ public static void main(String[] args) throws Exception { return; } else if (command.equals("jar") || command.equals("jars")) { - if (!pom.buildFromSource) { + if (!pom.getBuildFromSource()) { System.err.println("Cannot build " + pom + " from source"); System.exit(1); } pom.buildJar(); if (command.equals("jars")) - pom.copyDependencies(new File(pom.directory, "target"), true); + pom.copyDependencies(pom.getTarget(), true); return; } if (command.equals("clean")) @@ -108,9 +113,9 @@ else if (command.equals("jar") || command.equals("jars")) { else if (command.equals("get") || command.equals("get-dependencies")) pom.downloadDependencies(); else if (command.equals("run")) { - String mainClass = getSystemProperty("mainClass", pom.mainClass); + String mainClass = getSystemProperty("mainClass", pom.getMainClass()); if (mainClass == null) { - miniMaven.err.println("No main class specified in pom " + pom.coordinate); + err.println("No main class specified in pom " + pom.getCoordinate()); System.exit(1); } String[] paths = pom.getClassPath(false).split(File.pathSeparator); @@ -125,14 +130,14 @@ else if (command.equals("run")) { main.invoke(null, new Object[] { new String[0] }); } else if (command.equals("classpath")) - miniMaven.err.println(pom.getClassPath(false)); + err.println(pom.getClassPath(false)); else if (command.equals("list")) { Set result = new TreeSet(); Stack stack = new Stack(); stack.push(pom.getRoot()); while (!stack.empty()) { pom = stack.pop(); - if (result.contains(pom) || !pom.buildFromSource) + if (result.contains(pom) || !pom.getBuildFromSource()) continue; result.add(pom); for (POM child : pom.getChildren()) @@ -142,7 +147,7 @@ else if (command.equals("list")) { System.err.println(pom2); } else - miniMaven.err.println("Unhandled command: " + command + "\n" + usage); + err.println("Unhandled command: " + command + "\n" + usage); } protected static String getSystemProperty(String key, String defaultValue) { diff --git a/src-plugins/fake/src/main/java/fiji/build/SubFake.java b/src-plugins/fake/src/main/java/fiji/build/SubFake.java index cc20f7c064..b975fe75a9 100644 --- a/src-plugins/fake/src/main/java/fiji/build/SubFake.java +++ b/src-plugins/fake/src/main/java/fiji/build/SubFake.java @@ -1,6 +1,8 @@ package fiji.build; -import fiji.build.POM; +import fiji.build.minimaven.BuildEnvironment; +import fiji.build.minimaven.Coordinate; +import fiji.build.minimaven.POM; import java.io.File; import java.util.List; @@ -119,9 +121,10 @@ public POM getPOM() { targetBasename = targetBasename.substring(0, targetBasename.length() - 4); // TODO: targetBasename could end in "-" try { + boolean verbose = getVarBool("VERBOSE"); + boolean debug = getVarBool("DEBUG"); if (miniMaven == null) { - miniMaven = new BuildEnvironment(parser.fake.err, getVarBool("VERBOSE"), getVarBool("DEBUG")); - miniMaven.downloadAutomatically = !miniMaven.offlineMode; + miniMaven = new BuildEnvironment(parser.fake.err, true, verbose, debug); MiniMaven.ensureIJDirIsSet(); String ijDir = System.getProperty("ij.dir"); File submodules = new File(ijDir, "modules"); @@ -139,8 +142,8 @@ public POM getPOM() { } } pom = miniMaven.parse(file); - if (!targetBasename.equals(pom.getArtifact())) - pom = pom.findPOM(new Coordinate(null, targetBasename, null), miniMaven.verbose, miniMaven.downloadAutomatically); + if (!targetBasename.equals(pom.getArtifactId())) + pom = pom.findPOM(new Coordinate(null, targetBasename, null), verbose, miniMaven.getDownloadAutomatically()); } catch (Exception e) { e.printStackTrace(parser.fake.err); } diff --git a/src-plugins/fake/src/main/java/fiji/build/BuildEnvironment.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/BuildEnvironment.java similarity index 98% rename from src-plugins/fake/src/main/java/fiji/build/BuildEnvironment.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/BuildEnvironment.java index f29ee7a909..29a3937223 100644 --- a/src-plugins/fake/src/main/java/fiji/build/BuildEnvironment.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/BuildEnvironment.java @@ -1,4 +1,4 @@ -package fiji.build; +package fiji.build.minimaven; import java.io.File; import java.io.FileInputStream; @@ -50,6 +50,10 @@ public class BuildEnvironment { mavenRepository = repository; } + public boolean getDownloadAutomatically() { + return downloadAutomatically && !offlineMode; + } + protected static boolean isInteractiveConsole() { // We want to compile/run with Java5, so we cannot test System.console() directly try { @@ -59,9 +63,10 @@ protected static boolean isInteractiveConsole() { } } - public BuildEnvironment(PrintStream err, boolean verbose, boolean debug) throws FakeException { + public BuildEnvironment(PrintStream err, boolean downloadAutomatically, boolean verbose, boolean debug) { this.err = err; javac = new JavaCompiler(err, err); + this.downloadAutomatically = downloadAutomatically; this.verbose = verbose; this.debug = debug; if ("true".equalsIgnoreCase(System.getProperty("minimaven.offline"))) diff --git a/src-plugins/fake/src/main/java/fiji/build/Coordinate.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/Coordinate.java similarity index 98% rename from src-plugins/fake/src/main/java/fiji/build/Coordinate.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/Coordinate.java index 047ace03aa..67b6a73c4a 100644 --- a/src-plugins/fake/src/main/java/fiji/build/Coordinate.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/Coordinate.java @@ -1,4 +1,4 @@ -package fiji.build; +package fiji.build.minimaven; public class Coordinate { protected String groupId, artifactId, version, systemPath, classifier, scope, snapshotVersion; @@ -70,4 +70,4 @@ public String toString() { extra = "{" + extra.substring(1) + "}"; return getFileName(true, true, null) + extra; } -} \ No newline at end of file +} diff --git a/src-plugins/fake/src/main/java/fiji/build/JarClassLoader.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/JarClassLoader.java similarity index 88% rename from src-plugins/fake/src/main/java/fiji/build/JarClassLoader.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/JarClassLoader.java index d70fd92b97..44ede79651 100644 --- a/src-plugins/fake/src/main/java/fiji/build/JarClassLoader.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/JarClassLoader.java @@ -1,5 +1,6 @@ -package fiji.build; +package fiji.build.minimaven; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -117,7 +118,7 @@ public synchronized Class loadClass(String name, if (input == null) throw new ClassNotFoundException(name); try { - byte[] buffer = Util.readStream(input); + byte[] buffer = readStream(input); input.close(); result = defineClass(name, buffer, 0, buffer.length); @@ -133,4 +134,18 @@ public synchronized Class loadClass(String name, return result; } } -} \ No newline at end of file + + protected static byte[] readStream(InputStream in) throws IOException { + byte[] buffer = new byte[16384]; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (;;) { + int count = in.read(buffer); + if (count < 0) + break; + out.write(buffer, 0, count); + } + in.close(); + out.close(); + return out.toByteArray(); + } +} diff --git a/src-plugins/fake/src/main/java/fiji/build/JavaCompiler.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/JavaCompiler.java similarity index 69% rename from src-plugins/fake/src/main/java/fiji/build/JavaCompiler.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/JavaCompiler.java index 2359aa5d91..d524bd653e 100644 --- a/src-plugins/fake/src/main/java/fiji/build/JavaCompiler.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/JavaCompiler.java @@ -1,4 +1,4 @@ -package fiji.build; +package fiji.build.minimaven; import java.io.File; import java.io.IOException; @@ -17,7 +17,7 @@ public JavaCompiler(PrintStream err, PrintStream out) { // this function handles the javac singleton public void call(String[] arguments, - boolean verbose) throws FakeException { + boolean verbose) throws CompileError { synchronized(this) { try { if (javac == null) { @@ -33,14 +33,11 @@ public void call(String[] arguments, Object result = javac.invoke(null, new Object[] { arguments, new PrintWriter(err) }); - if (!result.equals(new Integer(0))) { - FakeException e = new FakeException("Compile error"); - e.printStackTrace(); - throw e; - } + if (!result.equals(new Integer(0))) + throw new CompileError(result); return; - } catch (FakeException e) { - /* was compile error */ + } catch (CompileError e) { + /* re-throw */ throw e; } catch (Exception e) { e.printStackTrace(err); @@ -57,13 +54,26 @@ public void call(String[] arguments, try { execute(newArguments, new File("."), verbose); } catch (Exception e) { - throw new FakeException("Could not even fall back " + throw new RuntimeException("Could not even fall back " + " to javac in the PATH"); } } + public static class CompileError extends Exception { + protected Object result; + + public CompileError(Object result) { + super("Compile error: " + result); + this.result = result; + } + + public Object getResult() { + return result; + } + } + protected void execute(String[] args, File dir, boolean verbose) - throws IOException, FakeException { + throws IOException { if (verbose) { String output = "Executing:"; for (int i = 0; i < args.length; i++) @@ -72,48 +82,27 @@ protected void execute(String[] args, File dir, boolean verbose) } /* stupid, stupid Windows... */ - if (Util.getPlatform().startsWith("win")) { - // handle .sh scripts - if (args[0].endsWith(".sh")) { - String[] newArgs = new String[args.length + 1]; - newArgs[0] = "sh.exe"; - System.arraycopy(args, 0, newArgs, 1, args.length); - args = newArgs; - } + if (System.getProperty("os.name").startsWith("Windows")) { for (int i = 0; i < args.length; i++) args[i] = quoteArg(args[i]); // stupid, stupid, stupid Windows taking all my time!!! if (args[0].startsWith("../")) args[0] = new File(dir, args[0]).getAbsolutePath(); - else if ((args[0].equals("bash") || args[0].equals("sh")) && Util.getPlatform().equals("win64")) { - String[] newArgs = new String[args.length + 2]; - newArgs[0] = System.getenv("WINDIR") + "\\SYSWOW64\\cmd.exe"; - newArgs[1] = "/C"; - System.arraycopy(args, 0, newArgs, 2, args.length); - args = newArgs; - if (verbose) { - String output = "Executing (win32 on win64 using SYSWOW64\\cmd.exe):"; - for (int i = 0; i < args.length; i++) - output += " '" + args[i] + "'"; - err.println(output); - } - - } } Process proc = Runtime.getRuntime().exec(args, null, dir); - new StreamDumper(proc.getErrorStream(), err).start(); - new StreamDumper(proc.getInputStream(), out).start(); + new ReadInto(proc.getErrorStream(), err).start(); + new ReadInto(proc.getInputStream(), out).start(); try { proc.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); - throw new FakeException(e.getMessage()); + throw new RuntimeException(e.getMessage()); } int exitValue = proc.exitValue(); if (exitValue != 0) - throw new FakeException("Failed: " + exitValue); + throw new RuntimeException("Failed: " + exitValue); } private static String quotables = " \"\'"; @@ -155,4 +144,4 @@ protected static JarClassLoader discoverJavac() throws IOException { } return new JarClassLoader(javac.getPath()); } -} \ No newline at end of file +} diff --git a/src-plugins/fake/src/main/java/fiji/build/POM.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/POM.java similarity index 96% rename from src-plugins/fake/src/main/java/fiji/build/POM.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/POM.java index 41289ddc75..c67386dd76 100644 --- a/src-plugins/fake/src/main/java/fiji/build/POM.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/POM.java @@ -1,4 +1,6 @@ -package fiji.build; +package fiji.build.minimaven; + +import fiji.build.minimaven.JavaCompiler.CompileError; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -151,10 +153,6 @@ public String getSourcePath() { return expand(sourceDirectory); } - public void buildJar() throws FakeException, IOException, ParserConfigurationException, SAXException { - build(true); - } - protected void addToJarRecursively(JarOutputStream out, File directory, String prefix) throws IOException { for (File file : directory.listFiles()) if (file.isFile()) { @@ -165,11 +163,15 @@ else if (file.isDirectory()) addToJarRecursively(out, file, prefix + file.getName() + "/"); } - public void build() throws FakeException, IOException, ParserConfigurationException, SAXException { + public void buildJar() throws CompileError, IOException, ParserConfigurationException, SAXException { + build(true); + } + + public void build() throws CompileError, IOException, ParserConfigurationException, SAXException { build(false); } - public void build(boolean makeJar) throws FakeException, IOException, ParserConfigurationException, SAXException { + public void build(boolean makeJar) throws CompileError, IOException, ParserConfigurationException, SAXException { if (!buildFromSource || built) return; boolean forceFullBuild = false; @@ -309,11 +311,15 @@ else if (file.isFile()) { return lastModified; } - public String getGroup() { + public Coordinate getCoordinate() { + return coordinate; + } + + public String getGroupId() { return coordinate.groupId; } - public String getArtifact() { + public String getArtifactId() { return coordinate.artifactId; } @@ -325,12 +331,20 @@ public String getJarName() { return coordinate.getJarName(); } + public String getMainClass() { + return mainClass; + } + public File getTarget() { if (!buildFromSource) return target; return new File(new File(directory, "target"), getJarName()); } + public boolean getBuildFromSource() { + return buildFromSource; + } + public String getClassPath(boolean forCompile) throws IOException, ParserConfigurationException, SAXException { StringBuilder builder = new StringBuilder(); builder.append(target); @@ -469,13 +483,13 @@ public String getProperty(String key) { return parent.getProperty(key); } - protected POM[] getChildren() { + public POM[] getChildren() { if (children == null) return new POM[0]; return children; } - protected POM getRoot() { + public POM getRoot() { POM result = this; while (result.parent != null) result = result.parent; @@ -498,7 +512,7 @@ protected void getRepositories(Set result) { child.getRepositories(result); } - protected POM findPOM(Coordinate dependency, boolean quiet, boolean downloadAutomatically) throws IOException, ParserConfigurationException, SAXException { + public POM findPOM(Coordinate dependency, boolean quiet, boolean downloadAutomatically) throws IOException, ParserConfigurationException, SAXException { if (dependency.artifactId.equals(expand(coordinate.artifactId)) && (dependency.groupId == null || dependency.groupId.equals(expand(coordinate.groupId))) && (dependency.version == null || coordinate.version == null || dependency.version.equals(expand(coordinate.version)))) @@ -755,7 +769,7 @@ else if (prefix.equals(">project>dependencies>dependency>systemPath")) else if (prefix.equals(">project>dependencies>dependency>classifier")) latestDependency.classifier = string; else if (prefix.equals(">project>profiles>profile>id")) { - isCurrentProfile = (!Util.getPlatform().equals("macosx") && "javac".equals(string)) || (coordinate.artifactId.equals("javassist") && (string.equals("jdk16") || string.equals("default-tools"))); + isCurrentProfile = (!System.getProperty("os.name").equals("Mac OS X") && "javac".equals(string)) || (coordinate.artifactId.equals("javassist") && (string.equals("jdk16") || string.equals("default-tools"))); if (env.debug) env.err.println((isCurrentProfile ? "Activating" : "Ignoring") + " profile " + string); } @@ -866,4 +880,4 @@ public void append(StringBuilder builder, String indent) { else child.append(builder, indent + " "); } -} \ No newline at end of file +} diff --git a/src-plugins/fake/src/main/java/fiji/build/ReadInto.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/ReadInto.java similarity index 96% rename from src-plugins/fake/src/main/java/fiji/build/ReadInto.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/ReadInto.java index 9b3f00af9d..258ecc095d 100644 --- a/src-plugins/fake/src/main/java/fiji/build/ReadInto.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/ReadInto.java @@ -1,4 +1,4 @@ -package fiji.build; +package fiji.build.minimaven; import java.io.BufferedReader; import java.io.IOException; @@ -38,4 +38,4 @@ public void run() { public String toString() { return buffer.toString(); } -} \ No newline at end of file +} diff --git a/src-plugins/fake/src/main/java/fiji/build/SnapshotPOMHandler.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/SnapshotPOMHandler.java similarity index 98% rename from src-plugins/fake/src/main/java/fiji/build/SnapshotPOMHandler.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/SnapshotPOMHandler.java index d4485c83f9..36ba57caea 100644 --- a/src-plugins/fake/src/main/java/fiji/build/SnapshotPOMHandler.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/SnapshotPOMHandler.java @@ -1,4 +1,4 @@ -package fiji.build; +package fiji.build.minimaven; import java.io.File; import java.io.FileInputStream; @@ -53,4 +53,4 @@ public static String parse(InputStream in) throws IOException, ParserConfigurati return handler.snapshotVersion + "-" + handler.timestamp + "-" + handler.buildNumber; throw new IOException("Missing timestamp/build number: " + handler.timestamp + ", " + handler.buildNumber); } -} \ No newline at end of file +} diff --git a/src-plugins/fake/src/main/java/fiji/build/VersionPOMHandler.java b/src-plugins/fake/src/main/java/fiji/build/minimaven/VersionPOMHandler.java similarity index 98% rename from src-plugins/fake/src/main/java/fiji/build/VersionPOMHandler.java rename to src-plugins/fake/src/main/java/fiji/build/minimaven/VersionPOMHandler.java index 66b6111e42..dfde0b848e 100644 --- a/src-plugins/fake/src/main/java/fiji/build/VersionPOMHandler.java +++ b/src-plugins/fake/src/main/java/fiji/build/minimaven/VersionPOMHandler.java @@ -1,4 +1,4 @@ -package fiji.build; +package fiji.build.minimaven; import java.io.File; import java.io.FileInputStream;