📄 exectask.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import java.io.File;import java.io.IOException;import java.util.Enumeration;import java.util.Vector;import java.util.Locale;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.apache.tools.ant.taskdefs.condition.Os;import org.apache.tools.ant.types.Commandline;import org.apache.tools.ant.types.Environment;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.RedirectorElement;import org.apache.tools.ant.util.FileUtils;/** * Executes a given command if the os platform is appropriate. * * @since Ant 1.2 * * @ant.task category="control" */public class ExecTask extends Task { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private String os; private String osFamily; private File dir; // CheckStyle:VisibilityModifier OFF - bc protected boolean failOnError = false; protected boolean newEnvironment = false; private Long timeout = null; private Environment env = new Environment(); protected Commandline cmdl = new Commandline(); private String resultProperty; private boolean failIfExecFails = true; private String executable; private boolean resolveExecutable = false; private boolean searchPath = false; private boolean spawn = false; private boolean incompatibleWithSpawn = false; //include locally for screening purposes private String inputString; private File input; private File output; private File error; protected Redirector redirector = new Redirector(this); protected RedirectorElement redirectorElement; // CheckStyle:VisibilityModifier ON /** * Controls whether the VM (1.3 and above) is used to execute the * command */ private boolean vmLauncher = true; /** * Create an instance. * Needs to be configured by binding to a project. */ public ExecTask() { } /** * create an instance that is helping another task. * Project, OwningTarget, TaskName and description are all * pulled out * @param owner task that we belong to */ public ExecTask(Task owner) { bindToOwner(owner); } /** * Set whether or not you want the process to be spawned. * Default is false. * @param spawn if true you do not want Ant to wait for the end of the process. * @since Ant 1.6 */ public void setSpawn(boolean spawn) { this.spawn = spawn; } /** * Set the timeout in milliseconds after which the process will be killed. * * @param value timeout in milliseconds. * * @since Ant 1.5 */ public void setTimeout(Long value) { timeout = value; incompatibleWithSpawn = true; } /** * Set the timeout in milliseconds after which the process will be killed. * * @param value timeout in milliseconds. */ public void setTimeout(Integer value) { setTimeout( (Long) ((value == null) ? null : new Long(value.intValue()))); } /** * Set the name of the executable program. * @param value the name of the executable program. */ public void setExecutable(String value) { this.executable = value; cmdl.setExecutable(value); } /** * Set the working directory of the process. * @param d the working directory of the process. */ public void setDir(File d) { this.dir = d; } /** * List of operating systems on which the command may be executed. * @param os list of operating systems on which the command may be executed. */ public void setOs(String os) { this.os = os; } /** * Sets a command line. * @param cmdl command line. * @ant.attribute ignore="true" */ public void setCommand(Commandline cmdl) { log("The command attribute is deprecated.\n" + "Please use the executable attribute and nested arg elements.", Project.MSG_WARN); this.cmdl = cmdl; } /** * File the output of the process is redirected to. If error is not * redirected, it too will appear in the output. * * @param out name of a file to which output should be sent. */ public void setOutput(File out) { this.output = out; incompatibleWithSpawn = true; } /** * Set the input file to use for the task. * * @param input name of a file from which to get input. */ public void setInput(File input) { if (inputString != null) { throw new BuildException("The \"input\" and \"inputstring\" " + "attributes cannot both be specified"); } this.input = input; incompatibleWithSpawn = true; } /** * Set the string to use as input. * * @param inputString the string which is used as the input source. */ public void setInputString(String inputString) { if (input != null) { throw new BuildException("The \"input\" and \"inputstring\" " + "attributes cannot both be specified"); } this.inputString = inputString; incompatibleWithSpawn = true; } /** * Controls whether error output of exec is logged. This is only useful when * output is being redirected and error output is desired in the Ant log. * * @param logError set to true to log error output in the normal ant log. */ public void setLogError(boolean logError) { redirector.setLogError(logError); incompatibleWithSpawn |= logError; } /** * Set the File to which the error stream of the process should be redirected. * * @param error a file to which stderr should be sent. * * @since Ant 1.6 */ public void setError(File error) { this.error = error; incompatibleWithSpawn = true; } /** * Sets the property name whose value should be set to the output of * the process. * * @param outputProp name of property. */ public void setOutputproperty(String outputProp) { redirector.setOutputProperty(outputProp); incompatibleWithSpawn = true; } /** * Sets the name of the property whose value should be set to the error of * the process. * * @param errorProperty name of property. * * @since Ant 1.6 */ public void setErrorProperty(String errorProperty) { redirector.setErrorProperty(errorProperty); incompatibleWithSpawn = true; } /** * Fail if the command exits with a non-zero return code. * * @param fail if true fail the command on non-zero return code. */ public void setFailonerror(boolean fail) { failOnError = fail; incompatibleWithSpawn |= fail; } /** * Do not propagate old environment when new environment variables are specified. * * @param newenv if true, do not propagate old environment * when new environment variables are specified. */ public void setNewenvironment(boolean newenv) { newEnvironment = newenv; } /** * Set whether to attempt to resolve the executable to a file. * * @param resolveExecutable if true, attempt to resolve the * path of the executable. */ public void setResolveExecutable(boolean resolveExecutable) { this.resolveExecutable = resolveExecutable; } /** * Set whether to search nested, then * system PATH environment variables for the executable. * * @param searchPath if true, search PATHs. */ public void setSearchPath(boolean searchPath) { this.searchPath = searchPath; } /** * Indicates whether to attempt to resolve the executable to a * file. * @return the resolveExecutable flag * * @since Ant 1.6 */ public boolean getResolveExecutable() { return resolveExecutable; } /** * Add an environment variable to the launched process. * * @param var new environment variable. */ public void addEnv(Environment.Variable var) { env.addVariable(var); } /** * Adds a command-line argument. * * @return new command line argument created. */ public Commandline.Argument createArg() { return cmdl.createArgument(); } /** * Sets the name of a property in which the return code of the * command should be stored. Only of interest if failonerror=false. * * @since Ant 1.5 * * @param resultProperty name of property. */ public void setResultProperty(String resultProperty) { this.resultProperty = resultProperty; incompatibleWithSpawn = true; } /** * Helper method to set result property to the * passed in value if appropriate. * * @param result value desired for the result property value. */ protected void maybeSetResultPropertyValue(int result) { if (resultProperty != null) { String res = Integer.toString(result); getProject().setNewProperty(resultProperty, res); } } /** * Set whether to stop the build if program cannot be started. * Defaults to true. * * @param flag stop the build if program cannot be started. * * @since Ant 1.5 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -