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.

ExecuteOn.java 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.DirectoryScanner;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.types.FileSet;
  59. import org.apache.tools.ant.types.Commandline;
  60. import org.apache.tools.ant.types.Mapper;
  61. import org.apache.tools.ant.types.EnumeratedAttribute;
  62. import org.apache.tools.ant.util.FileNameMapper;
  63. import org.apache.tools.ant.util.SourceFileScanner;
  64. import java.util.Hashtable;
  65. import java.util.Vector;
  66. import java.io.File;
  67. import java.io.IOException;
  68. /**
  69. * Executes a given command, supplying a set of files as arguments.
  70. *
  71. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  72. * @author <a href="mailto:mariusz@rakiura.org">Mariusz Nowostawski</a>
  73. *
  74. * @since Ant 1.2
  75. *
  76. * @ant.task category="control" name="apply"
  77. */
  78. public class ExecuteOn extends ExecTask {
  79. protected Vector filesets = new Vector();
  80. private boolean relative = false;
  81. private boolean parallel = false;
  82. protected String type = "file";
  83. protected Commandline.Marker srcFilePos = null;
  84. private boolean skipEmpty = false;
  85. protected Commandline.Marker targetFilePos = null;
  86. protected Mapper mapperElement = null;
  87. protected FileNameMapper mapper = null;
  88. protected File destDir = null;
  89. /**
  90. * Has &lt;srcfile&gt; been specified before &lt;targetfile&gt;
  91. */
  92. protected boolean srcIsFirst = true;
  93. /**
  94. * Source files to operate upon.
  95. */
  96. public void addFileset(FileSet set) {
  97. filesets.addElement(set);
  98. }
  99. /**
  100. * Whether the filenames should be passed on the command line as
  101. * absolute or relative pathnames. Paths are relative to the base
  102. * directory of the corresponding fileset for source files or the
  103. * dest attribute for target files.
  104. */
  105. public void setRelative(boolean relative) {
  106. this.relative = relative;
  107. }
  108. /**
  109. * If true, run the command only once, appending all files as arguments.
  110. * If false, command will be executed once for every file. Defaults to false.
  111. */
  112. public void setParallel(boolean parallel) {
  113. this.parallel = parallel;
  114. }
  115. /**
  116. * Whether the command works only on files, directories or both?
  117. */
  118. public void setType(FileDirBoth type) {
  119. this.type = type.getValue();
  120. }
  121. /**
  122. * If no source files have been found or are newer than their
  123. * corresponding target files, do not run the command.
  124. */
  125. public void setSkipEmptyFilesets(boolean skip) {
  126. skipEmpty = skip;
  127. }
  128. /**
  129. * The directory where target files are to be placed.
  130. */
  131. public void setDest(File destDir) {
  132. this.destDir = destDir;
  133. }
  134. /**
  135. * Marker that indicates where the name of the source file should
  136. * be put on the command line.
  137. */
  138. public Commandline.Marker createSrcfile() {
  139. if (srcFilePos != null) {
  140. throw new BuildException(getTaskType() + " doesn\'t support multiple "
  141. + "srcfile elements.", getLocation());
  142. }
  143. srcFilePos = cmdl.createMarker();
  144. return srcFilePos;
  145. }
  146. /**
  147. * Marker that indicates where the name of the target file should
  148. * be put on the command line.
  149. */
  150. public Commandline.Marker createTargetfile() {
  151. if (targetFilePos != null) {
  152. throw new BuildException(getTaskType() + " doesn\'t support multiple "
  153. + "targetfile elements.", getLocation());
  154. }
  155. targetFilePos = cmdl.createMarker();
  156. srcIsFirst = (srcFilePos != null);
  157. return targetFilePos;
  158. }
  159. /**
  160. * Mapper to use for mapping source files to target files.
  161. */
  162. public Mapper createMapper() throws BuildException {
  163. if (mapperElement != null) {
  164. throw new BuildException("Cannot define more than one mapper",
  165. getLocation());
  166. }
  167. mapperElement = new Mapper(getProject());
  168. return mapperElement;
  169. }
  170. /**
  171. * @todo using taskName here is brittle, as a user could override it.
  172. * this should probably be modified to use the classname instead.
  173. */
  174. protected void checkConfiguration() {
  175. if ("execon".equals(getTaskName())) {
  176. log("!! execon is deprecated. Use apply instead. !!");
  177. }
  178. super.checkConfiguration();
  179. if (filesets.size() == 0) {
  180. throw new BuildException("no filesets specified", getLocation());
  181. }
  182. if (targetFilePos != null || mapperElement != null
  183. || destDir != null) {
  184. if (mapperElement == null) {
  185. throw new BuildException("no mapper specified", getLocation());
  186. }
  187. if (destDir == null) {
  188. throw new BuildException("no dest attribute specified",
  189. getLocation());
  190. }
  191. mapper = mapperElement.getImplementation();
  192. }
  193. }
  194. protected void runExec(Execute exe) throws BuildException {
  195. try {
  196. Vector fileNames = new Vector();
  197. Vector baseDirs = new Vector();
  198. for (int i = 0; i < filesets.size(); i++) {
  199. FileSet fs = (FileSet) filesets.elementAt(i);
  200. File base = fs.getDir(getProject());
  201. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  202. if (!"dir".equals(type)) {
  203. String[] s = getFiles(base, ds);
  204. for (int j = 0; j < s.length; j++) {
  205. fileNames.addElement(s[j]);
  206. baseDirs.addElement(base);
  207. }
  208. }
  209. if (!"file".equals(type)) {
  210. String[] s = getDirs(base, ds);;
  211. for (int j = 0; j < s.length; j++) {
  212. fileNames.addElement(s[j]);
  213. baseDirs.addElement(base);
  214. }
  215. }
  216. if (fileNames.size() == 0 && skipEmpty) {
  217. log("Skipping fileset for directory "
  218. + base + ". It is empty.", Project.MSG_INFO);
  219. continue;
  220. }
  221. if (!parallel) {
  222. String[] s = new String[fileNames.size()];
  223. fileNames.copyInto(s);
  224. for (int j = 0; j < s.length; j++) {
  225. String[] command = getCommandline(s[j], base);
  226. log(Commandline.describeCommand(command),
  227. Project.MSG_VERBOSE);
  228. exe.setCommandline(command);
  229. runExecute(exe);
  230. }
  231. fileNames.removeAllElements();
  232. baseDirs.removeAllElements();
  233. }
  234. }
  235. if (parallel && (fileNames.size() > 0 || !skipEmpty)) {
  236. String[] s = new String[fileNames.size()];
  237. fileNames.copyInto(s);
  238. File[] b = new File[baseDirs.size()];
  239. baseDirs.copyInto(b);
  240. String[] command = getCommandline(s, b);
  241. log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
  242. exe.setCommandline(command);
  243. runExecute(exe);
  244. }
  245. } catch (IOException e) {
  246. throw new BuildException("Execute failed: " + e, e, getLocation());
  247. } finally {
  248. // close the output file if required
  249. logFlush();
  250. }
  251. }
  252. /**
  253. * Construct the command line for parallel execution.
  254. *
  255. * @param srcFiles The filenames to add to the commandline
  256. * @param baseDir filenames are relative to this dir
  257. */
  258. protected String[] getCommandline(String[] srcFiles, File[] baseDirs) {
  259. Vector targets = new Vector();
  260. if (targetFilePos != null) {
  261. Hashtable addedFiles = new Hashtable();
  262. for (int i = 0; i < srcFiles.length; i++) {
  263. String[] subTargets = mapper.mapFileName(srcFiles[i]);
  264. if (subTargets != null) {
  265. for (int j = 0; j < subTargets.length; j++) {
  266. String name = null;
  267. if (!relative) {
  268. name = (new File(destDir, subTargets[j])).getAbsolutePath();
  269. } else {
  270. name = subTargets[j];
  271. }
  272. if (!addedFiles.contains(name)) {
  273. targets.addElement(name);
  274. addedFiles.put(name, name);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. String[] targetFiles = new String[targets.size()];
  281. targets.copyInto(targetFiles);
  282. String[] orig = cmdl.getCommandline();
  283. String[] result
  284. = new String[orig.length + srcFiles.length + targetFiles.length];
  285. int srcIndex = orig.length;
  286. if (srcFilePos != null) {
  287. srcIndex = srcFilePos.getPosition();
  288. }
  289. if (targetFilePos != null) {
  290. int targetIndex = targetFilePos.getPosition();
  291. if (srcIndex < targetIndex
  292. || (srcIndex == targetIndex && srcIsFirst)) {
  293. // 0 --> srcIndex
  294. System.arraycopy(orig, 0, result, 0, srcIndex);
  295. // srcIndex --> targetIndex
  296. System.arraycopy(orig, srcIndex, result,
  297. srcIndex + srcFiles.length,
  298. targetIndex - srcIndex);
  299. // targets are already absolute file names
  300. System.arraycopy(targetFiles, 0, result,
  301. targetIndex + srcFiles.length,
  302. targetFiles.length);
  303. // targetIndex --> end
  304. System.arraycopy(orig, targetIndex, result,
  305. targetIndex + srcFiles.length + targetFiles.length,
  306. orig.length - targetIndex);
  307. } else {
  308. // 0 --> targetIndex
  309. System.arraycopy(orig, 0, result, 0, targetIndex);
  310. // targets are already absolute file names
  311. System.arraycopy(targetFiles, 0, result,
  312. targetIndex,
  313. targetFiles.length);
  314. // targetIndex --> srcIndex
  315. System.arraycopy(orig, targetIndex, result,
  316. targetIndex + targetFiles.length,
  317. srcIndex - targetIndex);
  318. // srcIndex --> end
  319. System.arraycopy(orig, srcIndex, result,
  320. srcIndex + srcFiles.length + targetFiles.length,
  321. orig.length - srcIndex);
  322. srcIndex += targetFiles.length;
  323. }
  324. } else { // no targetFilePos
  325. // 0 --> srcIndex
  326. System.arraycopy(orig, 0, result, 0, srcIndex);
  327. // srcIndex --> end
  328. System.arraycopy(orig, srcIndex, result,
  329. srcIndex + srcFiles.length,
  330. orig.length - srcIndex);
  331. }
  332. // fill in source file names
  333. for (int i = 0; i < srcFiles.length; i++) {
  334. if (!relative) {
  335. result[srcIndex + i] =
  336. (new File(baseDirs[i], srcFiles[i])).getAbsolutePath();
  337. } else {
  338. result[srcIndex + i] = srcFiles[i];
  339. }
  340. }
  341. return result;
  342. }
  343. /**
  344. * Construct the command line for serial execution.
  345. *
  346. * @param srcFile The filename to add to the commandline
  347. * @param baseDir filename is relative to this dir
  348. */
  349. protected String[] getCommandline(String srcFile, File baseDir) {
  350. return getCommandline(new String[] {srcFile}, new File[] {baseDir});
  351. }
  352. /**
  353. * Return the list of files from this DirectoryScanner that should
  354. * be included on the command line.
  355. */
  356. protected String[] getFiles(File baseDir, DirectoryScanner ds) {
  357. if (mapper != null) {
  358. SourceFileScanner sfs = new SourceFileScanner(this);
  359. return sfs.restrict(ds.getIncludedFiles(), baseDir, destDir,
  360. mapper);
  361. } else {
  362. return ds.getIncludedFiles();
  363. }
  364. }
  365. /**
  366. * Return the list of Directories from this DirectoryScanner that
  367. * should be included on the command line.
  368. */
  369. protected String[] getDirs(File baseDir, DirectoryScanner ds) {
  370. if (mapper != null) {
  371. SourceFileScanner sfs = new SourceFileScanner(this);
  372. return sfs.restrict(ds.getIncludedDirectories(), baseDir, destDir,
  373. mapper);
  374. } else {
  375. return ds.getIncludedDirectories();
  376. }
  377. }
  378. /**
  379. * Enumerated attribute with the values "file", "dir" and "both"
  380. * for the type attribute.
  381. */
  382. public static class FileDirBoth extends EnumeratedAttribute {
  383. /**
  384. * @see EnumeratedAttribute#getValues
  385. */
  386. public String[] getValues() {
  387. return new String[] {"file", "dir", "both"};
  388. }
  389. }
  390. }