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.

Java.java 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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", "Ant", 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.BuildException;
  56. import org.apache.tools.ant.ExitException;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.Task;
  59. import org.apache.tools.ant.types.Commandline;
  60. import org.apache.tools.ant.types.Path;
  61. import org.apache.tools.ant.types.CommandlineJava;
  62. import org.apache.tools.ant.types.Reference;
  63. import org.apache.tools.ant.types.Environment;
  64. import java.io.File;
  65. import java.io.PrintStream;
  66. import java.io.FileOutputStream;
  67. import java.io.IOException;
  68. import java.util.Vector;
  69. /**
  70. * This task acts as a loader for java applications but allows to use the same JVM
  71. * for the called application thus resulting in much faster operation.
  72. *
  73. * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  74. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  75. *
  76. * @ant:task category="java"
  77. */
  78. public class Java extends Task {
  79. private CommandlineJava cmdl = new CommandlineJava();
  80. private Environment env = new Environment();
  81. private boolean fork = false;
  82. private boolean newEnvironment = false;
  83. private File dir = null;
  84. private File out;
  85. private PrintStream outStream = null;
  86. private boolean failOnError = false;
  87. /**
  88. * Do the execution.
  89. */
  90. public void execute() throws BuildException {
  91. int err = -1;
  92. if ((err = executeJava()) != 0) {
  93. if (failOnError) {
  94. throw new BuildException("Java returned: "+err, location);
  95. } else {
  96. log("Java Result: " + err, Project.MSG_ERR);
  97. }
  98. }
  99. }
  100. /**
  101. * Do the execution and return a return code.
  102. *
  103. * @return the return code from the execute java class if it was executed in
  104. * a separate VM (fork = "yes").
  105. */
  106. public int executeJava() throws BuildException {
  107. String classname = cmdl.getClassname();
  108. if (classname == null && cmdl.getJar() == null) {
  109. throw new BuildException("Classname must not be null.");
  110. }
  111. if (!fork && cmdl.getJar() != null){
  112. throw new BuildException("Cannot execute a jar in non-forked mode. Please set fork='true'. ");
  113. }
  114. if (fork) {
  115. log("Forking " + cmdl.toString(), Project.MSG_VERBOSE);
  116. return run(cmdl.getCommandline());
  117. } else {
  118. if (cmdl.getVmCommand().size() > 1) {
  119. log("JVM args ignored when same JVM is used.", Project.MSG_WARN);
  120. }
  121. if (dir != null) {
  122. log("Working directory ignored when same JVM is used.", Project.MSG_WARN);
  123. }
  124. if (newEnvironment || null != env.getVariables()) {
  125. log("Changes to environment variables are ignored when same JVM is used.",
  126. Project.MSG_WARN);
  127. }
  128. log("Running in same VM " + cmdl.getJavaCommand().toString(),
  129. Project.MSG_VERBOSE);
  130. try {
  131. run(cmdl);
  132. return 0;
  133. }
  134. catch (ExitException ex) {
  135. return ex.getStatus();
  136. }
  137. }
  138. }
  139. /**
  140. * Set the classpath to be used for this compilation.
  141. */
  142. public void setClasspath(Path s) {
  143. createClasspath().append(s);
  144. }
  145. /**
  146. * Creates a nested classpath element
  147. */
  148. public Path createClasspath() {
  149. return cmdl.createClasspath(project).createPath();
  150. }
  151. /**
  152. * Adds a reference to a CLASSPATH defined elsewhere.
  153. */
  154. public void setClasspathRef(Reference r) {
  155. createClasspath().setRefid(r);
  156. }
  157. /**
  158. * set the jar name...
  159. */
  160. public void setJar(File jarfile) throws BuildException {
  161. if ( cmdl.getClassname() != null ){
  162. throw new BuildException("Cannot use 'jar' and 'classname' attributes in same command.");
  163. }
  164. cmdl.setJar(jarfile.getAbsolutePath());
  165. }
  166. /**
  167. * Set the class name.
  168. */
  169. public void setClassname(String s) throws BuildException {
  170. if ( cmdl.getJar() != null ){
  171. throw new BuildException("Cannot use 'jar' and 'classname' attributes in same command");
  172. }
  173. cmdl.setClassname(s);
  174. }
  175. /**
  176. * Set the command line arguments for the class.
  177. */
  178. public void setArgs(String s) {
  179. log("The args attribute is deprecated. " +
  180. "Please use nested arg elements.",
  181. Project.MSG_WARN);
  182. cmdl.createArgument().setLine(s);
  183. }
  184. /**
  185. * Creates a nested arg element.
  186. */
  187. public Commandline.Argument createArg() {
  188. return cmdl.createArgument();
  189. }
  190. /**
  191. * Set the forking flag.
  192. */
  193. public void setFork(boolean s) {
  194. this.fork = s;
  195. }
  196. /**
  197. * Set the command line arguments for the JVM.
  198. */
  199. public void setJvmargs(String s) {
  200. log("The jvmargs attribute is deprecated. " +
  201. "Please use nested jvmarg elements.",
  202. Project.MSG_WARN);
  203. cmdl.createVmArgument().setLine(s);
  204. }
  205. /**
  206. * Creates a nested jvmarg element.
  207. */
  208. public Commandline.Argument createJvmarg() {
  209. return cmdl.createVmArgument();
  210. }
  211. /**
  212. * Set the command used to start the VM (only if fork==false).
  213. */
  214. public void setJvm(String s) {
  215. cmdl.setVm(s);
  216. }
  217. /**
  218. * Add a nested sysproperty element.
  219. */
  220. public void addSysproperty(Environment.Variable sysp) {
  221. cmdl.addSysproperty(sysp);
  222. }
  223. /**
  224. * Throw a BuildException if process returns non 0.
  225. */
  226. public void setFailonerror(boolean fail) {
  227. failOnError = fail;
  228. }
  229. /**
  230. * The working directory of the process
  231. */
  232. public void setDir(File d) {
  233. this.dir = d;
  234. }
  235. /**
  236. * File the output of the process is redirected to.
  237. */
  238. public void setOutput(File out) {
  239. this.out = out;
  240. }
  241. /**
  242. * -mx or -Xmx depending on VM version
  243. */
  244. public void setMaxmemory(String max){
  245. cmdl.setMaxmemory(max);
  246. }
  247. public void setJVMVersion(String value) {
  248. cmdl.setVmversion(value);
  249. }
  250. /**
  251. * Add a nested env element - an environment variable.
  252. *
  253. * <p>Will be ignored if we are not forking a new VM.
  254. *
  255. * @since 1.32, Ant 1.5
  256. */
  257. public void addEnv(Environment.Variable var) {
  258. env.addVariable(var);
  259. }
  260. /**
  261. * Use a completely new environment.
  262. *
  263. * <p>Will be ignored if we are not forking a new VM.
  264. *
  265. * @since 1.32, Ant 1.5
  266. */
  267. public void setNewenvironment(boolean newenv) {
  268. newEnvironment = newenv;
  269. }
  270. protected void handleOutput(String line) {
  271. if (outStream != null) {
  272. outStream.println(line);
  273. }
  274. else {
  275. super.handleOutput(line);
  276. }
  277. }
  278. protected void handleErrorOutput(String line) {
  279. if (outStream != null) {
  280. outStream.println(line);
  281. }
  282. else {
  283. super.handleErrorOutput(line);
  284. }
  285. }
  286. /**
  287. * Executes the given classname with the given arguments as it
  288. * was a command line application.
  289. */
  290. private void run(CommandlineJava command) throws BuildException {
  291. ExecuteJava exe = new ExecuteJava();
  292. exe.setJavaCommand(command.getJavaCommand());
  293. exe.setClasspath(command.getClasspath());
  294. exe.setSystemProperties(command.getSystemProperties());
  295. if (out != null) {
  296. try {
  297. outStream = new PrintStream(new FileOutputStream(out));
  298. exe.execute(project);
  299. } catch (IOException io) {
  300. throw new BuildException(io, location);
  301. }
  302. finally {
  303. if (outStream != null) {
  304. outStream.close();
  305. }
  306. }
  307. }
  308. else {
  309. exe.execute(project);
  310. }
  311. }
  312. /**
  313. * Executes the given classname with the given arguments in a separate VM.
  314. */
  315. private int run(String[] command) throws BuildException {
  316. FileOutputStream fos = null;
  317. try {
  318. Execute exe = null;
  319. if (out == null) {
  320. exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
  321. Project.MSG_WARN),
  322. null);
  323. } else {
  324. fos = new FileOutputStream(out);
  325. exe = new Execute(new PumpStreamHandler(fos), null);
  326. }
  327. exe.setAntRun(project);
  328. if (dir == null) {
  329. dir = project.getBaseDir();
  330. } else if (!dir.exists() || !dir.isDirectory()) {
  331. throw new BuildException(dir.getAbsolutePath()+" is not a valid directory",
  332. location);
  333. }
  334. exe.setWorkingDirectory(dir);
  335. String[] environment = env.getVariables();
  336. if (environment != null) {
  337. for (int i=0; i<environment.length; i++) {
  338. log("Setting environment variable: "+environment[i],
  339. Project.MSG_VERBOSE);
  340. }
  341. }
  342. exe.setNewenvironment(newEnvironment);
  343. exe.setEnvironment(environment);
  344. exe.setCommandline(command);
  345. try {
  346. return exe.execute();
  347. } catch (IOException e) {
  348. throw new BuildException(e, location);
  349. }
  350. } catch (IOException io) {
  351. throw new BuildException(io, location);
  352. } finally {
  353. if (fos != null) {
  354. try {fos.close();} catch (IOException io) {}
  355. }
  356. }
  357. }
  358. /**
  359. * Executes the given classname with the given arguments as it
  360. * was a command line application.
  361. */
  362. protected void run(String classname, Vector args) throws BuildException {
  363. CommandlineJava cmdj = new CommandlineJava();
  364. cmdj.setClassname(classname);
  365. for (int i=0; i<args.size(); i++) {
  366. cmdj.createArgument().setValue((String) args.elementAt(i));
  367. }
  368. run(cmdj);
  369. }
  370. /**
  371. * Clear out the arguments to this java task.
  372. */
  373. public void clearArgs() {
  374. cmdl.clearJavaArgs();
  375. }
  376. }