Search in sources :

Example 1 with Environment

use of org.apache.tools.ant.types.Environment in project ant by apache.

the class AbstractCvsTask method runCommand.

/**
 * Sets up the environment for toExecute and then runs it.
 * @param toExecute the command line to execute
 * @throws BuildException if failonError is set to true and the cvs command fails
 */
protected void runCommand(Commandline toExecute) throws BuildException {
    // TODO: we should use JCVS (www.ice.com/JCVS) instead of
    // command line execution so that we don't rely on having
    // native CVS stuff around (SM)
    // We can't do it ourselves as jCVS is GPLed, a third party task
    // outside of Apache repositories would be possible though (SB).
    Environment env = new Environment();
    if (port > 0) {
        Environment.Variable var = new Environment.Variable();
        var.setKey("CVS_CLIENT_PORT");
        var.setValue(String.valueOf(port));
        env.addVariable(var);
        // non-standard environment variable used by CVSNT, WinCVS
        // and others
        var = new Environment.Variable();
        var.setKey("CVS_PSERVER_PORT");
        var.setValue(String.valueOf(port));
        env.addVariable(var);
    }
    /**
     * Need a better cross platform integration with <cvspass>, so
     * use the same filename.
     */
    if (passFile == null) {
        File defaultPassFile = new File(System.getProperty("cygwin.user.home", System.getProperty("user.home")) + File.separatorChar + ".cvspass");
        if (defaultPassFile.exists()) {
            this.setPassfile(defaultPassFile);
        }
    }
    if (passFile != null) {
        if (passFile.isFile() && passFile.canRead()) {
            Environment.Variable var = new Environment.Variable();
            var.setKey("CVS_PASSFILE");
            var.setValue(String.valueOf(passFile));
            env.addVariable(var);
            log("Using cvs passfile: " + String.valueOf(passFile), Project.MSG_VERBOSE);
        } else if (!passFile.canRead()) {
            log("cvs passfile: " + String.valueOf(passFile) + " ignored as it is not readable", Project.MSG_WARN);
        } else {
            log("cvs passfile: " + String.valueOf(passFile) + " ignored as it is not a file", Project.MSG_WARN);
        }
    }
    if (cvsRsh != null) {
        Environment.Variable var = new Environment.Variable();
        var.setKey("CVS_RSH");
        var.setValue(String.valueOf(cvsRsh));
        env.addVariable(var);
    }
    // 
    // Just call the getExecuteStreamHandler() and let it handle
    // the semantics of instantiation or retrieval.
    // 
    Execute exe = new Execute(getExecuteStreamHandler(), null);
    exe.setAntRun(getProject());
    if (dest == null) {
        dest = getProject().getBaseDir();
    }
    if (!dest.exists()) {
        dest.mkdirs();
    }
    exe.setWorkingDirectory(dest);
    exe.setCommandline(toExecute.getCommandline());
    exe.setEnvironment(env.getVariables());
    try {
        String actualCommandLine = executeToString(exe);
        log(actualCommandLine, Project.MSG_VERBOSE);
        int retCode = exe.execute();
        log("retCode=" + retCode, Project.MSG_DEBUG);
        if (failOnError && Execute.isFailure(retCode)) {
            throw new BuildException("cvs exited with error code " + retCode + StringUtils.LINE_SEP + "Command line was [" + actualCommandLine + "]", getLocation());
        }
    } catch (IOException e) {
        if (failOnError) {
            throw new BuildException(e, getLocation());
        }
        log("Caught exception: " + e.getMessage(), Project.MSG_WARN);
    } catch (BuildException e) {
        if (failOnError) {
            throw (e);
        }
        Throwable t = e.getCause();
        if (t == null) {
            t = e;
        }
        log("Caught exception: " + t.getMessage(), Project.MSG_WARN);
    } catch (Exception e) {
        if (failOnError) {
            throw new BuildException(e, getLocation());
        }
        log("Caught exception: " + e.getMessage(), Project.MSG_WARN);
    }
}
Also used : Environment(org.apache.tools.ant.types.Environment) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 2 with Environment

use of org.apache.tools.ant.types.Environment in project ant by apache.

the class WebsphereDeploymentTool method buildWebsphereJar.

/**
 * Helper method invoked by execute() for each websphere jar to be built.
 * Encapsulates the logic of constructing a java task for calling
 * websphere.ejbdeploy and executing it.
 *
 * @param sourceJar java.io.File representing the source (EJB1.1) jarfile.
 * @param destJar java.io.File representing the destination, websphere
 *      jarfile.
 */
private void buildWebsphereJar(File sourceJar, File destJar) {
    try {
        if (ejbdeploy) {
            Java javaTask = new Java(getTask());
            // Set the JvmArgs
            javaTask.createJvmarg().setValue("-Xms64m");
            javaTask.createJvmarg().setValue("-Xmx128m");
            // Set the Environment variable
            Environment.Variable var = new Environment.Variable();
            var.setKey("websphere.lib.dir");
            File libdir = new File(websphereHome, "lib");
            var.setValue(libdir.getAbsolutePath());
            javaTask.addSysproperty(var);
            // Set the working directory
            javaTask.setDir(websphereHome);
            // Set the Java class name
            javaTask.setTaskName("ejbdeploy");
            javaTask.setClassname("com.ibm.etools.ejbdeploy.EJBDeploy");
            javaTask.createArg().setValue(sourceJar.getPath());
            javaTask.createArg().setValue(tempdir);
            javaTask.createArg().setValue(destJar.getPath());
            javaTask.createArg().setLine(getOptions());
            if (getCombinedClasspath() != null && getCombinedClasspath().toString().length() > 0) {
                javaTask.createArg().setValue("-cp");
                javaTask.createArg().setValue(getCombinedClasspath().toString());
            }
            Path classpath = wasClasspath;
            if (classpath == null) {
                classpath = getCombinedClasspath();
            }
            javaTask.setFork(true);
            if (classpath != null) {
                javaTask.setClasspath(classpath);
            }
            log("Calling websphere.ejbdeploy for " + sourceJar.toString(), Project.MSG_VERBOSE);
            javaTask.execute();
        }
    } catch (Exception e) {
        // Have to catch this because of the semantics of calling main()
        throw new BuildException("Exception while calling ejbdeploy. Details: " + e.toString(), e);
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Java(org.apache.tools.ant.taskdefs.Java) Environment(org.apache.tools.ant.types.Environment) BuildException(org.apache.tools.ant.BuildException) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 BuildException (org.apache.tools.ant.BuildException)2 Environment (org.apache.tools.ant.types.Environment)2 JarFile (java.util.jar.JarFile)1 Java (org.apache.tools.ant.taskdefs.Java)1 Path (org.apache.tools.ant.types.Path)1