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

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