public boolean execute() throws BuildException { getRmic().log("Using SUN rmic compiler", Project.MSG_VERBOSE); Commandline cmd = setupRmicCommand(); // Create an instance of the rmic, redirecting output to // the project log LogOutputStream logstr = new LogOutputStream(getRmic(), Project.MSG_WARN); try { Class c = Class.forName("sun.rmi.rmic.Main"); Constructor cons = c.getConstructor(new Class[] {OutputStream.class, String.class}); Object rmic = cons.newInstance(new Object[] {logstr, "rmic"}); Method doRmic = c.getMethod("compile", new Class[] {String[].class}); Boolean ok = (Boolean) doRmic.invoke(rmic, (new Object[] {cmd.getArguments()})); return ok.booleanValue(); } catch (ClassNotFoundException ex) { throw new BuildException( "Cannot use SUN rmic, as it is not " + "available. A common solution is to " + "set the environment variable " + "JAVA_HOME or CLASSPATH.", getRmic().getLocation()); } catch (Exception ex) { if (ex instanceof BuildException) { throw (BuildException) ex; } else { throw new BuildException("Error starting SUN rmic: ", ex, getRmic().getLocation()); } } finally { try { logstr.close(); } catch (IOException e) { throw new BuildException(e); } } }
/** * execute this task. * * @throws BuildException on error. */ public void execute() throws BuildException { checkConfiguration(); Vector files = getFileList(); // quick exit if the target is up to date if (isUpToDate(files)) { return; } log("Building " + archiveType + ": " + cabFile.getAbsolutePath()); if (!Os.isFamily("windows")) { log("Using listcab/libcabinet", Project.MSG_VERBOSE); StringBuffer sb = new StringBuffer(); Enumeration fileEnum = files.elements(); while (fileEnum.hasMoreElements()) { sb.append(fileEnum.nextElement()).append("\n"); } sb.append("\n").append(cabFile.getAbsolutePath()).append("\n"); try { Process p = Execute.launch( getProject(), new String[] {"listcab"}, null, baseDir != null ? baseDir : getProject().getBaseDir(), true); OutputStream out = p.getOutputStream(); // Create the stream pumpers to forward listcab's stdout and stderr to the log // note: listcab is an interactive program, and issues prompts for every new line. // Therefore, make it show only with verbose logging turned on. LogOutputStream outLog = new LogOutputStream(this, Project.MSG_VERBOSE); LogOutputStream errLog = new LogOutputStream(this, Project.MSG_ERR); StreamPumper outPump = new StreamPumper(p.getInputStream(), outLog); StreamPumper errPump = new StreamPumper(p.getErrorStream(), errLog); // Pump streams asynchronously (new Thread(outPump)).start(); (new Thread(errPump)).start(); out.write(sb.toString().getBytes()); out.flush(); out.close(); // A wild default for when the thread is interrupted int result = DEFAULT_RESULT; try { // Wait for the process to finish result = p.waitFor(); // Wait for the end of output and error streams outPump.waitFor(); outLog.close(); errPump.waitFor(); errLog.close(); } catch (InterruptedException ie) { log("Thread interrupted: " + ie); } // Informative summary message in case of errors if (Execute.isFailure(result)) { log("Error executing listcab; error code: " + result); } } catch (IOException ex) { String msg = "Problem creating " + cabFile + " " + ex.getMessage(); throw new BuildException(msg, getLocation()); } } else { try { File listFile = createListFile(files); ExecTask exec = createExec(); File outFile = null; // die if cabarc fails exec.setFailonerror(true); exec.setDir(baseDir); if (!doVerbose) { outFile = FILE_UTILS.createTempFile("ant", "", null, true, true); exec.setOutput(outFile); } exec.setExecutable("cabarc"); exec.createArg().setValue("-r"); exec.createArg().setValue("-p"); if (!doCompress) { exec.createArg().setValue("-m"); exec.createArg().setValue("none"); } if (cmdOptions != null) { exec.createArg().setLine(cmdOptions); } exec.createArg().setValue("n"); exec.createArg().setFile(cabFile); exec.createArg().setValue("@" + listFile.getAbsolutePath()); exec.execute(); if (outFile != null) { outFile.delete(); } listFile.delete(); } catch (IOException ioe) { String msg = "Problem creating " + cabFile + " " + ioe.getMessage(); throw new BuildException(msg, getLocation()); } } }