Search in sources :

Example 1 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class Pvcs method execute.

/**
 * @throws BuildException Something is stopping the build...
 */
@Override
public void execute() throws BuildException {
    int result = 0;
    if (repository == null || repository.trim().isEmpty()) {
        throw new BuildException("Required argument repository not specified");
    }
    // Check workspace exists
    // Launch PCLI listversionedfiles -z -aw
    // Capture output
    // build the command line from what we got the format is
    Commandline commandLine = new Commandline();
    commandLine.setExecutable(getExecutable(PCLI_EXE));
    commandLine.createArgument().setValue("lvf");
    commandLine.createArgument().setValue("-z");
    commandLine.createArgument().setValue("-aw");
    if (getWorkspace() != null) {
        commandLine.createArgument().setValue("-sp" + getWorkspace());
    }
    commandLine.createArgument().setValue("-pr" + getRepository());
    String uid = getUserId();
    if (uid != null) {
        commandLine.createArgument().setValue("-id" + uid);
    }
    // default pvcs project is "/"
    if (getPvcsproject() == null && getPvcsprojects().isEmpty()) {
        pvcsProject = "/";
    }
    if (getPvcsproject() != null) {
        commandLine.createArgument().setValue(getPvcsproject());
    }
    if (!getPvcsprojects().isEmpty()) {
        for (PvcsProject pvcsProject : getPvcsprojects()) {
            String projectName = pvcsProject.getName();
            if (projectName == null || projectName.trim().isEmpty()) {
                throw new BuildException("name is a required attribute of pvcsproject");
            }
            commandLine.createArgument().setValue(projectName);
        }
    }
    File tmp = null;
    File tmp2 = null;
    try {
        Random rand = new Random(System.currentTimeMillis());
        tmp = new File("pvcs_ant_" + rand.nextLong() + ".log");
        OutputStream fos = Files.newOutputStream(tmp.toPath());
        tmp2 = new File("pvcs_ant_" + rand.nextLong() + ".log");
        log(commandLine.describeCommand(), Project.MSG_VERBOSE);
        try {
            result = runCmd(commandLine, new PumpStreamHandler(fos, new LogOutputStream(this, Project.MSG_WARN)));
        } finally {
            FileUtils.close(fos);
        }
        if (Execute.isFailure(result) && !ignorerc) {
            String msg = "Failed executing: " + commandLine.toString();
            throw new BuildException(msg, getLocation());
        }
        if (!tmp.exists()) {
            throw new BuildException("Communication between ant and pvcs " + "failed. No output generated from executing PVCS " + "commandline interface \"pcli\" and \"get\"");
        }
        // Create folders in workspace
        log("Creating folders", Project.MSG_INFO);
        createFolders(tmp);
        // Massage PCLI lvf output transforming '\' to '/' so get command works appropriately
        massagePCLI(tmp, tmp2);
        // Launch get on output captured from PCLI lvf
        commandLine.clearArgs();
        commandLine.setExecutable(getExecutable(GET_EXE));
        if (getConfig() != null && getConfig().length() > 0) {
            commandLine.createArgument().setValue("-c" + getConfig());
        }
        if (getForce() != null && getForce().equals("yes")) {
            commandLine.createArgument().setValue("-Y");
        } else {
            commandLine.createArgument().setValue("-N");
        }
        if (getPromotiongroup() != null) {
            commandLine.createArgument().setValue("-G" + getPromotiongroup());
        } else {
            if (getLabel() != null) {
                commandLine.createArgument().setValue("-v" + getLabel());
            } else {
                if (getRevision() != null) {
                    commandLine.createArgument().setValue("-r" + getRevision());
                }
            }
        }
        if (updateOnly) {
            commandLine.createArgument().setValue("-U");
        }
        commandLine.createArgument().setValue("@" + tmp2.getAbsolutePath());
        log("Getting files", Project.MSG_INFO);
        log("Executing " + commandLine.toString(), Project.MSG_VERBOSE);
        result = runCmd(commandLine, new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
        if (result != 0 && !ignorerc) {
            String msg = "Failed executing: " + commandLine.toString() + ". Return code was " + result;
            throw new BuildException(msg, getLocation());
        }
    } catch (FileNotFoundException e) {
        String msg = "Failed executing: " + commandLine.toString() + ". Exception: " + e.getMessage();
        throw new BuildException(msg, getLocation());
    } catch (IOException e) {
        String msg = "Failed executing: " + commandLine.toString() + ". Exception: " + e.getMessage();
        throw new BuildException(msg, getLocation());
    } catch (ParseException e) {
        String msg = "Failed executing: " + commandLine.toString() + ". Exception: " + e.getMessage();
        throw new BuildException(msg, getLocation());
    } finally {
        if (tmp != null) {
            tmp.delete();
        }
        if (tmp2 != null) {
            tmp2.delete();
        }
    }
}
Also used : Commandline(org.apache.tools.ant.types.Commandline) OutputStream(java.io.OutputStream) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LogStreamHandler(org.apache.tools.ant.taskdefs.LogStreamHandler) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) PumpStreamHandler(org.apache.tools.ant.taskdefs.PumpStreamHandler) Random(java.util.Random) BuildException(org.apache.tools.ant.BuildException) ParseException(java.text.ParseException) File(java.io.File)

