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.

mapper.html 29 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Mapper Type</title>
  20. </head>
  21. <body>
  22. <h2 id="mapper">Mapping File Names</h2>
  23. <p>Some tasks take source files and create target files. Depending on the task, it may be quite
  24. obvious which name a target file will have (using <a href="../Tasks/javac.html">javac</a>, you know
  25. there will be <samp>.class</samp> files for your <samp>.java</samp> files)&mdash;in other cases you
  26. may want to specify the target files, either to help Apache Ant or to get an extra bit of
  27. functionality.</p>
  28. <p>While source files are usually specified as <a href="fileset.html">fileset</a>s, you don't
  29. specify target files directly&mdash;instead, you tell Ant how to find the target file(s) for one
  30. source file. An instance of <code>org.apache.tools.ant.util.FileNameMapper</code> is responsible for
  31. this. It constructs target file names based on rules that can be parameterized with <var>from</var>
  32. and <var>to</var> attributes&mdash;the exact meaning of which is implementation-dependent.</p>
  33. <p>These instances are defined in <code>&lt;mapper&gt;</code> elements with the following
  34. attributes:</p>
  35. <table class="attr">
  36. <tr>
  37. <th>Attribute</th>
  38. <th>Description</th>
  39. <th>Required</th>
  40. </tr>
  41. <tr>
  42. <td>type</td>
  43. <td>specifies one of the built-in implementations.</td>
  44. <td rowspan="2">Exactly one of these</td>
  45. </tr>
  46. <tr>
  47. <td>classname</td>
  48. <td class="left">specifies the implementation by class name.</td>
  49. </tr>
  50. <tr>
  51. <td>classpath</td>
  52. <td>the classpath to use when looking up
  53. <code>classname</code>.</td>
  54. <td>No</td>
  55. </tr>
  56. <tr>
  57. <td>classpathref</td>
  58. <td>the classpath to use, given as <a href="../using.html#references">reference</a> to a
  59. path defined elsewhere.</td>
  60. <td>No</td>
  61. </tr>
  62. <tr>
  63. <td>from</td>
  64. <td>the <code>from</code> attribute for the given implementation.</td>
  65. <td>Depends on implementation</td>
  66. </tr>
  67. <tr>
  68. <td>to</td>
  69. <td>the <code>to</code> attribute for the given implementation.</td>
  70. <td>Depends on implementation</td>
  71. </tr>
  72. </table>
  73. <p>Note that Ant will not automatically convert <q>/</q> or <q>\</q> characters in the <var>to</var>
  74. and <var>from</var> attributes to the correct directory separator of your current platform. If you
  75. need to specify this separator, use <code>${file.separator}</code> instead. For the regexp
  76. mapper, <code>${file.separator}</code> will not work, as on Windows it is the <q>\</q> character,
  77. and this is an escape character for regular expressions, one should use
  78. the <code>handledirsep</code> attribute instead.
  79. </p>
  80. <h3>Parameters specified as nested elements</h3>
  81. <p>The classpath can be specified via a nested <code>&lt;classpath&gt;</code>, as well&mdash;that
  82. is, a <a href="../using.html#path">path</a>-like structure.</p>
  83. <p><em>Since Ant 1.7.0</em>, nested File Mappers can be supplied via
  84. either <code>&lt;mapper&gt;</code> elements
  85. or <a href="../Tasks/typedef.html"><code>&lt;typedef&gt;</code></a>'d implementations
  86. of <code>org.apache.tools.ant.util.FileNameMapper</code>. If nested File Mappers are specified by
  87. either means, the mapper will be implicitly configured as a <a href="#composite-mapper">composite
  88. mapper</a>.</p>
  89. <h3>The built-in mapper types</h3>
  90. <p>All built-in mappers are case-sensitive.</p>
  91. <p><em>Since Ant 1.7.0</em>, each of the built-in mapper implementation types is directly accessible
  92. using a specific tagname. This makes it possible for filename mappers to support attributes in
  93. addition to the generally available <var>to</var> and <var>from</var>.<br/> The <code>&lt;mapper
  94. type|classname=&quot;...&quot;&gt;</code> usage form remains valid for reasons of backward
  95. compatibility.</p>
  96. <!-- -->
  97. <!-- Identity Mapper -->
  98. <!-- -->
  99. <h4 id="identity-mapper">identity</h4>
  100. <p>The target file name is identical to the source file name. Both <var>to</var> and <var>from</var>
  101. will be ignored.</p>
  102. <h5>Examples</h5>
  103. <pre>
  104. &lt;mapper type=&quot;identity&quot;/&gt;
  105. &lt;identitymapper/&gt;</pre>
  106. <table>
  107. <tr>
  108. <th>Source file name</th>
  109. <th>Target file name</th>
  110. </tr>
  111. <tr>
  112. <td><code>A.java</code></td>
  113. <td><code>A.java</code></td>
  114. </tr>
  115. <tr>
  116. <td><code>foo/bar/B.java</code></td>
  117. <td><code>foo/bar/B.java</code></td>
  118. </tr>
  119. <tr>
  120. <td><code>C.properties</code></td>
  121. <td><code>C.properties</code></td>
  122. </tr>
  123. <tr>
  124. <td><code>Classes/dir/dir2/A.properties</code></td>
  125. <td><code>Classes/dir/dir2/A.properties</code></td>
  126. </tr>
  127. </table>
  128. <!-- -->
  129. <!-- Flatten Mapper -->
  130. <!-- -->
  131. <h4 id="flatten-mapper">flatten</h4>
  132. <p>The target file name is identical to the source file name, with all leading directory information
  133. stripped off. Both <var>to</var> and <var>from</var> will be ignored.</p>
  134. <h5>Examples</h5>
  135. <pre>
  136. &lt;mapper type=&quot;flatten&quot;/&gt;
  137. &lt;flattenmapper/&gt;</pre>
  138. <table>
  139. <tr>
  140. <th>Source file name</th>
  141. <th>Target file name</th>
  142. </tr>
  143. <tr>
  144. <td><code>A.java</code></td>
  145. <td><code>A.java</code></td>
  146. </tr>
  147. <tr>
  148. <td><code>foo/bar/B.java</code></td>
  149. <td><code>B.java</code></td>
  150. </tr>
  151. <tr>
  152. <td><code>C.properties</code></td>
  153. <td><code>C.properties</code></td>
  154. </tr>
  155. <tr>
  156. <td><code>Classes/dir/dir2/A.properties</code></td>
  157. <td><code>A.properties</code></td>
  158. </tr>
  159. </table>
  160. <!-- -->
  161. <!-- Merge Mapper -->
  162. <!-- -->
  163. <h4 id="merge-mapper">merge</h4>
  164. <p>The target file name will always be the same, as defined by <var>to</var>&mdash;<var>from</var>
  165. will be ignored.</p>
  166. <h5>Examples</h5>
  167. <pre>
  168. &lt;mapper type=&quot;merge&quot; to=&quot;archive.tar&quot;/&gt;
  169. &lt;mergemapper to=&quot;archive.tar&quot;/&gt;</pre>
  170. <table>
  171. <tr>
  172. <th>Source file name</th>
  173. <th>Target file name</th>
  174. </tr>
  175. <tr>
  176. <td><code>A.java</code></td>
  177. <td><code>archive.tar</code></td>
  178. </tr>
  179. <tr>
  180. <td><code>foo/bar/B.java</code></td>
  181. <td><code>archive.tar</code></td>
  182. </tr>
  183. <tr>
  184. <td><code>C.properties</code></td>
  185. <td><code>archive.tar</code></td>
  186. </tr>
  187. <tr>
  188. <td><code>Classes/dir/dir2/A.properties</code></td>
  189. <td><code>archive.tar</code></td>
  190. </tr>
  191. </table>
  192. <!-- -->
  193. <!-- Glob Mapper -->
  194. <!-- -->
  195. <h4 id="glob-mapper">glob</h4>
  196. <p>Both <var>to</var> and <var>from</var> are required and define patterns that may contain at most
  197. one <q>*</q>. For each source file that matches the <var>from</var> pattern, a target file name will
  198. be constructed from the <var>to</var> pattern by substituting the <q>*</q> in the <var>to</var>
  199. pattern with the text that matches the <q>*</q> in the <var>from</var> pattern. Source file names
  200. that don't match the <var>from</var> pattern will be ignored.</p>
  201. <h5>Examples</h5>
  202. <pre>
  203. &lt;mapper type=&quot;glob&quot; from=&quot;*.java&quot; to=&quot;*.java.bak&quot;/&gt;
  204. &lt;globmapper from=&quot;*.java&quot; to=&quot;*.java.bak&quot;/&gt;</pre>
  205. <table>
  206. <tr>
  207. <th>Source file name</th>
  208. <th>Target file name</th>
  209. </tr>
  210. <tr>
  211. <td><code>A.java</code></td>
  212. <td><code>A.java.bak</code></td>
  213. </tr>
  214. <tr>
  215. <td><code>foo/bar/B.java</code></td>
  216. <td><code>foo/bar/B.java.bak</code></td>
  217. </tr>
  218. <tr>
  219. <td><code>C.properties</code></td>
  220. <td>ignored</td>
  221. </tr>
  222. <tr>
  223. <td><code>Classes/dir/dir2/A.properties</code></td>
  224. <td>ignored</td>
  225. </tr>
  226. </table>
  227. <pre>
  228. &lt;mapper type=&quot;glob&quot; from=&quot;C*ies&quot; to=&quot;Q*y&quot;/&gt;
  229. &lt;globmapper from=&quot;C*ies&quot; to=&quot;Q*y&quot;/&gt;</pre>
  230. <table>
  231. <tr>
  232. <th>Source file name</th>
  233. <th>Target file name</th>
  234. </tr>
  235. <tr>
  236. <td><code>A.java</code></td>
  237. <td>ignored</td>
  238. </tr>
  239. <tr>
  240. <td><code>foo/bar/B.java</code></td>
  241. <td>ignored</td>
  242. </tr>
  243. <tr>
  244. <td><code>C.properties</code></td>
  245. <td><code>Q.property</code></td>
  246. </tr>
  247. <tr>
  248. <td><code>Classes/dir/dir2/A.properties</code></td>
  249. <td><code>Qlasses/dir/dir2/A.property</code></td>
  250. </tr>
  251. </table>
  252. <p>The <code>globmapper</code> mapper can take the following extra attributes.</p>
  253. <table class="attr">
  254. <tr>
  255. <th>Attribute</th>
  256. <th>Description</th>
  257. <th>Required</th>
  258. </tr>
  259. <tr>
  260. <td>casesensitive</td>
  261. <td>
  262. This attribute can be <q>true</q> or <q>false</q>. If this is <q>false</q>, the mapper will
  263. ignore case when matching the glob pattern. <em>Since Ant 1.6.3</em>
  264. </td>
  265. <td>No; default is <q>true</q></td>
  266. </tr>
  267. <tr>
  268. <td>handledirsep</td>
  269. <td>
  270. This attribute can be <q>true</q> or <q>false</q>. If this is specified, the mapper will
  271. ignore the difference between the normal directory separator characters&mdash;<q>\</q>
  272. and <q>/</q>. This attribute is useful for cross-platform build files. <em>Since Ant
  273. 1.6.3</em>
  274. <td>No; default is <q>false</q></td>
  275. </tr>
  276. </table>
  277. <p>An example:</p>
  278. <pre>
  279. &lt;pathconvert property="x" targetos="unix"&gt;
  280. &lt;path path="Aj.Java"/&gt;
  281. &lt;mapper&gt;
  282. &lt;chainedmapper&gt;
  283. &lt;flattenmapper/&gt;
  284. &lt;globmapper from="a*.java" to="*.java.bak" casesensitive="no"/&gt;
  285. &lt;/chainedmapper&gt;
  286. &lt;/mapper&gt;
  287. &lt;/pathconvert&gt;
  288. &lt;echo&gt;x is ${x}&lt;/echo&gt;</pre>
  289. <p>will output <code>x is j.java.bak</code>, and</p>
  290. <pre>
  291. &lt;pathconvert property="x" targetos="unix"&gt;
  292. &lt;path path="d/e/f/j.java"/&gt;
  293. &lt;mapper&gt;
  294. &lt;globmapper from="${basedir}\d/e\*" to="*" handledirsep="yes"/&gt;
  295. &lt;/mapper&gt;
  296. &lt;/pathconvert&gt;
  297. &lt;echo&gt;x is ${x}&lt;/echo&gt;</pre>
  298. <p>will output <code>x is f/j.java</code>.</p>
  299. <!-- -->
  300. <!-- RegExp Mapper -->
  301. <!-- -->
  302. <h4 id="regexp-mapper">regexp</h4>
  303. <p>Both <var>to</var> and <var>from</var> are required and define regular expressions. If the source
  304. file name (as a whole or in part) matches the <var>from</var> pattern, the target file name will be
  305. constructed from the <var>to</var> pattern, using <code>\0</code> to <code>\9</code> as
  306. back-references for the full match (<code>\0</code>) or the matches of the subexpressions in
  307. parentheses. The <var>to</var> pattern determines the <strong>whole</strong> file name, so if you
  308. wanted to replace the extension of a file you should not use <code>from="\.old$" to=".new"</code>
  309. but rather <code>from="(.*)\.old$" to="\1.new"</code> (or rather use a glob mapper in this
  310. case).</p>
  311. <p>Source files not matching the <var>from</var> pattern will be ignored.</p>
  312. <p>Note that you need to escape a dollar-sign (<q>$</q>) with another dollar-sign in Ant.</p>
  313. <p>The regexp mapper needs a supporting library and an implementation
  314. of <code>org.apache.tools.ant.util.regexp.RegexpMatcher</code> that hides the specifics of the
  315. library. <em>Since Ant 1.8.0</em>, Java 1.4 or later is required, so the implementation based on
  316. the <code>java.util.regex</code> package is always be available. You can still use the now retired
  317. Jakarta ORO or Jakarta Regex instead if your provide the corresponding jar in
  318. your <code>CLASSPATH</code>.</p>
  319. <p>For information about using <a href="https://savannah.gnu.org/projects/gnu-regexp/"
  320. target="_top">gnu.regexp</a> or <a href="https://github.com/kzn/regex4j/" target="_top">gnu.rex</a>
  321. with Ant, see <a href="https://marc.info/?l=ant-dev&m=97550753813481&w=2" target="_top">this</a>
  322. article. Please keep in mind that <a href="http://tusker.org/regex/regex_benchmark.html"
  323. target="_top">your mileage may vary</a> with different regexp engines.</p>
  324. <p>If you want to use one of the <a href="../install.html#librarydependencies">regular expression
  325. libraries</a> other than <code>java.util.regex</code> you need to also use the
  326. corresponding <code>ant-[apache-oro, apache-regexp].jar</code> from the Ant release you are using.
  327. Make sure that both will be loaded from the same classpath, that is either put them into
  328. your <code>CLASSPATH</code>, <samp>ANT_HOME/lib</samp> directory or a
  329. nested <code>&lt;classpath&gt;</code> element of the mapper&mdash;you cannot
  330. have <code>ant-[apache-oro, apache-regexp].jar</code> in <samp>ANT_HOME/lib</samp> and the library
  331. in a nested <code>&lt;classpath&gt;</code>.</p>
  332. <p>Ant will choose the regular expression library based on the following algorithm:</p>
  333. <ul>
  334. <li>If the system property <code>ant.regexp.matcherimpl</code> has been set, it is taken as the name
  335. of the class implementing <code>org.apache.tools.ant.util.regexp.RegexpMatcher</code> that should be
  336. used.</li>
  337. <li>If it has not been set, uses the JDK 1.4 classes.</li>
  338. </ul>
  339. <h5>Examples</h5>
  340. <pre>
  341. &lt;mapper type=&quot;regexp&quot; from=&quot;^(.*)\.java$$&quot; to=&quot;\1.java.bak&quot;/&gt;
  342. &lt;regexpmapper from=&quot;^(.*)\.java$$&quot; to=&quot;\1.java.bak&quot;/&gt;</pre>
  343. <table>
  344. <tr>
  345. <th>Source file name</th>
  346. <th>Target file name</th>
  347. </tr>
  348. <tr>
  349. <td><code>A.java</code></td>
  350. <td><code>A.java.bak</code></td>
  351. </tr>
  352. <tr>
  353. <td><code>foo/bar/B.java</code></td>
  354. <td><code>foo/bar/B.java.bak</code></td>
  355. </tr>
  356. <tr>
  357. <td><code>C.properties</code></td>
  358. <td>ignored</td>
  359. </tr>
  360. <tr>
  361. <td><code>Classes/dir/dir2/A.properties</code></td>
  362. <td>ignored</td>
  363. </tr>
  364. </table>
  365. <pre>
  366. &lt;mapper type=&quot;regexp&quot; from=&quot;^(.*)/([^/]+)/([^/]*)$$&quot; to=&quot;\1/\2/\2-\3&quot;/&gt;
  367. &lt;regexpmapper from=&quot;^(.*)/([^/]+)/([^/]*)$$&quot; to=&quot;\1/\2/\2-\3&quot;/&gt;</pre>
  368. <table>
  369. <tr>
  370. <th>Source file name</th>
  371. <th>Target file name</th>
  372. </tr>
  373. <tr>
  374. <td><code>A.java</code></td>
  375. <td>ignored</td>
  376. </tr>
  377. <tr>
  378. <td><code>foo/bar/B.java</code></td>
  379. <td><code>foo/bar/bar-B.java</code></td>
  380. </tr>
  381. <tr>
  382. <td><code>C.properties</code></td>
  383. <td>ignored</td>
  384. </tr>
  385. <tr>
  386. <td><code>Classes/dir/dir2/A.properties</code></td>
  387. <td><code>Classes/dir/dir2/dir2-A.properties</code></td>
  388. </tr>
  389. </table>
  390. <pre>
  391. &lt;mapper type="regexp" from="^(.*)\.(.*)$$" to="\2.\1"/&gt;
  392. &lt;regexpmapper from="^(.*)\.(.*)$$" to="\2.\1"/&gt;</pre>
  393. <table>
  394. <tr>
  395. <th>Source file name</th>
  396. <th>Target file name</th>
  397. </tr>
  398. <tr>
  399. <td><code>A.java</code></td>
  400. <td><code>java.A</code></td>
  401. </tr>
  402. <tr>
  403. <td><code>foo/bar/B.java</code></td>
  404. <td><code>java.foo/bar/B</code></td>
  405. </tr>
  406. <tr>
  407. <td><code>C.properties</code></td>
  408. <td><code>properties.C</code></td>
  409. </tr>
  410. <tr>
  411. <td><code>Classes/dir/dir2/A.properties</code></td>
  412. <td><code>properties.Classes/dir/dir2/A</code></td>
  413. </tr>
  414. </table>
  415. <pre>
  416. &lt;mapper type="regexp" from="^(.*?)(\$$[^/\\\.]*)?\.class$$" to="\1.java"/&gt;
  417. &lt;regexpmapper from="^(.*?)(\$$[^/\\\.]*)?\.class$$" to="\1.java"/&gt;</pre>
  418. <table>
  419. <tr>
  420. <th>Source file name</th>
  421. <th>Target file name</th>
  422. </tr>
  423. <tr>
  424. <td><code>ClassLoader.class</code></td>
  425. <td><code>ClassLoader.java</code></td>
  426. </tr>
  427. <tr>
  428. <td><code>java/lang/ClassLoader.class</code></td>
  429. <td><code>java/lang/ClassLoader.java</code></td>
  430. </tr>
  431. <tr>
  432. <td><code>java\lang\ClassLoader$1.class</code></td>
  433. <td><code>java\lang\ClassLoader.java</code></td>
  434. </tr>
  435. <tr>
  436. <td><code>java/lang/ClassLoader$foo$1.class</code></td>
  437. <td><code>java/lang/ClassLoader.java</code></td>
  438. </tr>
  439. </table>
  440. <p>The regexp mapper can take the following extra attributes.</p>
  441. <table class="attr">
  442. <tr>
  443. <th>Attribute</th>
  444. <th>Description</th>
  445. <th>Required</th>
  446. </tr>
  447. <tr>
  448. <td>casesensitive</td>
  449. <td>
  450. This attribute can be <q>true</q> or <q>false</q>. If this is <q>false</q>, the mapper will
  451. ignore case when matching the pattern.
  452. <em>Since Ant 1.6.3</em>
  453. </td>
  454. <td>No; default is <q>true</q></td>
  455. </tr>
  456. <tr>
  457. <td>handledirsep</td>
  458. <td>
  459. This attribute can be <q>true</q> or <q>false</q>. If this is specified, the mapper will
  460. treat a <q>\</q> character in a filename as a <q>/</q> for the purposes of matching. This
  461. attribute is useful for cross-platform build files.
  462. <em>Since Ant 1.6.3</em>
  463. <td>No; default is <q>false</q></td>
  464. </tr>
  465. </table>
  466. <p>An example:</p>
  467. <pre>
  468. &lt;pathconvert property="x" targetos="unix"&gt;
  469. &lt;path path="Aj.Java"/&gt;
  470. &lt;chainedmapper&gt;
  471. &lt;flattenmapper/&gt;
  472. &lt;regexpmapper from="a(.*)\.java" to="\1.java.bak" casesensitive="no"/&gt;
  473. &lt;/chainedmapper&gt;
  474. &lt;/pathconvert&gt;
  475. &lt;echo&gt;x is ${x}&lt;/echo&gt;</pre>
  476. <p>will output <code>x is j.java.bak</code>, and</p>
  477. <pre>
  478. &lt;pathconvert property="hd.prop" targetos="windows"&gt;
  479. &lt;path path="d\e/f\j.java"/&gt;
  480. &lt;chainedmapper&gt;
  481. &lt;regexpmapper from="${basedir}/d/e/(.*)" to="\1" handledirsep="yes"/&gt;
  482. &lt;/chainedmapper&gt;
  483. &lt;/pathconvert&gt;</pre>
  484. <p>will set <code>hd.prop</code> to <code>f\j.java</code>.</p>
  485. <!-- -->
  486. <!-- Package Mapper -->
  487. <!-- -->
  488. <h4 id="package-mapper">package</h4>
  489. <p>Sharing the same syntax as the <a href="#glob-mapper">glob mapper</a>, the package mapper
  490. replaces directory separators found in the matched source pattern with dots in the target pattern
  491. placeholder. This mapper is particularly useful in combination with <code>&lt;uptodate&gt;</code>
  492. and <code>&lt;junit&gt;</code> output.</p>
  493. <p>The <var>to</var> and <var>from</var> attributes are both required.</p>
  494. <h5>Example</h5>
  495. <pre>
  496. &lt;mapper type="package" from="*Test.java" to="TEST-*Test.xml"/&gt;
  497. &lt;packagemapper from="*Test.java" to="TEST-*Test.xml"/&gt;</pre>
  498. <table>
  499. <tr>
  500. <th>Source file name</th>
  501. <th>Target file name</th>
  502. </tr>
  503. <tr>
  504. <td><code>org/apache/tools/ant/util/PackageMapperTest.java</code></td>
  505. <td><code>TEST-org.apache.tools.ant.util.PackageMapperTest.xml</code></td>
  506. </tr>
  507. <tr>
  508. <td><code>org/apache/tools/ant/util/Helper.java</code></td>
  509. <td>ignored</td>
  510. </tr>
  511. </table>
  512. <!-- -->
  513. <!-- Unpackage Mapper -->
  514. <!-- -->
  515. <h4 id="unpackage-mapper">unpackage</h4>
  516. <p><em>Since Ant 1.6.0</em></p>
  517. <p>This mapper is the inverse of the <a href="#package-mapper">package</a> mapper. It replaces the
  518. dots in a package name with directory separators. This is useful for matching XML formatter results
  519. against their JUnit test test cases. The mapper shares the sample syntax as
  520. the <a href="#glob-mapper">glob mapper</a>.</p>
  521. <p>The <var>to</var> and <var>from</var> attributes are both required.</p>
  522. <h5>Example</h5>
  523. <pre>
  524. &lt;mapper type="unpackage" from="TEST-*Test.xml" to="${test.src.dir}/*Test.java"&gt;
  525. &lt;unpackagemapper from="TEST-*Test.xml" to="${test.src.dir}/*Test.java"&gt;</pre>
  526. <table>
  527. <tr>
  528. <th>Source file name</th>
  529. <th>Target file name</th>
  530. </tr>
  531. <tr>
  532. <td><code>TEST-org.acme.AcmeTest.xml</code></td>
  533. <td><code>${test.src.dir}/org/acme/AcmeTest.java</code></td>
  534. </tr>
  535. </table>
  536. <!-- -->
  537. <!-- Composite Mapper -->
  538. <!-- -->
  539. <h4 id="composite-mapper">composite</h4>
  540. <p><em>Since Ant 1.7.0</em></p>
  541. <p>This mapper implementation can contain multiple nested mappers. File mapping is performed by
  542. passing the source filename to each nested <code>&lt;mapper&gt;</code> in turn, returning all
  543. results. The <var>to</var> and <var>from</var> attributes are ignored.</p>
  544. <p><em>Since Ant 1.8.0</em>, the order of the mapped results is the same as the order of the nested
  545. mappers; prior to Ant 1.8.0 the order has been undefined.</p>
  546. <h5>Examples</h5>
  547. <pre>
  548. &lt;compositemapper&gt;
  549. &lt;identitymapper/&gt;
  550. &lt;packagemapper from="*.java" to="*"/&gt;
  551. &lt;/compositemapper&gt;</pre>
  552. <table>
  553. <tr>
  554. <th>Source file name</th>
  555. <th>Target file names</th>
  556. </tr>
  557. <tr>
  558. <td rowspan="2"><code>foo/bar/A.java</code></td>
  559. <td><code>foo/bar/A.java</code></td>
  560. </tr>
  561. <tr>
  562. <td><code>foo.bar.A</code></td>
  563. </tr>
  564. </table>
  565. <p>The composite mapper has no corresponding <code>&lt;mapper <var>type</var>&gt;</code>
  566. attribute.</p>
  567. <!-- -->
  568. <!-- Chained Mapper -->
  569. <!-- -->
  570. <h4 id="chained-mapper">chained</h4>
  571. <p><em>Since Ant 1.7.0</em></p>
  572. <p>This mapper implementation can contain multiple nested mappers. File mapping is performed by
  573. passing the source filename to the first nested mapper, its results to the second, and so on. The
  574. target filenames generated by the last nested mapper comprise the ultimate results of the mapping
  575. operation. The <var>to</var> and <var>from</var> attributes are ignored.</p>
  576. <h5>Examples</h5>
  577. <pre>
  578. &lt;chainedmapper&gt;
  579. &lt;flattenmapper/&gt;
  580. &lt;globmapper from="*" to="new/path/*"/&gt;
  581. &lt;mapper&gt;
  582. &lt;globmapper from="*" to="*1"/&gt;
  583. &lt;globmapper from="*" to="*2"/&gt;
  584. &lt;/mapper&gt;
  585. &lt;/chainedmapper&gt;</pre>
  586. <table>
  587. <tr>
  588. <th>Source file name</th>
  589. <th>Target file names</th>
  590. </tr>
  591. <tr>
  592. <td rowspan="2"><code>foo/bar/A.java</code></td>
  593. <td><code>new/path/A.java1</code></td>
  594. </tr>
  595. <tr>
  596. <td><code>new/path/A.java2</code></td>
  597. </tr>
  598. <tr>
  599. <td rowspan="2"><code>boo/far/B.java</code></td>
  600. <td><code>new/path/B.java1</code></td>
  601. </tr>
  602. <tr>
  603. <td><code>new/path/B.java2</code></td>
  604. </tr>
  605. </table>
  606. <p>The chained mapper has no corresponding <code>&lt;mapper <var>type</var>&gt;</code>
  607. attribute.</p>
  608. <!-- -->
  609. <!-- Filter Mapper -->
  610. <!-- -->
  611. <h4 id="filter-mapper">filtermapper</h4>
  612. <p><em>Since Ant 1.6.3</em></p>
  613. <p>This mapper implementation applies a <a href="filterchain.html">filterchain</a> to the source
  614. file name.</p>
  615. <h5>Examples</h5>
  616. <pre>
  617. &lt;filtermapper&gt;
  618. &lt;replacestring from="\" to="/"/&gt;
  619. &lt;/filtermapper&gt;</pre>
  620. <table>
  621. <tr>
  622. <th>Source file name</th>
  623. <th>Target file names</th>
  624. </tr>
  625. <tr>
  626. <td><code>foo\bar\A.java</code></td>
  627. <td><code>foo/bar/A.java</code></td>
  628. </tr>
  629. </table>
  630. <pre>
  631. &lt;filtermapper&gt;
  632. &lt;scriptfilter language="beanshell"&gt;
  633. self.setToken(self.getToken().toUpperCase());
  634. &lt;/scriptfilter&gt;
  635. &lt;/filtermapper&gt;</pre>
  636. <table>
  637. <tr>
  638. <th>Source file name</th>
  639. <th>Target file names</th>
  640. </tr>
  641. <tr>
  642. <td><code>foo\bar\A.java</code></td>
  643. <td><code>FOO\BAR\A.JAVA</code></td>
  644. </tr>
  645. </table>
  646. <p>The filtermapper has no corresponding
  647. <code>&lt;mapper <var>type</var>&gt;</code> attribute.
  648. </p>
  649. <!-- -->
  650. <!-- Script Mapper -->
  651. <!-- -->
  652. <h4 id="script-mapper">scriptmapper</h4>
  653. <p><em>Since Ant 1.7</em></p>
  654. <p>This mapper executes a script written in <a href="https://jakarta.apache.org/bsf"
  655. target="_top">Apache BSF</a>
  656. or <a href="https://jcp.org/aboutJava/communityprocess/maintenance/jsr223/223ChangeLog.html"
  657. target="_top">JSR 223</a> supported language, once per file to map.</p>
  658. <p>The script can be declared inline or in a specified file.</p>
  659. <p>See the <a href="../Tasks/script.html">Script</a> task for an explanation of scripts and
  660. dependencies.</p>
  661. <table class="attr">
  662. <tr>
  663. <th>Attribute</th>
  664. <th>Description</th>
  665. <th>Required</th>
  666. </tr>
  667. <tr>
  668. <td>language</td>
  669. <td>Scripting language</td>
  670. <td>Yes</td>
  671. </tr>
  672. <tr>
  673. <td>manager</td>
  674. <td>The script engine manager to use. See the <a href="../Tasks/script.html">script</a> task
  675. for using this attribute.</td>
  676. <td>No; default is <q>auto</q></td>
  677. </tr>
  678. <tr>
  679. <td>src</td>
  680. <td>File containing the script</td>
  681. <td>No</td>
  682. </tr>
  683. <tr>
  684. <td>encoding</td>
  685. <td>The encoding of the script as a file. <em>Since Ant 1.10.2</em></td>
  686. <td>No; defaults to default JVM character encoding</td>
  687. </tr>
  688. <tr>
  689. <td>setbeans</td>
  690. <td>whether to have all properties, references and targets as global variables in the
  691. script. <em>Since Ant 1.8.0</em></td>
  692. <td>No; default is <q>true</q></td>
  693. </tr>
  694. <tr>
  695. <td>classpath</td>
  696. <td>The classpath to pass into the script.</td>
  697. <td>No</td>
  698. </tr>
  699. <tr>
  700. <td>classpathref</td>
  701. <td>The classpath to use, given as a <a href="../using.html#references">reference</a> to a
  702. path defined elsewhere.
  703. <td>No</td>
  704. </tr>
  705. </table>
  706. <p>This filename mapper can take a nested &lt;classpath&gt; element. See
  707. the <a href="../Tasks/script.html">script</a> task on how to use this element.
  708. </p>
  709. <h5>Example</h5>
  710. <pre>
  711. &lt;scriptmapper language="javascript"&gt;
  712. self.addMappedName(source.toUpperCase());
  713. self.addMappedName(source.toLowerCase());
  714. &lt;/scriptmapper&gt;</pre>
  715. <table>
  716. <tr>
  717. <th>Source file name</th>
  718. <th>Target file names</th>
  719. </tr>
  720. <tr>
  721. <td rowspan="2"><code>foo\bar\A.java</code></td>
  722. <td><code>FOO\BAR\A.JAVA</code></td>
  723. </tr>
  724. <tr>
  725. <td><code>foo\bar\a.java</code></td>
  726. </tr>
  727. </table>
  728. <p>To use this mapper, the scripts need access to the source file, and the ability to return
  729. multiple mappings. Here are the relevant beans and their methods. The script is called once for
  730. every source file, with the list of mapped names reset after every invocation.</p>
  731. <table>
  732. <tr>
  733. <th>Script bean</th>
  734. <th>Description</th>
  735. </tr>
  736. <tr>
  737. <td><code>source: String</code></td>
  738. <td>The file/path to map</td>
  739. </tr>
  740. <tr>
  741. <td><code>self</code></td>
  742. <td>The scriptmapper itself</td>
  743. </tr>
  744. <tr>
  745. <td><code>self.addMappedName(String name)</code></td>
  746. <td>Add a new mapping</td>
  747. </tr>
  748. <tr>
  749. <td><code>self.clear()</code></td>
  750. <td>Reset the list of files</td>
  751. </tr>
  752. </table>
  753. <p>The scriptmapper has no corresponding <code>&lt;mapper <var>type</var>&gt;</code> attribute.</p>
  754. <h4 id="firstmatch-mapper">firstmatchmapper</h4>
  755. <p><em>Since Ant 1.8.0</em></p>
  756. <p>This mapper supports an arbitrary number of nested mappers and returns the results of the first
  757. mapper that matches. This is different from <a href="#composite-mapper">composite mapper</a> which
  758. collects the results of all matching children.</p>
  759. <h5>Examples</h5>
  760. <pre>
  761. &lt;firstmatchmapper&gt;
  762. &lt;globmapper from="*.txt" to="*.bak"/&gt;
  763. &lt;globmapper from="*A.*" to="*B.*"/&gt;
  764. &lt;/firstmatchmapper&gt;</pre>
  765. <table>
  766. <tr>
  767. <th>Source file name</th>
  768. <th>Target file names</th>
  769. </tr>
  770. <tr>
  771. <td><code>foo/bar/A.txt</code></td>
  772. <td><code>foo/bar/A.bak</code></td>
  773. </tr>
  774. <tr>
  775. <td><code>foo/bar/A.java</code></td>
  776. <td><code>foo/bar/B.java</code></td>
  777. </tr>
  778. </table>
  779. <p>The firstmatchmapper has no corresponding <code>&lt;mapper <var>type</var>&gt;</code>
  780. attribute.</p>
  781. <h4 id="cutdirs-mapper">cutdirsmapper</h4>
  782. <p><em>Since Ant 1.8.2</em></p>
  783. <p>This mapper strips a configured number of leading directories from the source file name.</p>
  784. <h5>Examples</h5>
  785. <pre>&lt;cutdirsmapper dirs="1"/&gt;</pre>
  786. <table>
  787. <tr>
  788. <th>Source file name</th>
  789. <th>Target file names</th>
  790. </tr>
  791. <tr>
  792. <td><code>foo/bar/A.txt</code></td>
  793. <td><code>bar/A.txt</code></td>
  794. </tr>
  795. </table>
  796. <p>The cutdirsmapper has no corresponding <code>&lt;mapper <var>type</var>&gt;</code> attribute.</p>
  797. <table class="attr">
  798. <tr>
  799. <th>Attribute</th>
  800. <th>Description</th>
  801. <th>Required</th>
  802. </tr>
  803. <tr>
  804. <td>dirs</td>
  805. <td>Number of directories to strip (must be a positive number).</td>
  806. <td>Yes</td>
  807. </tr>
  808. </table>
  809. </body>
  810. </html>