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 26 KiB

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