protected static POM findPOM(POM root, String artifactId) { if (artifactId == null || artifactId.equals(root.getArtifactId())) return root; for (POM child : root.getChildren()) { POM pom = findPOM(child, artifactId); if (pom != null) return pom; } return null; }
public static void main(String[] args) throws Exception { ensureIJDirIsSet(); 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.getArtifactId().equals("pom-ij-base") ? "ij-app" : root.getArtifactId()); POM pom = findPOM(root, artifactId); if (pom == null) pom = root; if (command.equals("compile") || command.equals("build") || command.equals("compile-and-run")) { pom.build(); if (command.equals("compile-and-run")) command = "run"; else return; } else if (command.equals("jar") || command.equals("jars")) { if (!pom.getBuildFromSource()) { System.err.println("Cannot build " + pom + " from source"); System.exit(1); } pom.buildJar(); if (command.equals("jars")) pom.copyDependencies(pom.getTarget(), true); return; } if (command.equals("clean")) pom.clean(); else if (command.equals("get") || command.equals("get-dependencies")) pom.downloadDependencies(); else if (command.equals("run")) { String mainClass = getSystemProperty("mainClass", pom.getMainClass()); if (mainClass == null) { err.println("No main class specified in pom " + pom.getCoordinate()); System.exit(1); } String[] paths = pom.getClassPath(false).split(File.pathSeparator); URL[] urls = new URL[paths.length]; for (int i = 0; i < urls.length; i++) urls[i] = new URL("file:" + paths[i] + (paths[i].endsWith(".jar") ? "" : "/")); URLClassLoader classLoader = new URLClassLoader(urls); // needed for sezpoz Thread.currentThread().setContextClassLoader(classLoader); Class<?> clazz = classLoader.loadClass(mainClass); Method main = clazz.getMethod("main", new Class[] {String[].class}); main.invoke(null, new Object[] {new String[0]}); } else if (command.equals("classpath")) err.println(pom.getClassPath(false)); else if (command.equals("list")) { Set<POM> result = new TreeSet<POM>(); Stack<POM> stack = new Stack<POM>(); stack.push(pom.getRoot()); while (!stack.empty()) { pom = stack.pop(); if (result.contains(pom) || !pom.getBuildFromSource()) continue; result.add(pom); for (POM child : pom.getChildren()) stack.push(child); } for (POM pom2 : result) System.err.println(pom2); } else err.println("Unhandled command: " + command + "\n" + usage); }