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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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="../CoreTypes/fileset.html">FileSet</a> and
  37. supports all 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>If the manifest is omitted, a simple one will be supplied by Ant.</p>
  47. <p>The <code>update</code> parameter controls what happens if the JAR
  48. file already exists. When set to <code>yes</code>, the JAR file is
  49. updated with the files specified. When set to <code>no</code> (the
  50. default) the JAR file is overwritten. An example use of this is
  51. provided in the <a href="zip.html">Zip task documentation</a>. Please
  52. note that ZIP files store file modification times with a granularity
  53. of two seconds. If a file is less than two seconds newer than the
  54. entry in the archive, Ant will not consider it newer.</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://java.sun.com/j2se/1.3/docs/guide/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://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning">
  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. <h3>Parameters</h3>
  79. <table border="1" cellpadding="2" cellspacing="0">
  80. <tr>
  81. <td valign="top"><b>Attribute</b></td>
  82. <td valign="top"><b>Description</b></td>
  83. <td align="center" valign="top"><b>Required</b></td>
  84. </tr>
  85. <tr>
  86. <td valign="top">destfile</td>
  87. <td valign="top">the JAR file to create.</td>
  88. <td valign="top" align="center">Yes</td>
  89. </tr>
  90. <tr>
  91. <td valign="top">basedir</td>
  92. <td valign="top">the directory from which to jar the files.</td>
  93. <td valign="top" align="center">No</td>
  94. </tr>
  95. <tr>
  96. <td valign="top">compress</td>
  97. <td valign="top">Not only store data but also compress them,
  98. defaults to true. Unless you set the <em>keepcompression</em>
  99. attribute to false, this will apply to the entire archive, not
  100. only the files you've added while updating.</td>
  101. <td align="center" valign="top">No</td>
  102. </tr>
  103. <tr>
  104. <td valign="top">keepcompression</td>
  105. <td valign="top">For entries coming from existing archives (like
  106. nested <em>zipfileset</em>s or while updating the archive), keep
  107. the compression as it has been originally instead of using the
  108. <em>compress</em> attribute. Defaults false. <em>Since Ant
  109. 1.6</em></td>
  110. <td align="center" valign="top">No</td>
  111. </tr>
  112. <tr>
  113. <td valign="top">encoding</td>
  114. <td valign="top">The character encoding to use for filenames
  115. inside the archive. Defaults to UTF8. <strong>It is not
  116. recommended to change this value as the created archive will most
  117. likely be unreadable for Java otherwise.</strong></td>
  118. <td align="center" valign="top">No</td>
  119. </tr>
  120. <tr>
  121. <td valign="top">filesonly</td>
  122. <td valign="top">Store only file entries, defaults to false</td>
  123. <td align="center" valign="top">No</td>
  124. </tr>
  125. <tr>
  126. <td valign="top">includes</td>
  127. <td valign="top">comma- or space-separated list of patterns of files that must be
  128. included. All files are included when omitted.</td>
  129. <td valign="top" align="center">No</td>
  130. </tr>
  131. <tr>
  132. <td valign="top">includesfile</td>
  133. <td valign="top">the name of a file. Each line of this file is
  134. taken to be an include pattern</td>
  135. <td valign="top" align="center">No</td>
  136. </tr>
  137. <tr>
  138. <td valign="top">excludes</td>
  139. <td valign="top">comma- or space-separated list of patterns of files that must be
  140. excluded. No files (except default excludes) are excluded when omitted.</td>
  141. <td valign="top" align="center">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">excludesfile</td>
  145. <td valign="top">the name of a file. Each line of this file is
  146. taken to be an exclude pattern</td>
  147. <td valign="top" align="center">No</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">defaultexcludes</td>
  151. <td valign="top">indicates whether default excludes should be used or not
  152. (&quot;yes&quot;/&quot;no&quot;). Default excludes are used when omitted.</td>
  153. <td valign="top" align="center">No</td>
  154. </tr>
  155. <tr>
  156. <td valign="top">manifest</td>
  157. <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>
  158. <td valign="top" align="center">No</td>
  159. </tr>
  160. <tr>
  161. <td valign="top">filesetmanifest</td>
  162. <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;.
  163. </td>
  164. <td valign="top" align="center">No</td>
  165. </tr>
  166. <tr>
  167. <td valign="top">update</td>
  168. <td valign="top">indicates whether to update or overwrite
  169. the destination file if it already exists. Default is &quot;false&quot;.</td>
  170. <td valign="top" align="center">No</td>
  171. </tr>
  172. <tr>
  173. <td valign="top">whenmanifestonly</td>
  174. <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>
  175. <td valign="top" align="center">No</td>
  176. </tr>
  177. <tr>
  178. <td valign="top">duplicate</td>
  179. <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>
  180. <td valign="top" align="center">No</td>
  181. </tr>
  182. <tr>
  183. <td valign="top">index</td>
  184. <td valign="top">whether to create an <A
  185. HREF="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Index">index
  186. list</A> to speed up classloading. This is a JDK 1.3+ specific
  187. feature. Unless you specify additional jars with nested <a
  188. href="#indexjars"><code>indexjars</code></a> elements, only the
  189. contents of this jar will be included in the index. Defaults to
  190. false.</td>
  191. <td valign="top" align="center">No</td>
  192. </tr>
  193. <tr>
  194. <td valign="top">manifestencoding</td>
  195. <td valign="top">The encoding used to read the JAR manifest, when a manifest file is specified.</td>
  196. <td valign="top" align="center">No, defaults to the platform encoding.</td>
  197. </tr>
  198. <tr>
  199. <td valign="top">roundup</td>
  200. <td valign="top">Whether the file modification times will be
  201. rounded up to the next even number of seconds.<br>
  202. Zip archives store file modification times with a granularity of
  203. two seconds, so the times will either be rounded up or down. If
  204. you round down, the archive will always seem out-of-date when you
  205. rerun the task, so the default is to round up. Rounding up may
  206. lead to a different type of problems like JSPs inside a web
  207. archive that seem to be slightly more recent than precompiled
  208. pages, rendering precompilation useless.<br>
  209. Defaults to true. <em>Since Ant 1.6.2</em></td>
  210. <td align="center" valign="top">No</td>
  211. </tr>
  212. <tr>
  213. <td valign="top">level</td>
  214. <td valign="top">Non-default level at which file compression should be
  215. performed. Valid values range from 0 (no compression/fastest) to 9
  216. (maximum compression/slowest). <em>Since Ant 1.7</em></td>
  217. <td valign="top" align="center">No</td>
  218. </tr>
  219. <tr>
  220. <td valign="top">strict</td>
  221. <td valign="top">Configures how to handle breaks of the packaging version
  222. specification: <ul>
  223. <li><b>fail</b> = throws a BuildException</li>
  224. <li><b>warn</b> = logs a message on warn level</li>
  225. <li><b>ignore</b> = logs a message on verbose level (default)</li>
  226. </ul>
  227. <em>Since Ant 1.7.1</em></td>
  228. <td valign="top" align="center">No, defaults to <tt>ignore</tt>. </td>
  229. </tr>
  230. <tr>
  231. <td valign="top">preserve0permissions</td>
  232. <td valign="top">when updating an archive or adding entries from a
  233. different archive Ant will assume that a Unix permissions value of
  234. 0 (nobody is allowed to do anything to the file/directory) means
  235. that the permissions haven't been stored at all rather than real
  236. permissions and will instead apply its own default values.<br/>
  237. Set this attribute to true if you really want to preserve the
  238. original permission field.<em>since Ant 1.8.0</em>
  239. </td>
  240. <td valign="top" align="center">No, default is false</td>
  241. </tr>
  242. </table>
  243. <h3>Nested elements</h3>
  244. <h4>metainf</h4>
  245. <p>The nested <code>metainf</code> element specifies a <a
  246. href="../CoreTypes/fileset.html">FileSet</a>. All files included in this fileset will
  247. end up in the <code>META-INF</code> directory of the jar file. If this
  248. fileset includes a file named <code>MANIFEST.MF</code>, the file is
  249. ignored and you will get a warning.</p>
  250. <h4>manifest</h4>
  251. <p>The manifest nested element allows the manifest for the Jar file to
  252. be provided inline in the build file rather than in an external
  253. file. This element is identical to the
  254. <a href="manifest.html">manifest</a> task, but the file and mode
  255. attributes must be omitted.</p>
  256. <p>
  257. If both an inline manifest and an external file are both specified, the
  258. manifests are merged.
  259. </p>
  260. <p>When using inline manifests, the Jar task will check whether the manifest
  261. contents have changed (i.e. the manifest as specified is different in any way
  262. from the manifest that exists in the Jar, if it exists.
  263. If the manifest values have changed the jar will be updated or rebuilt, as
  264. appropriate.
  265. </p>
  266. <a name="indexjars"><h4>indexjars</h4></a>
  267. <p><em>since ant 1.6.2</em></p>
  268. <p>The nested <code>indexjars</code> element specifies a <a
  269. href="../using.html#path">PATH like structure</a>. Its content is
  270. completely ignored unless you set the index attribute of the task to
  271. true.</p>
  272. <p>The index created by this task will contain indices for the
  273. archives contained in this path, the names used for the archives
  274. depend on your manifest:</p>
  275. <ul>
  276. <li>If the generated jar's manifest contains no Class-Path
  277. attribute, the file name without any leading directory path will be
  278. used and all parts of the path will get indexed.</li>
  279. <li>If the manifest contains a Class-Path attribute, this task will
  280. try to guess which part of the Class-Path belongs to a given
  281. archive. If it cannot guess a name, the archive will be skipped,
  282. otherwise the name listed inside the Class-Path attribute will be
  283. used.</li>
  284. </ul>
  285. <p>This task will not create any index entries for archives that are
  286. empty or only contain files inside the META-INF directory.</p>
  287. <a name="service"><h4>service</h4></a>
  288. <p><em>since ant 1.7.0</em></p>
  289. <p>
  290. The nested <code>service</code> element specifies a service.
  291. Services are described by
  292. <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider</a>.
  293. The approach is to have providers JARs include files named by the service
  294. provided, for example,
  295. META-INF/services/javax.script.ScriptEngineFactory
  296. which can include implementation class names, one per line (usually just one per JAR).
  297. The name of the
  298. service is set by the "type" attribute. The classname implementing
  299. the service is the the "provider" attribute, or it one wants to
  300. specify a number of classes that implement the service, by
  301. "provider" nested elements.
  302. </p>
  303. <p>
  304. <table border="1" cellpadding="2" cellspacing="0">
  305. <tr>
  306. <td valign="top"><b>Attribute</b></td>
  307. <td valign="top"><b>Description</b></td>
  308. <td align="center" valign="top"><b>Required</b></td>
  309. </tr>
  310. <tr>
  311. <td valign="top">type</td>
  312. <td valign="top">The name of the service.</td>
  313. <td valign="top" align="center">Yes</td>
  314. </tr>
  315. <tr>
  316. <td valign="top">provider</td>
  317. <td valign="top">
  318. The classname of the class implemening the service.
  319. </td>
  320. <td valign="top" align="center">Yes, unless there is a nested
  321. <code>&lt;provider&gt;</code> element.</td>
  322. </tr>
  323. </table>
  324. <p>
  325. The provider classname is specified either by the "provider" attribute, or
  326. by a nested &lt;provider&gt; element, which has a single "classname" attribute.
  327. If a JAR file has more that one implementation of the service, a number of
  328. nested &lt;provider&gt; elements may be used.
  329. </p>
  330. <h3>Examples</h3>
  331. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot; basedir=&quot;${build}/classes&quot;/&gt;</pre>
  332. <p>jars all files in the <code>${build}/classes</code> directory into a file
  333. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.</p>
  334. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;
  335. basedir=&quot;${build}/classes&quot;
  336. excludes=&quot;**/Test.class&quot;
  337. /&gt;</pre>
  338. <p>jars all files in the <code>${build}/classes</code> directory into a file
  339. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Files
  340. with the name <code>Test.class</code> are excluded.</p>
  341. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;
  342. basedir=&quot;${build}/classes&quot;
  343. includes=&quot;mypackage/test/**&quot;
  344. excludes=&quot;**/Test.class&quot;
  345. /&gt;</pre>
  346. <p>jars all files in the <code>${build}/classes</code> directory into a file
  347. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Only
  348. files under the directory <code>mypackage/test</code> are used, and files with
  349. the name <code>Test.class</code> are excluded.</p>
  350. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;&gt;
  351. &lt;fileset dir=&quot;${build}/classes&quot;
  352. excludes=&quot;**/Test.class&quot;
  353. /&gt;
  354. &lt;fileset dir=&quot;${src}/resources&quot;/&gt;
  355. &lt;/jar&gt;</pre>
  356. <p>jars all files in the <code>${build}/classes</code> directory and also
  357. in the <code>${src}/resources</code> directory together into a file
  358. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.
  359. Files with the name <code>Test.class</code> are excluded.
  360. If there are files such as <code>${build}/classes/mypackage/MyClass.class</code>
  361. and <code>${src}/resources/mypackage/image.gif</code>, they will appear
  362. in the same directory in the JAR (and thus be considered in the same package
  363. by Java).</p>
  364. <pre> &lt;jar destfile=&quot;test.jar&quot; basedir=&quot;.&quot;&gt;
  365. &lt;include name=&quot;build&quot;/&gt;
  366. &lt;manifest&gt;
  367. &lt;!-- Who is building this jar? --&gt;
  368. &lt;attribute name=&quot;Built-By&quot; value=&quot;${user.name}&quot;/&gt;
  369. &lt;!-- Information about the program itself --&gt;
  370. &lt;attribute name=&quot;Implementation-Vendor&quot; value=&quot;ACME inc.&quot;/&gt;
  371. &lt;attribute name=&quot;Implementation-Title&quot; value=&quot;GreatProduct&quot;/&gt;
  372. &lt;attribute name=&quot;Implementation-Version&quot; value=&quot;1.0.0beta2&quot;/&gt;
  373. &lt;!-- details --&gt;
  374. &lt;section name=&quot;common/class1.class&quot;&gt;
  375. &lt;attribute name=&quot;Sealed&quot; value=&quot;false&quot;/&gt;
  376. &lt;/section&gt;
  377. &lt;/manifest&gt;
  378. &lt;/jar&gt;</pre>
  379. <p>
  380. This is an example of an inline manifest specification including the version of the build
  381. program (Implementation-Version). Note that the Built-By attribute will take the value of the Ant
  382. property ${user.name}. The manifest produced by the above would look like this:
  383. </p>
  384. <pre><code>Manifest-Version: 1.0
  385. Built-By: conor
  386. Implementation-Vendor: ACME inc.
  387. Implementation-Title: GreatProduct
  388. Implementation-Version: 1.0.0beta2
  389. Created-By: Apache Ant 1.7.0
  390. Name: common/MyClass.class
  391. Sealed: false</code></pre>
  392. <p>
  393. The following shows how to create a jar file specifing a service
  394. with an implementation of the JDK6 scripting interface:
  395. </p>
  396. <blockquote><pre>&lt;jar jarfile="pinky.jar"&gt;
  397. &lt;fileset dir="build/classes"/&gt;
  398. &lt;service type="javax.script.ScriptEngineFactory"
  399. provider="org.acme.PinkyLanguage"/&gt;
  400. &lt;/jar&gt;
  401. </pre></blockquote>
  402. <p>
  403. The following shows how to create a jar file specifing a service
  404. with two implementations of the JDK6 scripting interface:
  405. </p>
  406. <blockquote><pre>
  407. &lt;jar jarfile="pinkyandbrain.jar"&gt;
  408. &lt;fileset dir="classes"/&gt;
  409. &lt;service type="javax.script.ScriptEngineFactory"&gt;
  410. &lt;provider classname="org.acme.PinkyLanguage"/&gt;
  411. &lt;provider classname="org.acme.BrainLanguage"/&gt;
  412. &lt;/service&gt;
  413. &lt;/jar&gt;
  414. </pre></blockquote>
  415. </body>
  416. </html>