Example 2 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class Symlink method loadLinks.

/**
 * Load links from properties files included in one or more FileSets.
 *
 * <p>This method is only invoked when the action attribute is set to
 * &quot;recreate&quot;. The filesets passed in are assumed to specify the
 * names of the property files with the link information and the
 * subdirectories in which to look for them.</p>
 *
 * @param fileSets    The <code>FileSet</code>s for this task.
 * @return            The links to be made.
 */
private Properties loadLinks(List<FileSet> fileSets) {
    Properties finalList = new Properties();
    // loop through the supplied file sets:
    for (FileSet fs : fileSets) {
        DirectoryScanner ds = new DirectoryScanner();
        fs.setupDirectoryScanner(ds, getProject());
        ds.setFollowSymlinks(false);
        ds.scan();
        File dir = fs.getDir(getProject());
        // load included files as properties files:
        for (String name : ds.getIncludedFiles()) {
            File inc = new File(dir, name);
            File pf = inc.getParentFile();
            Properties lnks = new Properties();
            try (InputStream is = new BufferedInputStream(Files.newInputStream(inc.toPath()))) {
                lnks.load(is);
                pf = pf.getCanonicalFile();
            } catch (FileNotFoundException fnfe) {
                handleError("Unable to find " + name + "; skipping it.");
                continue;
            } catch (IOException ioe) {
                handleError("Unable to open " + name + " or its parent dir; skipping it.");
                continue;
            }
            try {
                lnks.store(new PrintStream(new LogOutputStream(this, Project.MSG_INFO)), "listing properties");
            } catch (IOException ex) {
                log("failed to log unshortened properties");
                lnks.list(new PrintStream(new LogOutputStream(this, Project.MSG_INFO)));
            }
            // working directory:
            for (String key : lnks.stringPropertyNames()) {
                finalList.put(new File(pf, key).getAbsolutePath(), lnks.getProperty(key));
            }
        }
    }
    return finalList;
}
Also used : PrintStream(java.io.PrintStream) FileSet(org.apache.tools.ant.types.FileSet) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream)

Example 3 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class JUnitTask method execute.

/**
 * Execute a list of tests in a single forked Java VM.
 * @param testList the list of tests to execute.
 * @param thread Identifies which thread is test running in (0 for single-threaded runs)
 * @throws BuildException on error.
 */
protected void execute(final List<JUnitTest> testList, final int thread) throws BuildException {
    // Create a temporary file to pass the test cases to run to
    // the runner (one test case per line)
    final File casesFile = createTempPropertiesFile("junittestcases");
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(casesFile))) {
        log("Creating casesfile '" + casesFile.getAbsolutePath() + "' with content: ", Project.MSG_VERBOSE);
        final PrintStream logWriter = new PrintStream(new LogOutputStream(this, Project.MSG_VERBOSE));
        JUnitTest test = null;
        for (JUnitTest t : testList) {
            test = t;
            test.setThread(thread);
            printDual(writer, logWriter, test.getName());
            if (test.getMethods() != null) {
                printDual(writer, logWriter, ":" + test.getMethodsString().replace(',', '+'));
            }
            if (test.getTodir() == null) {
                printDual(writer, logWriter, "," + getProject().resolveFile("."));
            } else {
                printDual(writer, logWriter, "," + test.getTodir());
            }
            if (test.getOutfile() == null) {
                printlnDual(writer, logWriter, "," + "TEST-" + test.getName());
            } else {
                printlnDual(writer, logWriter, "," + test.getOutfile());
            }
        }
        writer.flush();
        // execute the test and get the return code
        final ExecuteWatchdog watchdog = createWatchdog();
        final TestResultHolder result = executeAsForked(test, watchdog, casesFile);
        actOnTestResult(result, test, "Tests");
    } catch (final IOException e) {
        log(e.toString(), Project.MSG_ERR);
        throw new BuildException(e);
    } finally {
        try {
            FILE_UTILS.tryHardToDelete(casesFile);
        } catch (final Exception e) {
            log(e.toString(), Project.MSG_ERR);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ExecuteWatchdog(org.apache.tools.ant.taskdefs.ExecuteWatchdog) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) BufferedWriter(java.io.BufferedWriter)

Example 4 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class Javac12 method execute.

/**
 * Run the compilation.
 * @return true if the compiler ran with a zero exit result (ok)
 * @exception BuildException if the compilation has problems.
 */
public boolean execute() throws BuildException {
    attributes.log("Using classic compiler", Project.MSG_VERBOSE);
    Commandline cmd = setupJavacCommand(true);
    OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN);
    try {
        // Create an instance of the compiler, redirecting output to
        // the project log
        Class<?> c = Class.forName(CLASSIC_COMPILER_CLASSNAME);
        Constructor cons = c.getConstructor(OutputStream.class, String.class);
        Object compiler = cons.newInstance(logstr, "javac");
        // Call the compile() method
        Method compile = c.getMethod("compile", String[].class);
        return (Boolean) compile.invoke(compiler, new Object[] { cmd.getArguments() });
    } catch (ClassNotFoundException ex) {
        throw new BuildException("Cannot use classic compiler, as it is " + "not available. \n" + " A common solution is " + "to set the environment variable" + " JAVA_HOME to your jdk directory.\n" + "It is currently set to \"" + JavaEnvUtils.getJavaHome() + "\"", location);
    } catch (Exception ex) {
        if (ex instanceof BuildException) {
            throw (BuildException) ex;
        } else {
            throw new BuildException("Error starting classic compiler: ", ex, location);
        }
    } finally {
        FileUtils.close(logstr);
    }
}
Also used : Commandline(org.apache.tools.ant.types.Commandline) Constructor(java.lang.reflect.Constructor) OutputStream(java.io.OutputStream) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) Method(java.lang.reflect.Method) BuildException(org.apache.tools.ant.BuildException) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) BuildException(org.apache.tools.ant.BuildException)

