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

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