⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exectask.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setFailIfExecutionFails(boolean flag) {        failIfExecFails = flag;        incompatibleWithSpawn = true;    }    /**     * Set whether output should be appended to or overwrite an existing file.     * Defaults to false.     *     * @param append if true append is desired.     *     * @since 1.30, Ant 1.5     */    public void setAppend(boolean append) {        redirector.setAppend(append);        incompatibleWithSpawn = true;    }    /**     * Add a <code>RedirectorElement</code> to this task.     *     * @param redirectorElement   <code>RedirectorElement</code>.     * @since Ant 1.6.2     */    public void addConfiguredRedirector(RedirectorElement redirectorElement) {        if (this.redirectorElement != null) {            throw new BuildException("cannot have > 1 nested <redirector>s");        }        this.redirectorElement = redirectorElement;        incompatibleWithSpawn = true;    }    /**     * Restrict this execution to a single OS Family     * @param osFamily the family to restrict to.     */    public void setOsFamily(String osFamily) {        this.osFamily = osFamily.toLowerCase(Locale.US);    }    /**     * The method attempts to figure out where the executable is so that we can feed     * the full path. We first try basedir, then the exec dir, and then     * fallback to the straight executable name (i.e. on the path).     *     * @param exec the name of the executable.     * @param mustSearchPath if true, the executable will be looked up in     * the PATH environment and the absolute path is returned.     *     * @return the executable as a full path if it can be determined.     *     * @since Ant 1.6     */    protected String resolveExecutable(String exec, boolean mustSearchPath) {        if (!resolveExecutable) {            return exec;        }        // try to find the executable        File executableFile = getProject().resolveFile(exec);        if (executableFile.exists()) {            return executableFile.getAbsolutePath();        }        // now try to resolve against the dir if given        if (dir != null) {            executableFile = FILE_UTILS.resolveFile(dir, exec);            if (executableFile.exists()) {                return executableFile.getAbsolutePath();            }        }        // couldn't find it - must be on path        if (mustSearchPath) {            Path p = null;            String[] environment = env.getVariables();            if (environment != null) {                for (int i = 0; i < environment.length; i++) {                    if (isPath(environment[i])) {                        p = new Path(getProject(), getPath(environment[i]));                        break;                    }                }            }            if (p == null) {                Vector envVars = Execute.getProcEnvironment();                Enumeration e = envVars.elements();                while (e.hasMoreElements()) {                    String line = (String) e.nextElement();                    if (isPath(line)) {                        p = new Path(getProject(), getPath(line));                        break;                    }                }            }            if (p != null) {                String[] dirs = p.list();                for (int i = 0; i < dirs.length; i++) {                    executableFile                        = FILE_UTILS.resolveFile(new File(dirs[i]), exec);                    if (executableFile.exists()) {                        return executableFile.getAbsolutePath();                    }                }            }        }        // mustSearchPath is false, or no PATH or not found - keep our        // fingers crossed.        return exec;    }    /**     * Do the work.     *     * @throws BuildException in a number of circumstances:     * <ul>     * <li>if failIfExecFails is set to true and the process cannot be started</li>     * <li>the java13command launcher can send build exceptions</li>     * <li>this list is not exhaustive or limitative</li>     * </ul>     */    public void execute() throws BuildException {        // Quick fail if this is not a valid OS for the command        if (!isValidOs()) {            return;        }        File savedDir = dir; // possibly altered in prepareExec        cmdl.setExecutable(resolveExecutable(executable, searchPath));        checkConfiguration();        try {            runExec(prepareExec());        } finally {            dir = savedDir;        }    }    /**     * Has the user set all necessary attributes?     * @throws BuildException if there are missing required parameters.     */    protected void checkConfiguration() throws BuildException {        if (cmdl.getExecutable() == null) {            throw new BuildException("no executable specified", getLocation());        }        if (dir != null && !dir.exists()) {            throw new BuildException("The directory " + dir + " does not exist");        }        if (dir != null && !dir.isDirectory()) {            throw new BuildException(dir + " is not a directory");        }        if (spawn && incompatibleWithSpawn) {            getProject().log("spawn does not allow attributes related to input, "            + "output, error, result", Project.MSG_ERR);            getProject().log("spawn also does not allow timeout", Project.MSG_ERR);            getProject().log("finally, spawn is not compatible "                + "with a nested I/O <redirector>", Project.MSG_ERR);            throw new BuildException("You have used an attribute "                + "or nested element which is not compatible with spawn");        }        setupRedirector();    }    /**     * Set up properties on the redirector that we needed to store locally.     */    protected void setupRedirector() {        redirector.setInput(input);        redirector.setInputString(inputString);        redirector.setOutput(output);        redirector.setError(error);    }    /**     * Is this the OS the user wanted?     * @return boolean.     * <ul>     * <li>     * <li><code>true</code> if the os and osfamily attributes are null.</li>     * <li><code>true</code> if osfamily is set, and the os family and must match     * that of the current OS, according to the logic of     * {@link Os#isOs(String, String, String, String)}, and the result of the     * <code>os</code> attribute must also evaluate true.     * </li>     * <li>     * <code>true</code> if os is set, and the system.property os.name     * is found in the os attribute,</li>     * <li><code>false</code> otherwise.</li>     * </ul>     */    protected boolean isValidOs() {        //hand osfamily off to Os class, if set        if (osFamily != null && !Os.isOs(osFamily, null, null, null)) {            return false;        }        //the Exec OS check is different from Os.isOs(), which        //probes for a specific OS. Instead it searches the os field        //for the current os.name        String myos = System.getProperty("os.name");        log("Current OS is " + myos, Project.MSG_VERBOSE);        if ((os != null) && (os.indexOf(myos) < 0)) {            // this command will be executed only on the specified OS            log("This OS, " + myos                    + " was not found in the specified list of valid OSes: " + os,                    Project.MSG_VERBOSE);            return false;        }        return true;    }    /**     * Set whether to launch new process with VM, otherwise use the OS's shell.     * Default value is true.     * @param vmLauncher true if we want to launch new process with VM,     * false if we want to use the OS's shell.     */    public void setVMLauncher(boolean vmLauncher) {        this.vmLauncher = vmLauncher;    }    /**     * Create an Execute instance with the correct working directory set.     *     * @return an instance of the Execute class.     *     * @throws BuildException under unknown circumstances.     */    protected Execute prepareExec() throws BuildException {        // default directory to the project's base directory        if (dir == null) {            dir = getProject().getBaseDir();        }        if (redirectorElement != null) {            redirectorElement.configure(redirector);        }        Execute exe = new Execute(createHandler(), createWatchdog());        exe.setAntRun(getProject());        exe.setWorkingDirectory(dir);        exe.setVMLauncher(vmLauncher);        exe.setSpawn(spawn);        String[] environment = env.getVariables();        if (environment != null) {            for (int i = 0; i < environment.length; i++) {                log("Setting environment variable: " + environment[i],                    Project.MSG_VERBOSE);            }        }        exe.setNewenvironment(newEnvironment);        exe.setEnvironment(environment);        return exe;    }    /**     * A Utility method for this classes and subclasses to run an     * Execute instance (an external command).     *     * @param exe instance of the execute class.     *     * @throws IOException in case of problem to attach to the stdin/stdout/stderr     * streams of the process.     */    protected final void runExecute(Execute exe) throws IOException {        int returnCode = -1; // assume the worst        if (!spawn) {            returnCode = exe.execute();            //test for and handle a forced process death            if (exe.killedProcess()) {                String msg = "Timeout: killed the sub-process";                if (failOnError) {                    throw new BuildException(msg);                } else {                    log(msg, Project.MSG_WARN);                }            }            maybeSetResultPropertyValue(returnCode);            redirector.complete();            if (Execute.isFailure(returnCode)) {                if (failOnError) {                    throw new BuildException(getTaskType() + " returned: "                        + returnCode, getLocation());                } else {                    log("Result: " + returnCode, Project.MSG_ERR);                }            }        } else {            exe.spawn();        }    }    /**     * Run the command using the given Execute instance. This may be     * overridden by subclasses.     *     * @param exe instance of Execute to run.     *     * @throws BuildException if the new process could not be started     * only if failIfExecFails is set to true (the default).     */    protected void runExec(Execute exe) throws BuildException {        // show the command        log(cmdl.describeCommand(), Project.MSG_VERBOSE);        exe.setCommandline(cmdl.getCommandline());        try {            runExecute(exe);        } catch (IOException e) {            if (failIfExecFails) {                throw new BuildException("Execute failed: " + e.toString(), e,                                         getLocation());            } else {                log("Execute failed: " + e.toString(), Project.MSG_ERR);            }        } finally {            // close the output file if required            logFlush();        }    }    /**     * Create the StreamHandler to use with our Execute instance.     *     * @return instance of ExecuteStreamHandler.     *     * @throws BuildException under unknown circumstances.     */    protected ExecuteStreamHandler createHandler() throws BuildException {        return redirector.createHandler();    }    /**     * Create the Watchdog to kill a runaway process.     *     * @return instance of ExecuteWatchdog.     *     * @throws BuildException under unknown circumstances.     */    protected ExecuteWatchdog createWatchdog() throws BuildException {        return (timeout == null)            ? null : new ExecuteWatchdog(timeout.longValue());    }    /**     * Flush the output stream - if there is one.     */    protected void logFlush() {    }    private boolean isPath(String line) {        return line.startsWith("PATH=")            || line.startsWith("Path=");    }    private String getPath(String line) {        return line.substring("PATH=".length());    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -