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

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