Example 5 with LogOutputStream

use of org.apache.tools.ant.taskdefs.LogOutputStream in project ant by apache.

the class Rpm method execute.

/**
 * Execute the task
 *
 * @throws BuildException is there is a problem in the task execution.
 */
@Override
public void execute() throws BuildException {
    Commandline toExecute = new Commandline();
    toExecute.setExecutable(rpmBuildCommand == null ? guessRpmBuildCommand() : rpmBuildCommand);
    if (topDir != null) {
        toExecute.createArgument().setValue("--define");
        toExecute.createArgument().setValue("_topdir " + topDir);
    }
    toExecute.createArgument().setLine(command);
    if (cleanBuildDir) {
        toExecute.createArgument().setValue("--clean");
    }
    if (removeSpec) {
        toExecute.createArgument().setValue("--rmspec");
    }
    if (removeSource) {
        toExecute.createArgument().setValue("--rmsource");
    }
    toExecute.createArgument().setValue("SPECS/" + specFile);
    ExecuteStreamHandler streamhandler = null;
    OutputStream outputstream = null;
    OutputStream errorstream = null;
    if (error == null && output == null) {
        if (!quiet) {
            streamhandler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
        } else {
            streamhandler = new LogStreamHandler(this, Project.MSG_DEBUG, Project.MSG_DEBUG);
        }
    } else {
        if (output != null) {
            OutputStream fos = null;
            try {
                // NOSONAR
                fos = Files.newOutputStream(output.toPath());
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                outputstream = new PrintStream(bos);
            } catch (IOException e) {
                FileUtils.close(fos);
                throw new BuildException(e, getLocation());
            }
        } else if (!quiet) {
            outputstream = new LogOutputStream(this, Project.MSG_INFO);
        } else {
            outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
        }
        if (error != null) {
            OutputStream fos = null;
            try {
                fos = Files.newOutputStream(error.toPath());
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                errorstream = new PrintStream(bos);
            } catch (IOException e) {
                FileUtils.close(fos);
                throw new BuildException(e, getLocation());
            }
        } else if (!quiet) {
            errorstream = new LogOutputStream(this, Project.MSG_WARN);
        } else {
            errorstream = new LogOutputStream(this, Project.MSG_DEBUG);
        }
        streamhandler = new PumpStreamHandler(outputstream, errorstream);
    }
    Execute exe = getExecute(toExecute, streamhandler);
    try {
        log("Building the RPM based on the " + specFile + " file");
        int returncode = exe.execute();
        if (Execute.isFailure(returncode)) {
            String msg = "'" + toExecute.getExecutable() + "' failed with exit code " + returncode;
            if (failOnError) {
                throw new BuildException(msg);
            }
            log(msg, Project.MSG_ERR);
        }
    } catch (IOException e) {
        throw new BuildException(e, getLocation());
    } finally {
        FileUtils.close(outputstream);
        FileUtils.close(errorstream);
    }
}
Also used : PrintStream(java.io.PrintStream) Commandline(org.apache.tools.ant.types.Commandline) Execute(org.apache.tools.ant.taskdefs.Execute) ExecuteStreamHandler(org.apache.tools.ant.taskdefs.ExecuteStreamHandler) OutputStream(java.io.OutputStream) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) LogStreamHandler(org.apache.tools.ant.taskdefs.LogStreamHandler) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) PumpStreamHandler(org.apache.tools.ant.taskdefs.PumpStreamHandler) BuildException(org.apache.tools.ant.BuildException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)9 IOException (java.io.IOException)8 BuildException (org.apache.tools.ant.BuildException)8 OutputStream (java.io.OutputStream)5 File (java.io.File)4 Commandline (org.apache.tools.ant.types.Commandline)4 PrintStream (java.io.PrintStream)3 PumpStreamHandler (org.apache.tools.ant.taskdefs.PumpStreamHandler)3 FileNotFoundException (java.io.FileNotFoundException)2 Method (java.lang.reflect.Method)2 Execute (org.apache.tools.ant.taskdefs.Execute)2 LogStreamHandler (org.apache.tools.ant.taskdefs.LogStreamHandler)2 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)1 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1