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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2001 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. public class ExecuteOn extends ExecTask {
  75. protected Vector filesets = new Vector();
  76. private boolean relative = false;
  77. private boolean parallel = false;
  78. protected String type = "file";
  79. protected Commandline.Marker srcFilePos = null;
  80. private boolean skipEmpty = false;
  81. protected Commandline.Marker targetFilePos = null;
  82. protected Mapper mapperElement = null;
  83. protected FileNameMapper mapper = null;
  84. protected File destDir = null;
  85. /**
  86. * Has &lt;srcfile&gt; been specified before &lt;targetfile&gt;
  87. */
  88. protected boolean srcIsFirst = true;
  89. /**
  90. * Adds a set of files (nested fileset attribute).
  91. */
  92. public void addFileset(FileSet set) {
  93. filesets.addElement(set);
  94. }
  95. /**
  96. * Should filenames be returned as relative path names?
  97. */
  98. public void setRelative(boolean relative) {
  99. this.relative = relative;
  100. }
  101. /**
  102. * Shall the command work on all specified files in parallel?
  103. */
  104. public void setParallel(boolean parallel) {
  105. this.parallel = parallel;
  106. }
  107. /**
  108. * Shall the command work only on files, directories or both?
  109. */
  110. public void setType(FileDirBoth type) {
  111. this.type = type.getValue();
  112. }
  113. /**
  114. * Should empty filesets be ignored?
  115. */
  116. public void setSkipEmptyFilesets(boolean skip) {
  117. skipEmpty = skip;
  118. }
  119. /**
  120. * Set the destination directory.
  121. */
  122. public void setDest(File destDir) {
  123. this.destDir = destDir;
  124. }
  125. /**
  126. * Marker that indicates where the name of the source file should
  127. * be put on the command line.
  128. */
  129. public Commandline.Marker createSrcfile() {
  130. if (srcFilePos != null) {
  131. throw new BuildException(taskType + " doesn\'t support multiple srcfile elements.",
  132. location);
  133. }
  134. srcFilePos = cmdl.createMarker();
  135. return srcFilePos;
  136. }
  137. /**
  138. * Marker that indicates where the name of the target file should
  139. * be put on the command line.
  140. */
  141. public Commandline.Marker createTargetfile() {
  142. if (targetFilePos != null) {
  143. throw new BuildException(taskType + " doesn\'t support multiple targetfile elements.",
  144. location);
  145. }
  146. targetFilePos = cmdl.createMarker();
  147. srcIsFirst = (srcFilePos != null);
  148. return targetFilePos;
  149. }
  150. /**
  151. * Defines the FileNameMapper to use (nested mapper element).
  152. */
  153. public Mapper createMapper() throws BuildException {
  154. if (mapperElement != null) {
  155. throw new BuildException("Cannot define more than one mapper",
  156. location);
  157. }
  158. mapperElement = new Mapper(project);
  159. return mapperElement;
  160. }
  161. protected void checkConfiguration() {
  162. if ("execon".equals(taskName)) {
  163. log("!! execon is deprecated. Use apply instead. !!");
  164. }
  165. super.checkConfiguration();
  166. if (filesets.size() == 0) {
  167. throw new BuildException("no filesets specified", location);
  168. }
  169. if (targetFilePos != null || mapperElement != null
  170. || destDir != null) {
  171. if (mapperElement == null) {
  172. throw new BuildException("no mapper specified", location);
  173. }
  174. if (mapperElement == null) {
  175. throw new BuildException("no dest attribute specified",
  176. location);
  177. }
  178. mapper = mapperElement.getImplementation();
  179. }
  180. }
  181. protected void runExec(Execute exe) throws BuildException {
  182. try {
  183. Vector fileNames = new Vector();
  184. Vector baseDirs = new Vector();
  185. for (int i=0; i<filesets.size(); i++) {
  186. FileSet fs = (FileSet) filesets.elementAt(i);
  187. File base = fs.getDir(project);
  188. DirectoryScanner ds = fs.getDirectoryScanner(project);
  189. if (!"dir".equals(type)) {
  190. String[] s = getFiles(base, ds);
  191. for (int j=0; j<s.length; j++) {
  192. fileNames.addElement(s[j]);
  193. baseDirs.addElement(base);
  194. }
  195. }
  196. if (!"file".equals(type)) {
  197. String[] s = getDirs(base, ds);;
  198. for (int j=0; j<s.length; j++) {
  199. fileNames.addElement(s[j]);
  200. baseDirs.addElement(base);
  201. }
  202. }
  203. if (fileNames.size() == 0 && skipEmpty) {
  204. log("Skipping fileset for directory "
  205. + base + ". It is empty.", Project.MSG_INFO);
  206. continue;
  207. }
  208. if (!parallel) {
  209. String[] s = new String[fileNames.size()];
  210. fileNames.copyInto(s);
  211. for (int j=0; j<s.length; j++) {
  212. String[] command = getCommandline(s[j], base);
  213. log("Executing " + Commandline.toString(command),
  214. Project.MSG_VERBOSE);
  215. exe.setCommandline(command);
  216. runExecute(exe);
  217. }
  218. fileNames.removeAllElements();
  219. baseDirs.removeAllElements();
  220. }
  221. }
  222. if (parallel && (fileNames.size() > 0 || !skipEmpty)) {
  223. String[] s = new String[fileNames.size()];
  224. fileNames.copyInto(s);
  225. File[] b = new File[baseDirs.size()];
  226. baseDirs.copyInto(b);
  227. String[] command = getCommandline(s, b);
  228. log("Executing " + Commandline.toString(command),
  229. Project.MSG_VERBOSE);
  230. exe.setCommandline(command);
  231. runExecute(exe);
  232. }
  233. } catch (IOException e) {
  234. throw new BuildException("Execute failed: " + e, e, location);
  235. } finally {
  236. // close the output file if required
  237. logFlush();
  238. }
  239. }
  240. /**
  241. * Construct the command line for parallel execution.
  242. *
  243. * @param srcFiles The filenames to add to the commandline
  244. * @param baseDir filenames are relative to this dir
  245. */
  246. protected String[] getCommandline(String[] srcFiles, File[] baseDirs) {
  247. Vector targets = new Vector();
  248. if (targetFilePos != null) {
  249. Hashtable addedFiles = new Hashtable();
  250. for (int i=0; i<srcFiles.length; i++) {
  251. String[] subTargets = mapper.mapFileName(srcFiles[i]);
  252. if (subTargets != null) {
  253. for (int j=0; j<subTargets.length; j++) {
  254. String name = null;
  255. if (!relative) {
  256. name =
  257. (new File(destDir, subTargets[j])).getAbsolutePath();
  258. } else {
  259. name = subTargets[j];
  260. }
  261. if (!addedFiles.contains(name)) {
  262. targets.addElement(name);
  263. addedFiles.put(name, name);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. String[] targetFiles = new String[targets.size()];
  270. targets.copyInto(targetFiles);
  271. String[] orig = cmdl.getCommandline();
  272. String[] result = new String[orig.length+srcFiles.length+targetFiles.length];
  273. int srcIndex = orig.length;
  274. if (srcFilePos != null) {
  275. srcIndex = srcFilePos.getPosition();
  276. }
  277. if (targetFilePos != null) {
  278. int targetIndex = targetFilePos.getPosition();
  279. if (srcIndex < targetIndex
  280. || (srcIndex == targetIndex && srcIsFirst)) {
  281. // 0 --> srcIndex
  282. System.arraycopy(orig, 0, result, 0, srcIndex);
  283. // srcIndex --> targetIndex
  284. System.arraycopy(orig, srcIndex, result,
  285. srcIndex + srcFiles.length,
  286. targetIndex - srcIndex);
  287. // targets are already absolute file names
  288. System.arraycopy(targetFiles, 0, result,
  289. targetIndex + srcFiles.length,
  290. targetFiles.length);
  291. // targetIndex --> end
  292. System.arraycopy(orig, targetIndex, result,
  293. targetIndex + srcFiles.length + targetFiles.length,
  294. orig.length - targetIndex);
  295. } else {
  296. // 0 --> targetIndex
  297. System.arraycopy(orig, 0, result, 0, targetIndex);
  298. // targets are already absolute file names
  299. System.arraycopy(targetFiles, 0, result,
  300. targetIndex,
  301. targetFiles.length);
  302. // targetIndex --> srcIndex
  303. System.arraycopy(orig, targetIndex, result,
  304. targetIndex + targetFiles.length,
  305. srcIndex - targetIndex);
  306. // srcIndex --> end
  307. System.arraycopy(orig, srcIndex, result,
  308. srcIndex + srcFiles.length + targetFiles.length,
  309. orig.length - srcIndex);
  310. srcIndex += targetFiles.length;
  311. }
  312. } else { // no targetFilePos
  313. // 0 --> srcIndex
  314. System.arraycopy(orig, 0, result, 0, srcIndex);
  315. // srcIndex --> end
  316. System.arraycopy(orig, srcIndex, result,
  317. srcIndex + srcFiles.length,
  318. orig.length - srcIndex);
  319. }
  320. // fill in source file names
  321. for (int i=0; i < srcFiles.length; i++) {
  322. if (!relative) {
  323. result[srcIndex+i] =
  324. (new File(baseDirs[i], srcFiles[i])).getAbsolutePath();
  325. } else {
  326. result[srcIndex+i] = srcFiles[i];
  327. }
  328. }
  329. return result;
  330. }
  331. /**
  332. * Construct the command line for serial execution.
  333. *
  334. * @param srcFile The filename to add to the commandline
  335. * @param baseDir filename is relative to this dir
  336. */
  337. protected String[] getCommandline(String srcFile, File baseDir) {
  338. return getCommandline(new String[] {srcFile}, new File[] {baseDir});
  339. }
  340. /**
  341. * Return the list of files from this DirectoryScanner that should
  342. * be included on the command line.
  343. */
  344. protected String[] getFiles(File baseDir, DirectoryScanner ds) {
  345. if (mapper != null) {
  346. SourceFileScanner sfs = new SourceFileScanner(this);
  347. return sfs.restrict(ds.getIncludedFiles(), baseDir, destDir,
  348. mapper);
  349. } else {
  350. return ds.getIncludedFiles();
  351. }
  352. }
  353. /**
  354. * Return the list of Directories from this DirectoryScanner that
  355. * should be included on the command line.
  356. */
  357. protected String[] getDirs(File baseDir, DirectoryScanner ds) {
  358. if (mapper != null) {
  359. SourceFileScanner sfs = new SourceFileScanner(this);
  360. return sfs.restrict(ds.getIncludedDirectories(), baseDir, destDir,
  361. mapper);
  362. } else {
  363. return ds.getIncludedDirectories();
  364. }
  365. }
  366. /**
  367. * Enumerated attribute with the values "file", "dir" and "both"
  368. * for the type attribute.
  369. */
  370. public static class FileDirBoth extends EnumeratedAttribute {
  371. public String[] getValues() {
  372. return new String[] {"file", "dir", "both"};
  373. }
  374. }
  375. }