You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExecTask.java 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.*;
  56. import org.apache.tools.ant.types.*;
  57. import java.io.*;
  58. /**
  59. * Executes a given command if the os platform is appropriate.
  60. *
  61. * @author duncan@x180.com
  62. * @author rubys@us.ibm.com
  63. * @author thomas.haas@softwired-inc.com
  64. * @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
  65. */
  66. public class ExecTask extends Task {
  67. private String os;
  68. private File out;
  69. private File dir;
  70. private boolean failOnError = false;
  71. private Integer timeout = null;
  72. private Environment env = new Environment();
  73. private Commandline cmdl = new Commandline();
  74. private FileOutputStream fos = null;
  75. public void setTimeout(Integer value) {
  76. timeout = value;
  77. }
  78. public void setExecutable(String value) {
  79. cmdl.setExecutable(value);
  80. }
  81. public void setDir(File d) {
  82. this.dir = d;
  83. }
  84. public void setOs(String os) {
  85. this.os = os;
  86. }
  87. public void setCommand(Commandline cmdl) {
  88. this.cmdl = cmdl;
  89. }
  90. public void setOutput(File out) {
  91. this.out = out;
  92. }
  93. public void setFailonerror(boolean fail) {
  94. failOnError = fail;
  95. }
  96. public void addEnv(Environment.Variable var) {
  97. env.addVariable(var);
  98. }
  99. public Commandline.Argument createArg() {
  100. return cmdl.createArgument();
  101. }
  102. public void execute() throws BuildException {
  103. if (cmdl.getExecutable() == null) {
  104. throw new BuildException("no executable specified", location);
  105. }
  106. String[] orig = cmdl.getCommandline();
  107. int err = -1; // assume the worst
  108. // test if os match
  109. String myos = System.getProperty("os.name");
  110. log("Myos = " + myos, Project.MSG_VERBOSE);
  111. if ((os != null) && (os.indexOf(myos) < 0)){
  112. // this command will be executed only on the specified OS
  113. log("Not found in " + os, Project.MSG_VERBOSE);
  114. return;
  115. }
  116. // default directory to the project's base directory
  117. if (dir == null) dir = project.getBaseDir();
  118. if (myos.toLowerCase().indexOf("windows") >= 0) {
  119. if (!dir.equals(project.resolveFile("."))) {
  120. if (myos.toLowerCase().indexOf("nt") >= 0) {
  121. cmdl = new Commandline();
  122. cmdl.setExecutable("cmd");
  123. cmdl.addValue("/c");
  124. cmdl.addValue("cd");
  125. cmdl.addValue(dir.getAbsolutePath());
  126. cmdl.addValue("&&");
  127. cmdl.addLine(orig);
  128. } else {
  129. String ant = project.getProperty("ant.home");
  130. if (ant == null) {
  131. throw new BuildException("Property 'ant.home' not found", location);
  132. }
  133. String antRun = project.resolveFile(ant + "/bin/antRun.bat").toString();
  134. cmdl = new Commandline();
  135. cmdl.setExecutable(antRun);
  136. cmdl.addValue(dir.getAbsolutePath());
  137. cmdl.addLine(orig);
  138. }
  139. }
  140. } else {
  141. String ant = project.getProperty("ant.home");
  142. if (ant == null) throw new BuildException("Property 'ant.home' not found", location);
  143. String antRun = project.resolveFile(ant + "/bin/antRun").toString();
  144. cmdl = new Commandline();
  145. cmdl.setExecutable(antRun);
  146. cmdl.addValue(dir.getAbsolutePath());
  147. cmdl.addLine(orig);
  148. }
  149. try {
  150. // show the command
  151. log(cmdl.toString(), Project.MSG_VERBOSE);
  152. final Execute exe = new Execute(createHandler(), createWatchdog());
  153. exe.setCommandline(cmdl.getCommandline());
  154. exe.setEnvironment(env.getVariables());
  155. err = exe.execute();
  156. if (err != 0) {
  157. if (failOnError) {
  158. throw new BuildException("Exec returned: "+err, location);
  159. } else {
  160. log("Result: " + err, Project.MSG_ERR);
  161. }
  162. }
  163. } catch (IOException e) {
  164. throw new BuildException("Execute failed: " + e, e, location);
  165. } finally {
  166. // close the output file if required
  167. logFlush();
  168. }
  169. }
  170. protected ExecuteStreamHandler createHandler() throws BuildException {
  171. if(out!=null) {
  172. try {
  173. fos = new FileOutputStream(out);
  174. log("Output redirected to " + out, Project.MSG_VERBOSE);
  175. return new PumpStreamHandler(fos);
  176. } catch (FileNotFoundException fne) {
  177. throw new BuildException("Cannot write to "+out, fne, location);
  178. } catch (IOException ioe) {
  179. throw new BuildException("Cannot write to "+out, ioe, location);
  180. }
  181. } else {
  182. return new LogStreamHandler(this,
  183. Project.MSG_INFO, Project.MSG_WARN);
  184. }
  185. }
  186. protected ExecuteWatchdog createWatchdog() throws BuildException {
  187. if (timeout == null) return null;
  188. return new ExecuteWatchdog(timeout.intValue());
  189. }
  190. protected void logFlush() {
  191. try {
  192. if (fos != null) fos.close();
  193. } catch (IOException io) {}
  194. }
  195. }