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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Jar Task</title>
  5. </head>
  6. <body>
  7. <h2><a name="jar">Jar</a></h2>
  8. <h3>Description</h3>
  9. <p>Jars a set of files.</p>
  10. <p>The <i>basedir</i> attribute is the reference directory from where to jar.</p>
  11. <p>Note that file permissions will not be stored in the resulting jarfile.</p>
  12. <p>It is possible to refine the set of files that are being jarred. This can be
  13. done with the <i>includes</i>, <i>includesfile</i>, <i>excludes</i>, <i>excludesfile</i> and <i>defaultexcludes</i>
  14. attributes. With the <i>includes</i> or <i>includesfile</i> attribute you specify the files you want to
  15. have included by using patterns. The <i>exclude</i> or <i>excludesfile</i> attribute is used to specify
  16. the files you want to have excluded. This is also done with patterns. And
  17. finally with the <i>defaultexcludes</i> attribute, you can specify whether you
  18. want to use default exclusions or not. See the section on <a
  19. href="../dirtasks.html#directorybasedtasks">directory based tasks</a>, on how the
  20. inclusion/exclusion of files works, and how to write patterns.</p>
  21. <p>This task forms an implicit <a href="../CoreTypes/fileset.html">FileSet</a> and
  22. supports all attributes of <code>&lt;fileset&gt;</code>
  23. (<code>dir</code> becomes <code>basedir</code>) as well as the nested
  24. <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code> and
  25. <code>&lt;patternset&gt;</code> elements.</p>
  26. <p>You can also use nested file sets for more flexibility, and specify
  27. multiple ones to merge together different trees of files into one JAR.
  28. The extended fileset and groupfileset child elements from the zip task are
  29. also available in the jar task.
  30. See the <a href="zip.html">Zip</a> task for more details and examples.</p>
  31. <p>If the manifest is omitted, a simple one will be supplied by Ant.</p>
  32. <p>The <code>update</code> parameter controls what happens if the JAR
  33. file already exists. When set to <code>yes</code>, the JAR file is
  34. updated with the files specified. When set to <code>no</code> (the
  35. default) the JAR file is overwritten. An example use of this is
  36. provided in the <a href="zip.html">Zip task documentation</a>. Please
  37. note that ZIP files store file modification times with a granularity
  38. of two seconds. If a file is less than two seconds newer than the
  39. entry in the archive, Ant will not consider it newer.</p>
  40. <p>(The Jar task is a shortcut for specifying the manifest file of a JAR file.
  41. The same thing can be accomplished by using the <i>fullpath</i>
  42. attribute of a zipfileset in a Zip task. The one difference is that if the
  43. <i>manifest</i> attribute is not specified, the Jar task will
  44. include an empty one for you.)</p>
  45. <p>Manifests are processed by the Jar task according to the
  46. <a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html">Jar file specification.</a>
  47. Note in particular that this may result in manifest lines greater than 72 bytes
  48. being wrapped and continued on the next line.
  49. </p>
  50. <p><b>Please note that the zip format allows multiple files of the same
  51. fully-qualified name to exist within a single archive. This has been
  52. documented as causing various problems for unsuspecting users. If you wish
  53. to avoid this behavior you must set the <code>duplicate</code> attribute
  54. to a value other than its default, <code>&quot;add&quot;</code>.</b></p>
  55. <h3>Parameters</h3>
  56. <table border="1" cellpadding="2" cellspacing="0">
  57. <tr>
  58. <td valign="top"><b>Attribute</b></td>
  59. <td valign="top"><b>Description</b></td>
  60. <td align="center" valign="top"><b>Required</b></td>
  61. </tr>
  62. <tr>
  63. <td valign="top">destfile</td>
  64. <td valign="top">the JAR file to create.</td>
  65. <td valign="top" align="center">Yes</td>
  66. </tr>
  67. <tr>
  68. <td valign="top">basedir</td>
  69. <td valign="top">the directory from which to jar the files.</td>
  70. <td valign="top" align="center">No</td>
  71. </tr>
  72. <tr>
  73. <td valign="top">compress</td>
  74. <td valign="top">Not only store data but also compress them,
  75. defaults to true. Unless you set the <em>keepcompression</em>
  76. attribute to false, this will apply to the entire archive, not
  77. only the files you've added while updating.</td>
  78. <td align="center" valign="top">No</td>
  79. </tr>
  80. <tr>
  81. <td valign="top">keepcompression</td>
  82. <td valign="top">For entries coming from existing archives (like
  83. nested <em>zipfileset</em>s or while updating the archive), keep
  84. the compression as it has been originally instead of using the
  85. <em>compress</em> attribute. Defaults false. <em>Since Ant
  86. 1.6</em></td>
  87. <td align="center" valign="top">No</td>
  88. </tr>
  89. <tr>
  90. <td valign="top">encoding</td>
  91. <td valign="top">The character encoding to use for filenames
  92. inside the archive. Defaults to UTF8. <strong>It is not
  93. recommended to change this value as the created archive will most
  94. likely be unreadable for Java otherwise.</strong></td>
  95. <td align="center" valign="top">No</td>
  96. </tr>
  97. <tr>
  98. <td valign="top">filesonly</td>
  99. <td valign="top">Store only file entries, defaults to false</td>
  100. <td align="center" valign="top">No</td>
  101. </tr>
  102. <tr>
  103. <td valign="top">includes</td>
  104. <td valign="top">comma- or space-separated list of patterns of files that must be
  105. included. All files are included when omitted.</td>
  106. <td valign="top" align="center">No</td>
  107. </tr>
  108. <tr>
  109. <td valign="top">includesfile</td>
  110. <td valign="top">the name of a file. Each line of this file is
  111. taken to be an include pattern</td>
  112. <td valign="top" align="center">No</td>
  113. </tr>
  114. <tr>
  115. <td valign="top">excludes</td>
  116. <td valign="top">comma- or space-separated list of patterns of files that must be
  117. excluded. No files (except default excludes) are excluded when omitted.</td>
  118. <td valign="top" align="center">No</td>
  119. </tr>
  120. <tr>
  121. <td valign="top">excludesfile</td>
  122. <td valign="top">the name of a file. Each line of this file is
  123. taken to be an exclude pattern</td>
  124. <td valign="top" align="center">No</td>
  125. </tr>
  126. <tr>
  127. <td valign="top">defaultexcludes</td>
  128. <td valign="top">indicates whether default excludes should be used or not
  129. (&quot;yes&quot;/&quot;no&quot;). Default excludes are used when omitted.</td>
  130. <td valign="top" align="center">No</td>
  131. </tr>
  132. <tr>
  133. <td valign="top">manifest</td>
  134. <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>
  135. <td valign="top" align="center">No</td>
  136. </tr>
  137. <tr>
  138. <td valign="top">filesetmanifest</td>
  139. <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;.
  140. </td>
  141. <td valign="top" align="center">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">update</td>
  145. <td valign="top">indicates whether to update or overwrite
  146. the destination file if it already exists. Default is &quot;false&quot;.</td>
  147. <td valign="top" align="center">No</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">whenempty</td>
  151. <td valign="top">behavior when no files match. Valid values are &quot;fail&quot;, &quot;skip&quot;, and &quot;create&quot;. Default is &quot;skip&quot;.</td>
  152. <td valign="top" align="center">No</td>
  153. </tr>
  154. <tr>
  155. <td valign="top">duplicate</td>
  156. <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>
  157. <td valign="top" align="center">No</td>
  158. </tr>
  159. <tr>
  160. <td valign="top">index</td>
  161. <td valign="top">whether to create an <A
  162. HREF="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Index">index
  163. list</A> to speed up classloading. This is a JDK 1.3+ specific
  164. feature. Unless you specify additional jars with nested <a
  165. href="#indexjars"><code>indexjars</code></a> elements, only the
  166. contents of this jar will be included in the index. Defaults to
  167. false.</td>
  168. <td valign="top" align="center">No</td>
  169. </tr>
  170. <tr>
  171. <td valign="top">manifestencoding</td>
  172. <td valign="top">The encoding used to read the JAR manifest, when a manifest file is specified.</td>
  173. <td valign="top" align="center">No, defaults to the platform encoding.</td>
  174. </tr>
  175. <tr>
  176. <td valign="top">roundup</td>
  177. <td valign="top">Whether the file modification times will be
  178. rounded up to the next even number of seconds.<br>
  179. Zip archives store file modification times with a granularity of
  180. two seconds, so the times will either be rounded up or down. If
  181. you round down, the archive will always seem out-of-date when you
  182. rerun the task, so the default is to round up. Rounding up may
  183. lead to a different type of problems like JSPs inside a web
  184. archive that seem to be slightly more recent than precompiled
  185. pages, rendering precompilation useless.<br>
  186. Defaults to true. <em>Since Ant 1.6.2</em></td>
  187. <td align="center" valign="top">No</td>
  188. </tr>
  189. <tr>
  190. <td valign="top">level</td>
  191. <td valign="top">Non-default level at which file compression should be
  192. performed. Valid values range from 0 (no compression/fastest) to 9
  193. (maximum compression/slowest). <em>Since Ant 1.7</em></td>
  194. <td valign="top" align="center">No</td>
  195. </tr>
  196. </table>
  197. <h3>Nested elements</h3>
  198. <h4>metainf</h4>
  199. <p>The nested <code>metainf</code> element specifies a <a
  200. href="../CoreTypes/fileset.html">FileSet</a>. All files included in this fileset will
  201. end up in the <code>META-INF</code> directory of the jar file. If this
  202. fileset includes a file named <code>MANIFEST.MF</code>, the file is
  203. ignored and you will get a warning.</p>
  204. <h4>manifest</h4>
  205. <p>The manifest nested element allows the manifest for the Jar file to
  206. be provided inline in the build file rather than in an external
  207. file. This element is identical to the
  208. <a href="manifest.html">manifest</a> task, but the file and mode
  209. attributes must be omitted.</p>
  210. <p>
  211. If both an inline manifest and an external file are both specified, the
  212. manifests are merged.
  213. </p>
  214. <p>When using inline manifests, the Jar task will check whether the manifest
  215. contents have changed (i.e. the manifest as specified is different in any way
  216. from the manifest that exists in the Jar, if it exists.
  217. If the manifest values have changed the jar will be updated or rebuilt, as
  218. appropriate.
  219. </p>
  220. <a name="indexjars"><h4>indexjars</h4></a>
  221. <p><em>since ant 1.6.2</em></p>
  222. <p>The nested <code>indexjars</code> element specifies a <a
  223. href="../using.html#path">PATH like structure</a>. Its content is
  224. completely ignored unless you set the index attribute of the task to
  225. true.</p>
  226. <p>The index created by this task will contain indices for the
  227. archives contained in this path, the names used for the archioves
  228. depend on your manifest:</p>
  229. <ul>
  230. <li>If the generated jar's manifest contains no Class-Path
  231. attribute, the file name without any leading directory path will be
  232. used and all parts of the path will get indexed.</li>
  233. <li>If the manifest contains a Class-Path attribute, this task will
  234. try to guess which part of the Class-Path belongs to a given
  235. archive. If it cannot guess a name, the archive will be skipped,
  236. otherwise tha name listed inside the Class-PAth attribute will be
  237. used.</li>
  238. </ul>
  239. <p>This task will not create any index entries for archives that are
  240. empty or only contain files inside the META-INF directory.</p>
  241. <h3>Examples</h3>
  242. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot; basedir=&quot;${build}/classes&quot;/&gt;</pre>
  243. <p>jars all files in the <code>${build}/classes</code> directory into a file
  244. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.</p>
  245. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;
  246. basedir=&quot;${build}/classes&quot;
  247. excludes=&quot;**/Test.class&quot;
  248. /&gt;</pre>
  249. <p>jars all files in the <code>${build}/classes</code> directory into a file
  250. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Files
  251. with the name <code>Test.class</code> are excluded.</p>
  252. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;
  253. basedir=&quot;${build}/classes&quot;
  254. includes=&quot;mypackage/test/**&quot;
  255. excludes=&quot;**/Test.class&quot;
  256. /&gt;</pre>
  257. <p>jars all files in the <code>${build}/classes</code> directory into a file
  258. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Only
  259. files under the directory <code>mypackage/test</code> are used, and files with
  260. the name <code>Test.class</code> are excluded.</p>
  261. <pre> &lt;jar destfile=&quot;${dist}/lib/app.jar&quot;&gt;
  262. &lt;fileset dir=&quot;${build}/classes&quot;
  263. excludes=&quot;**/Test.class&quot;
  264. /&gt;
  265. &lt;fileset dir=&quot;${src}/resources&quot;/&gt;
  266. &lt;/jar&gt;</pre>
  267. <p>jars all files in the <code>${build}/classes</code> directory and also
  268. in the <code>${src}/resources</code> directory together into a file
  269. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.
  270. Files with the name <code>Test.class</code> are excluded.
  271. If there are files such as <code>${build}/classes/mypackage/MyClass.class</code>
  272. and <code>${src}/resources/mypackage/image.gif</code>, they will appear
  273. in the same directory in the JAR (and thus be considered in the same package
  274. by Java).</p>
  275. <pre> &lt;jar destfile=&quot;test.jar&quot; basedir=&quot;.&quot;&gt;
  276. &lt;include name=&quot;build&quot;/&gt;
  277. &lt;manifest&gt;
  278. &lt;attribute name=&quot;Built-By&quot; value=&quot;${user.name}&quot;/&gt;
  279. &lt;section name=&quot;common/class1.class&quot;&gt;
  280. &lt;attribute name=&quot;Sealed&quot; value=&quot;false&quot;/&gt;
  281. &lt;/section&gt;
  282. &lt;/manifest&gt;
  283. &lt;/jar&gt;</pre>
  284. <p>
  285. This is an example of an inline manifest specification. Note that the Built-By
  286. attribute will take the value of the Ant property ${user.name}. The manifest
  287. produced by the above would look like this:
  288. </p>
  289. <pre><code>Manifest-Version: 1.0
  290. Built-By: conor
  291. Created-By: Apache Ant 1.5alpha
  292. Name: common/class1.class
  293. Sealed: false</code></pre>
  294. <hr>
  295. <p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation. All rights
  296. Reserved.</p>
  297. </body>
  298. </html>