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.

jar.html 27 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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>Jar Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="jar">Jar</a></h2>
  23. <h3>Description</h3>
  24. <p>Jars a set of files.</p>
  25. <p>The <i>basedir</i> attribute is the reference directory from where to jar.</p>
  26. <p>Note that file permissions will not be stored in the resulting jarfile.</p>
  27. <p>It is possible to refine the set of files that are being jarred. This can be
  28. done with the <i>includes</i>, <i>includesfile</i>, <i>excludes</i>, <i>excludesfile</i> and <i>defaultexcludes</i>
  29. attributes. With the <i>includes</i> or <i>includesfile</i> attribute you specify the files you want to
  30. have included by using patterns. The <i>exclude</i> or <i>excludesfile</i> attribute is used to specify
  31. the files you want to have excluded. This is also done with patterns. And
  32. finally with the <i>defaultexcludes</i> attribute, you can specify whether you
  33. want to use default exclusions or not. See the section on <a
  34. href="../dirtasks.html#directorybasedtasks">directory based tasks</a>, on how the
  35. inclusion/exclusion of files works, and how to write patterns.</p>
  36. <p>This task forms an implicit <a href="../Types/fileset.html">FileSet</a> and
  37. supports most attributes of <code>&lt;fileset&gt;</code>
  38. (<code>dir</code> becomes <code>basedir</code>) as well as the nested
  39. <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code> and
  40. <code>&lt;patternset&gt;</code> elements.</p>
  41. <p>You can also use nested file sets for more flexibility, and specify
  42. multiple ones to merge together different trees of files into one JAR.
  43. The extended fileset and groupfileset child elements from the zip task are
  44. also available in the jar task.
  45. See the <a href="zip.html">Zip</a> task for more details and examples.</p>
  46. <p>The <code>update</code> parameter controls what happens if the JAR
  47. file already exists. When set to <code>yes</code>, the JAR file is
  48. updated with the files specified. When set to <code>no</code> (the
  49. default) the JAR file is overwritten. An example use of this is
  50. provided in the <a href="zip.html">Zip task documentation</a>. Please
  51. note that ZIP files store file modification times with a granularity
  52. of two seconds. If a file is less than two seconds newer than the
  53. entry in the archive, Ant will not consider it newer.</p>
  54. <p>If the manifest is omitted, a simple one will be supplied by Apache Ant.</p>
  55. <p>The <code>whenmanifestonly</code> parameter controls what happens when no
  56. files, apart from the manifest file, or nested services, match.
  57. If <code>skip</code>, the JAR is not created and a warning is issued.
  58. If <code>fail</code>, the JAR is not created and the build is halted with an error.
  59. If <code>create</code>, (default) an empty JAR file (only containing a manifest and services)
  60. is created.</p>
  61. <p>(The Jar task is a shortcut for specifying the manifest file of a JAR file.
  62. The same thing can be accomplished by using the <i>fullpath</i>
  63. attribute of a zipfileset in a Zip task. The one difference is that if the
  64. <i>manifest</i> attribute is not specified, the Jar task will
  65. include an empty one for you.)</p>
  66. <p>Manifests are processed by the Jar task according to the
  67. <a target="_blank" href="http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html">Jar file specification.</a>
  68. Note in particular that this may result in manifest lines greater than 72 bytes
  69. being wrapped and continued on the next line.</p>
  70. <p>The Jar task checks whether you specified package information according to the
  71. <a target="_blank" href="http://docs.oracle.com/javase/7/docs/technotes/guides/versioning/spec/versioning2.html#wp90779">
  72. versioning specification</a>.</p>
  73. <p><b>Please note that the zip format allows multiple files of the same
  74. fully-qualified name to exist within a single archive. This has been
  75. documented as causing various problems for unsuspecting users. If you wish
  76. to avoid this behavior you must set the <code>duplicate</code> attribute
  77. to a value other than its default, <code>"add"</code>.</b></p>
  78. <p>To cryptographically sign your JAR file, use the <a href="signjar.html">SignJar task</a> on the JAR that you create from this task.</p>
  79. <p>For creating a simple version of a <a target="_blank" href="http://openjdk.java.net/jeps/238">JEP-238 multi release jar</a>,
  80. you don't need any special tools. Just set the required manifest entry and place the files where required, as you could see
  81. in the <a href="#jep238-example">JEP238-example</a>. If you want to tune this kind of jar, e.g. decreasing the size by deleting
  82. 'same' classes from the versions-branches, you have to do more ...</p>
  83. <h3>Parameters</h3>
  84. <table border="1" cellpadding="2" cellspacing="0">
  85. <tr>
  86. <td valign="top"><b>Attribute</b></td>
  87. <td valign="top"><b>Description</b></td>
  88. <td align="center" valign="top"><b>Required</b></td>
  89. </tr>
  90. <tr>
  91. <td valign="top">destfile</td>
  92. <td valign="top">the JAR file to create.</td>
  93. <td valign="top" align="center">Yes</td>
  94. </tr>
  95. <tr>
  96. <td valign="top">basedir</td>
  97. <td valign="top">the directory from which to jar the files.</td>
  98. <td valign="top" align="center">No</td>
  99. </tr>
  100. <tr>
  101. <td valign="top">compress</td>
  102. <td valign="top">Not only store data but also compress them,
  103. defaults to true. Unless you set the <em>keepcompression</em>
  104. attribute to false, this will apply to the entire archive, not
  105. only the files you've added while updating.</td>
  106. <td align="center" valign="top">No</td>
  107. </tr>
  108. <tr>
  109. <td valign="top">keepcompression</td>
  110. <td valign="top">For entries coming from existing archives (like
  111. nested <em>zipfileset</em>s or while updating the archive), keep
  112. the compression as it has been originally instead of using the
  113. <em>compress</em> attribute. Defaults false. <em>Since Ant
  114. 1.6</em></td>
  115. <td align="center" valign="top">No</td>
  116. </tr>
  117. <tr>
  118. <td valign="top">encoding</td>
  119. <td valign="top">The character encoding to use for filenames
  120. inside the archive. Defaults to UTF8. <strong>It is not
  121. recommended to change this value as the created archive will
  122. most likely be unreadable for Java otherwise.</strong>
  123. <br/>See also the <a href="zip.html#encoding">discussion in the
  124. zip task page</a></td>
  125. <td align="center" valign="top">No</td>
  126. </tr>
  127. <tr>
  128. <td valign="top">filesonly</td>
  129. <td valign="top">Store only file entries, defaults to false</td>
  130. <td align="center" valign="top">No</td>
  131. </tr>
  132. <tr>
  133. <td valign="top">includes</td>
  134. <td valign="top">comma- or space-separated list of patterns of files that must be
  135. included. All files are included when omitted.</td>
  136. <td valign="top" align="center">No</td>
  137. </tr>
  138. <tr>
  139. <td valign="top">includesfile</td>
  140. <td valign="top">the name of a file. Each line of this file is
  141. taken to be an include pattern</td>
  142. <td valign="top" align="center">No</td>
  143. </tr>
  144. <tr>
  145. <td valign="top">excludes</td>
  146. <td valign="top">comma- or space-separated list of patterns of files that must be
  147. excluded. No files (except default excludes) are excluded when omitted.</td>
  148. <td valign="top" align="center">No</td>
  149. </tr>
  150. <tr>
  151. <td valign="top">excludesfile</td>
  152. <td valign="top">the name of a file. Each line of this file is
  153. taken to be an exclude pattern</td>
  154. <td valign="top" align="center">No</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">defaultexcludes</td>
  158. <td valign="top">indicates whether default excludes should be used or not
  159. (&quot;yes&quot;/&quot;no&quot;). Default excludes are used when omitted.</td>
  160. <td valign="top" align="center">No</td>
  161. </tr>
  162. <tr>
  163. <td valign="top">manifest</td>
  164. <td valign="top">the manifest file to use. This can be either the location of a manifest, or the name of a jar added through a fileset. If its the name of an added jar, the task expects the manifest to be in the jar at META-INF/MANIFEST.MF</td>
  165. <td valign="top" align="center">No</td>
  166. </tr>
  167. <tr>
  168. <td valign="top">filesetmanifest</td>
  169. <td valign="top">behavior when a Manifest is found in a zipfileset or zipgroupfileset file is found. Valid values are &quot;skip&quot;, &quot;merge&quot;, and &quot;mergewithoutmain&quot;. &quot;merge&quot; will merge all of the manifests together, and merge this into any other specified manifests. &quot;mergewithoutmain&quot; merges everything but the Main section of the manifests. Default value is &quot;skip&quot;.
  170. </td>
  171. <td valign="top" align="center">No</td>
  172. </tr>
  173. <tr>
  174. <td valign="top">update</td>
  175. <td valign="top">indicates whether to update or overwrite
  176. the destination file if it already exists. Default is &quot;false&quot;.</td>
  177. <td valign="top" align="center">No</td>
  178. </tr>
  179. <tr>
  180. <td valign="top">whenmanifestonly</td>
  181. <td valign="top">behavior when no files match. Valid values are &quot;fail&quot;, &quot;skip&quot;, and &quot;create&quot;. Default is &quot;create&quot;.</td>
  182. <td valign="top" align="center">No</td>
  183. </tr>
  184. <tr>
  185. <td valign="top">duplicate</td>
  186. <td valign="top">behavior when a duplicate file is found. Valid values are &quot;add&quot;, &quot;preserve&quot;, and &quot;fail&quot;. The default value is &quot;add&quot;. </td>
  187. <td valign="top" align="center">No</td>
  188. </tr>
  189. <tr>
  190. <td valign="top">index</td>
  191. <td valign="top">whether to create an <A
  192. HREF="http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#JAR_Index">index
  193. list</A> to speed up classloading. This is a JDK 1.3+ specific
  194. feature. Unless you specify additional jars with nested <a
  195. href="#indexjars"><code>indexjars</code></a> elements, only the
  196. contents of this jar will be included in the index. Defaults to
  197. false.</td>
  198. <td valign="top" align="center">No</td>
  199. </tr>
  200. <tr>
  201. <td valign="top">indexMetaInf</td>
  202. <td valign="top">whether to include META-INF and its children in
  203. the index. Doesn't have any effect if <em>index</em> is
  204. false.<br/>
  205. Sun's jar implementation used to skip the META-INF directory and
  206. Ant followed that example. The behavior has been changed with
  207. <a href="https://bugs.openjdk.java.net/browse/JDK-4408526">Java
  208. 5</a>. In order to avoid problems with Ant generated jars on
  209. Java 1.4 or earlier Ant will not include META-INF unless
  210. explicitly asked to.<br/>
  211. <em>Ant 1.8.0</em> - Defaults to false.</td>
  212. <td valign="top" align="center">No</td>
  213. </tr>
  214. <tr>
  215. <td valign="top">manifestencoding</td>
  216. <td valign="top">The encoding used to read the JAR manifest, when
  217. a manifest file is specified. The task will always use UTF-8
  218. when writing the manifest.</td>
  219. <td valign="top" align="center">No, defaults to the platform encoding.</td>
  220. </tr>
  221. <tr>
  222. <td valign="top">roundup</td>
  223. <td valign="top">Whether the file modification times will be
  224. rounded up to the next even number of seconds.<br>
  225. Zip archives store file modification times with a granularity of
  226. two seconds, so the times will either be rounded up or down. If
  227. you round down, the archive will always seem out-of-date when you
  228. rerun the task, so the default is to round up. Rounding up may
  229. lead to a different type of problems like JSPs inside a web
  230. archive that seem to be slightly more recent than precompiled
  231. pages, rendering precompilation useless.<br>
  232. Defaults to true. <em>Since Ant 1.6.2</em></td>
  233. <td align="center" valign="top">No</td>
  234. </tr>
  235. <tr>
  236. <td valign="top">level</td>
  237. <td valign="top">Non-default level at which file compression should be
  238. performed. Valid values range from 0 (no compression/fastest) to 9
  239. (maximum compression/slowest). <em>Since Ant 1.7</em></td>
  240. <td valign="top" align="center">No</td>
  241. </tr>
  242. <tr>
  243. <td valign="top">strict</td>
  244. <td valign="top">Configures how to handle breaks of the packaging version
  245. specification: <ul>
  246. <li><b>fail</b> = throws a BuildException</li>
  247. <li><b>warn</b> = logs a message on warn level</li>
  248. <li><b>ignore</b> = logs a message on verbose level (default)</li>
  249. </ul>
  250. <em>Since Ant 1.7.1</em></td>
  251. <td valign="top" align="center">No, defaults to <tt>ignore</tt>. </td>
  252. </tr>
  253. <tr>
  254. <td valign="top">preserve0permissions</td>
  255. <td valign="top">when updating an archive or adding entries from a
  256. different archive Ant will assume that a Unix permissions value of
  257. 0 (nobody is allowed to do anything to the file/directory) means
  258. that the permissions haven't been stored at all rather than real
  259. permissions and will instead apply its own default values.<br/>
  260. Set this attribute to true if you really want to preserve the
  261. original permission field.<em>since Ant 1.8.0</em>
  262. </td>
  263. <td valign="top" align="center">No, default is false</td>
  264. </tr>
  265. <tr>
  266. <td valign="top">useLanguageEncodingFlag</td>
  267. <td valign="top">Whether to set the language encoding flag if the
  268. encoding is UTF-8. This setting doesn't have any effect if the
  269. encoding is not UTF-8.
  270. <em>Since Ant 1.8.0</em>.
  271. <br/>See also the <a href="zip.html#encoding">discussion in the
  272. zip task page</a></td>
  273. <td valign="top" align="center">No, default is true</td>
  274. </tr>
  275. <tr>
  276. <td valign="top">createUnicodeExtraFields</td>
  277. <td valign="top">Whether to create unicode extra fields to store
  278. the file names a second time inside the entry's metadata.
  279. <br>Possible values are "never", "always" and "not-encodeable"
  280. which will only add Unicode extra fields if the file name cannot
  281. be encoded using the specified encoding.
  282. <em>Since Ant 1.8.0</em>.
  283. <br/>See also the <a href="zip.html#encoding">discussion in the
  284. zip task page</a></td>
  285. <td align="center" valign="top">No, default is "never"</td>
  286. </tr>
  287. <tr>
  288. <td valign="top">fallbacktoUTF8</td>
  289. <td valign="top">Whether to use UTF-8 and the language encoding
  290. flag instead of the specified encoding if a file name cannot be
  291. encoded using the specified encoding.
  292. <em>Since Ant 1.8.0</em>.
  293. <br/>See also the <a href="zip.html#encoding">discussion in the
  294. zip task page</a></td>
  295. <td align="center" valign="top">No, default is false</td>
  296. </tr>
  297. <tr>
  298. <td valign="top">mergeClassPathAttributes</td>
  299. <td valign="top">Whether to merge the Class-Path attributes found
  300. in different manifests (if merging manifests). If false, only
  301. the attribute of the last merged manifest will be preserved.
  302. <em>Since Ant 1.8.0</em>.
  303. <br/>unless you also set flattenAttributes to true this may
  304. result in manifests containing multiple Class-Path attributes
  305. which violates the manifest specification.</td>
  306. <td align="center" valign="top">No, default is false</td>
  307. </tr>
  308. <tr>
  309. <td valign="top">flattenAttributes</td>
  310. <td valign="top">Whether to merge attributes occurring more than
  311. once in a section (this can only happen for the Class-Path
  312. attribute) into a single attribute.
  313. <em>Since Ant 1.8.0</em>.</td>
  314. <td align="center" valign="top">No, default is false</td>
  315. </tr>
  316. <tr>
  317. <td valign="top">zip64Mode</td>
  318. <td valign="top">When to use Zip64 extensions for entries. The
  319. possible values are "never", "always" and "as-needed".
  320. <em>Since Ant 1.9.1</em>.
  321. <br/>See also the <a href="zip.html#zip64">discussion in the
  322. zip task page</a></td>
  323. <td align="center" valign="top">No, default is "never"</td>
  324. </tr>
  325. </table>
  326. <h3>Nested elements</h3>
  327. <h4>metainf</h4>
  328. <p>The nested <code>metainf</code> element specifies a <a
  329. href="../Types/fileset.html">FileSet</a>. All files included in this fileset will
  330. end up in the <code>META-INF</code> directory of the jar file. If this
  331. fileset includes a file named <code>MANIFEST.MF</code>, the file is
  332. ignored and you will get a warning.</p>
  333. <h4>manifest</h4>
  334. <p>The manifest nested element allows the manifest for the Jar file to
  335. be provided inline in the build file rather than in an external
  336. file. This element is identical to the
  337. <a href="manifest.html">manifest</a> task, but the file and mode
  338. attributes must be omitted.</p>
  339. <p>
  340. If both an inline manifest and an external file are both specified, the
  341. manifests are merged.
  342. </p>
  343. <p>When using inline manifests, the Jar task will check whether the manifest
  344. contents have changed (i.e. the manifest as specified is different in any way
  345. from the manifest that exists in the Jar, if it exists.
  346. If the manifest values have changed the jar will be updated or rebuilt, as
  347. appropriate.
  348. </p>
  349. <a name="indexjars"><h4>indexjars</h4></a>
  350. <p><em>since ant 1.6.2</em></p>
  351. <p>The nested <code>indexjars</code> element specifies a <a
  352. href="../using.html#path">PATH like structure</a>. Its content is
  353. completely ignored unless you set the index attribute of the task to
  354. true.</p>
  355. <p>The index created by this task will contain indices for the
  356. archives contained in this path, the names used for the archives
  357. depend on your manifest:</p>
  358. <ul>
  359. <li>If the generated jar's manifest contains no Class-Path
  360. attribute, the file name without any leading directory path will be
  361. used and all parts of the path will get indexed.</li>
  362. <li>If the manifest contains a Class-Path attribute, this task will
  363. try to guess which part of the Class-Path belongs to a given
  364. archive. If it cannot guess a name, the archive will be skipped,
  365. otherwise the name listed inside the Class-Path attribute will be
  366. used.</li>
  367. </ul>
  368. <p>This task will not create any index entries for archives that are
  369. empty or only contain files inside the META-INF directory unless
  370. the <code>indexmetainf</code> attribute has been set
  371. to <code>true</code>.</p>
  372. <a name="service"><h4>service</h4></a>
  373. <p><em>since ant 1.7.0</em></p>
  374. <p>
  375. The nested <code>service</code> element specifies a service.
  376. Services are described in the
  377. <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Service_Provider">service provider overview</a>.
  378. The approach is to have providers JARs include files named by the service
  379. provided, for example,
  380. META-INF/services/javax.script.ScriptEngineFactory
  381. which can include implementation class names, one per line (usually just one per JAR).
  382. The name of the
  383. service is set by the "type" attribute. The classname implementing
  384. the service is the the "provider" attribute, or it one wants to
  385. specify a number of classes that implement the service, by
  386. "provider" nested elements.
  387. </p>
  388. <p>
  389. <table border="1" cellpadding="2" cellspacing="0">
  390. <tr>
  391. <td valign="top"><b>Attribute</b></td>
  392. <td valign="top"><b>Description</b></td>
  393. <td align="center" valign="top"><b>Required</b></td>
  394. </tr>
  395. <tr>
  396. <td valign="top">type</td>
  397. <td valign="top">The name of the service.</td>
  398. <td valign="top" align="center">Yes</td>
  399. </tr>
  400. <tr>
  401. <td valign="top">provider</td>
  402. <td valign="top">
  403. The classname of the class implementing the service.
  404. </td>
  405. <td valign="top" align="center">Yes, unless there is a nested
  406. <code>&lt;provider&gt;</code> element.</td>
  407. </tr>
  408. </table>
  409. <p>
  410. The provider classname is specified either by the "provider" attribute, or
  411. by a nested &lt;provider&gt; element, which has a single "classname" attribute.
  412. If a JAR file has more that one implementation of the service, a number of
  413. nested &lt;provider&gt; elements may be used.
  414. </p>
  415. <h3>Examples</h3>
  416. <h4>Simple</h4>
  417. <blockquote><pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot; basedir=&quot;${build}/classes&quot;/&gt;</pre></blockquote>
  418. <p>jars all files in the <code>${build}/classes</code> directory into a file
  419. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.</p>
  420. <h4>With filters</h4>
  421. <blockquote><pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;
  422. basedir=&quot;${build}/classes&quot;
  423. excludes=&quot;**/Test.class&quot;
  424. /&gt;</pre></blockquote>
  425. <p>jars all files in the <code>${build}/classes</code> directory into a file
  426. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Files
  427. with the name <code>Test.class</code> are excluded.</p>
  428. <blockquote><pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;
  429. basedir=&quot;${build}/classes&quot;
  430. includes=&quot;mypackage/test/**&quot;
  431. excludes=&quot;**/Test.class&quot;
  432. /&gt;</pre></blockquote>
  433. <p>jars all files in the <code>${build}/classes</code> directory into a file
  434. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Only
  435. files under the directory <code>mypackage/test</code> are used, and files with
  436. the name <code>Test.class</code> are excluded.</p>
  437. <h4>Multiple filesets</h4>
  438. <blockquote><pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;&gt;
  439. &lt;fileset dir=&quot;${build}/classes&quot;
  440. excludes=&quot;**/Test.class&quot;
  441. /&gt;
  442. &lt;fileset dir=&quot;${src}/resources&quot;/&gt;
  443. &lt;/jar&gt;</pre></blockquote>
  444. <p>jars all files in the <code>${build}/classes</code> directory and also
  445. in the <code>${src}/resources</code> directory together into a file
  446. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.
  447. Files with the name <code>Test.class</code> are excluded.
  448. If there are files such as <code>${build}/classes/mypackage/MyClass.class</code>
  449. and <code>${src}/resources/mypackage/image.gif</code>, they will appear
  450. in the same directory in the JAR (and thus be considered in the same package
  451. by Java).</p>
  452. <h4>Merging archives</h4>
  453. <blockquote><pre> &lt;jar destfile="build/main/checksites.jar"&gt;
  454. &lt;fileset dir="build/main/classes"/&gt;
  455. &lt;zipfileset includes="**/*.class" src="lib/main/some.jar"/&gt;
  456. &lt;manifest&gt;
  457. &lt;attribute name="Main-Class"
  458. value="com.acme.checksites.Main"/&gt;
  459. &lt;/manifest&gt;
  460. &lt;/jar&gt;</pre></blockquote>
  461. <p>
  462. Creates an executable jar file with a main class "com.acme.checksites.Main", and
  463. embeds all the classes from the jar <code>lib/main/some.jar</code>.
  464. </p>
  465. <blockquote><pre> &lt;jar destfile="build/main/checksites.jar"&gt;
  466. &lt;fileset dir="build/main/classes"/&gt;
  467. &lt;restrict&gt;
  468. &lt;name name="**/*.class"/&gt;
  469. &lt;archives&gt;
  470. &lt;zips&gt;
  471. &lt;fileset dir="lib/main" includes="**/*.jar"/&gt;
  472. &lt;/zips&gt;
  473. &lt;/archives&gt;
  474. &lt;/restrict&gt;
  475. &lt;manifest&gt;
  476. &lt;attribute name="Main-Class"
  477. value="com.acme.checksites.Main"/&gt;
  478. &lt;/manifest&gt;
  479. &lt;/jar&gt;</pre></blockquote>
  480. <p>
  481. Creates an executable jar file with a main class "com.acme.checksites.Main", and
  482. embeds all the classes from all the jars in <code>lib/main</code>.
  483. </p>
  484. <h4>Inline manifest</h4>
  485. <blockquote><pre> &lt;jar destfile=&quot;test.jar&quot; basedir=&quot;.&quot;&gt;
  486. &lt;include name=&quot;build&quot;/&gt;
  487. &lt;manifest&gt;
  488. &lt;!-- If this is an Applet or Web Start application, include
  489. the proper attributes from <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/index.html">http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/index.html</a> --&gt;
  490. &lt;attribute name=&quot;Permissions&quot; value=&quot;sandbox&quot;/&gt;
  491. &lt;attribute name=&quot;Codebase&quot; value=&quot;example.com&quot;/&gt;
  492. &lt;!-- Who is building this jar? --&gt;
  493. &lt;attribute name=&quot;Built-By&quot; value=&quot;${user.name}&quot;/&gt;
  494. &lt;!-- Information about the program itself --&gt;
  495. &lt;attribute name=&quot;Implementation-Vendor&quot; value=&quot;ACME inc.&quot;/&gt;
  496. &lt;attribute name=&quot;Implementation-Title&quot; value=&quot;GreatProduct&quot;/&gt;
  497. &lt;attribute name=&quot;Implementation-Version&quot; value=&quot;1.0.0beta2&quot;/&gt;
  498. &lt;!-- details --&gt;
  499. &lt;section name=&quot;common/MyClass.class&quot;&gt;
  500. &lt;attribute name=&quot;Sealed&quot; value=&quot;false&quot;/&gt;
  501. &lt;/section&gt;
  502. &lt;/manifest&gt;
  503. &lt;/jar&gt;</pre></blockquote>
  504. <p>
  505. This is an example of an inline manifest specification including the version of the build
  506. program (Implementation-Version). Note that the Built-By attribute will take the value of the Ant
  507. property ${user.name}. The manifest produced by the above would look like this:
  508. </p>
  509. <blockquote><pre><code>Manifest-Version: 1.0
  510. Permissions: sandbox
  511. Codebase: example.com
  512. Built-By: conor
  513. Implementation-Vendor: ACME inc.
  514. Implementation-Title: GreatProduct
  515. Implementation-Version: 1.0.0beta2
  516. Created-By: Apache Ant 1.9.2
  517. Name: common/MyClass.class
  518. Sealed: false</code></pre></blockquote>
  519. <h4>Service Provider</h4>
  520. <p>
  521. The following shows how to create a jar file specifying a service
  522. with an implementation of the JDK6 scripting interface:
  523. </p>
  524. <blockquote><pre>&lt;jar destfile="pinky.jar"&gt;
  525. &lt;fileset dir="build/classes"/&gt;
  526. &lt;service type="javax.script.ScriptEngineFactory"
  527. provider="org.acme.PinkyLanguage"/&gt;
  528. &lt;/jar&gt;
  529. </pre></blockquote>
  530. <p>
  531. The following shows how to create a jar file specifing a service
  532. with two implementations of the JDK6 scripting interface:
  533. </p>
  534. <blockquote><pre>
  535. &lt;jar destfile="pinkyandbrain.jar"&gt;
  536. &lt;fileset dir="classes"/&gt;
  537. &lt;service type="javax.script.ScriptEngineFactory"&gt;
  538. &lt;provider classname="org.acme.PinkyLanguage"/&gt;
  539. &lt;provider classname="org.acme.BrainLanguage"/&gt;
  540. &lt;/service&gt;
  541. &lt;/jar&gt;
  542. </pre></blockquote>
  543. <a name="jep238-example"/>
  544. <h4>JEP238 example: a Multi-Release JAR Files</h4>
  545. <p>
  546. Here we want to create a <i>Multi-Release JAR File</i> according the specification
  547. <a target="_blank" href="http://openjdk.java.net/jeps/238">JEP-238</a>.
  548. It defines on top of a JAR the possibility to place additional or overwriting classes
  549. in a jar, which are available according to the Java version you run.<br>
  550. Basically it sais, that you have to set the manifest entry <tt>Multi-Release: true</tt>
  551. and place all additional or overwriting classes in
  552. <tt>META-INF/versions/<i>number</i>/package-structure</tt>, e.g.
  553. <tt>META-INF/versions/9/org/apache/ant/MyClass.class</tt>
  554. </p>
  555. <p>
  556. In this example we expect that the normal classes are compiled into
  557. <code>${java.classes}</code> and the Java9 classes are compiled into
  558. <code>${java9.classes}</code>.
  559. </p>
  560. <blockquote><pre>
  561. &lt;jar destfile=&quot;mrjar.jar&quot;&gt;
  562. &lt;manifest&gt;
  563. &lt;!-- special mf-entry according to the spec --&gt;
  564. &lt;attribute name=&quot;Multi-Release&quot; value=&quot;true&quot;/&gt;
  565. &lt;/manifest&gt;
  566. &lt;!-- directory structure according to the spec ... --&gt;
  567. &lt;!-- ... default classes loadable by old (&lt;Java9) versions --&gt;
  568. &lt;fileset dir=&quot;${java.classes}&quot;/&gt;
  569. &lt;!-- ... per release classes, require Java9+ for loadable via standard ClassLoader --&gt;
  570. &lt;zipfileset prefix=&quot;META-INF/versions/9/&quot; dir=&quot;${java9.classes}&quot;/&gt;
  571. &lt;/jar&gt;
  572. </pre></blockquote>
  573. </body>
  574. </html>