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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Apache Ant User Manual</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 attributes from the zip task are also available
  29. 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.
  32. You should not include <samp>META-INF/MANIFEST.MF</samp> in your set of files.</p>
  33. <p>The <code>update</code> parameter controls what happens if the
  34. JAR 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>.</p>
  38. <p>(The Jar task is a shortcut for specifying the manifest file of a JAR file.
  39. The same thing can be accomplished by using the <i>fullpath</i>
  40. attribute of a zipfileset in a Zip task. The one difference is that if the
  41. <i>manifest</i> attribute is not specified, the Jar task will
  42. include an empty one for you.)</p>
  43. <p>Manifests are processed by the Jar task according to the
  44. <a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html">Jar file specification.</a>
  45. Note in particular that this may result in manifest lines greater than 72 bytes
  46. being wrapped and continued on the next line.
  47. </p>
  48. <h3>Parameters</h3>
  49. <table border="1" cellpadding="2" cellspacing="0">
  50. <tr>
  51. <td valign="top"><b>Attribute</b></td>
  52. <td valign="top"><b>Description</b></td>
  53. <td align="center" valign="top"><b>Required</b></td>
  54. </tr>
  55. <tr>
  56. <td valign="top">file</td>
  57. <td valign="top">the jar-file to create.</td>
  58. <td valign="top" align="center">Yes</td>
  59. </tr>
  60. <tr>
  61. <td valign="top">basedir</td>
  62. <td valign="top">the directory from which to jar the files.</td>
  63. <td valign="top" align="center">No</td>
  64. </tr>
  65. <tr>
  66. <td valign="top">compress</td>
  67. <td valign="top">Not only store data but also compress them, defaults to true</td>
  68. <td align="center" valign="top">No</td>
  69. </tr>
  70. <tr>
  71. <td valign="top">encoding</td>
  72. <td valign="top">The character encoding to use for filenames
  73. inside the archive. Defaults to UTF8. <strong>It is not
  74. recommended to change this value as the created archive will most
  75. likely be unreadable for Java otherwise.</strong></td>
  76. <td align="center" valign="top">No</td>
  77. </tr>
  78. <tr>
  79. <td valign="top">filesonly</td>
  80. <td valign="top">Store only file entries, defaults to false</td>
  81. <td align="center" valign="top">No</td>
  82. </tr>
  83. <tr>
  84. <td valign="top">includes</td>
  85. <td valign="top">comma separated list of patterns of files that must be
  86. included. All files are included when omitted.</td>
  87. <td valign="top" align="center">No</td>
  88. </tr>
  89. <tr>
  90. <td valign="top">includesfile</td>
  91. <td valign="top">the name of a file. Each line of this file is
  92. taken to be an include pattern</td>
  93. <td valign="top" align="center">No</td>
  94. </tr>
  95. <tr>
  96. <td valign="top">excludes</td>
  97. <td valign="top">comma separated list of patterns of files that must be
  98. excluded. No files (except default excludes) are excluded when omitted.</td>
  99. <td valign="top" align="center">No</td>
  100. </tr>
  101. <tr>
  102. <td valign="top">excludesfile</td>
  103. <td valign="top">the name of a file. Each line of this file is
  104. taken to be an exclude pattern</td>
  105. <td valign="top" align="center">No</td>
  106. </tr>
  107. <tr>
  108. <td valign="top">defaultexcludes</td>
  109. <td valign="top">indicates whether default excludes should be used or not
  110. (&quot;yes&quot;/&quot;no&quot;). Default excludes are used when omitted.</td>
  111. <td valign="top" align="center">No</td>
  112. </tr>
  113. <tr>
  114. <td valign="top">manifest</td>
  115. <td valign="top">the manifest file to use.</td>
  116. <td valign="top" align="center">No</td>
  117. </tr>
  118. <tr>
  119. <td valign="top">update</td>
  120. <td valign="top">indicates whether to update or overwrite
  121. the destination file if it already exists.</td>
  122. <td valign="top" align="center">No</td>
  123. </tr>
  124. </table>
  125. <h3>Nested elements</h3>
  126. <h4>metainf</h4>
  127. <p>The nested <code>metainf</code> element specifies a <a
  128. href="../CoreTypes/fileset.html">FileSet</a>. All files included in this fileset will
  129. end up in the <code>META-INF</code> directory of the jar file. If this
  130. fileset includes a file named <code>MANIFEST.MF</code>, the file is
  131. ignored and you will get a warning.</p>
  132. <h4>Manifest</h4>
  133. <p>
  134. The manifest nested element allows the manifest for the Jar file to be
  135. provided inline in the build file rather than in an external file. This
  136. makes it easy to produce Jar manifests which take values from Ant properties.
  137. If both an inline manifest and an external file are both specified, the
  138. manifests are merged.
  139. </p>
  140. <p>
  141. The manifest element supports nested elements to reflect the structure of
  142. manifests, according to the
  143. <a href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html">Jar file
  144. specification</a>. Specifically, a manifest
  145. element consists of a set of attributes and sections. These sections in turn
  146. may contain attributes.
  147. </p>
  148. <p>When using inline manifests, the Jar task will check whether the build
  149. file is more recent that the Jar file when deciding whether to rebuild the
  150. Jar. This will not take into account property file changes which may affect
  151. the resulting Jar.
  152. </p>
  153. <p> The manifest element itself does not support any attributes. It serves
  154. merely as a container for the attribute and section elements that make up the
  155. manifest. The attributes of those elements are as follows:
  156. </p>
  157. <p>
  158. <b>Section</b>
  159. </p>
  160. <p>
  161. <table border="1" cellpadding="2" cellspacing="0">
  162. <tr>
  163. <td valign="top"><b>Attribute</b></td>
  164. <td valign="top"><b>Description</b></td>
  165. <td align="center" valign="top"><b>Required</b></td>
  166. </tr>
  167. <tr>
  168. <td valign="top">Name</td>
  169. <td valign="top">The name of the section</td>
  170. <td valign="top" align="center">Yes</td>
  171. </tr>
  172. </table>
  173. </p>
  174. <p>
  175. <b>Attribute</b>
  176. </p>
  177. <p>
  178. <table border="1" cellpadding="2" cellspacing="0">
  179. <tr>
  180. <td valign="top"><b>Attribute</b></td>
  181. <td valign="top"><b>Description</b></td>
  182. <td align="center" valign="top"><b>Required</b></td>
  183. </tr>
  184. <tr>
  185. <td valign="top">Name</td>
  186. <td valign="top">The name of the attribute</td>
  187. <td valign="top" align="center">Yes</td>
  188. </tr>
  189. <tr>
  190. <td valign="top">Value</td>
  191. <td valign="top">The value of the attribute</td>
  192. <td valign="top" align="center">Yes</td>
  193. </tr>
  194. </table>
  195. </p>
  196. <h3>Examples</h3>
  197. <pre> &lt;jar file=&quot;${dist}/lib/app.jar&quot; basedir=&quot;${build}/classes&quot;/&gt;</pre>
  198. <p>jars all files in the <code>${build}/classes</code> directory into a file
  199. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.</p>
  200. <pre> &lt;jar file=&quot;${dist}/lib/app.jar&quot;
  201. basedir=&quot;${build}/classes&quot;
  202. excludes=&quot;**/Test.class&quot;
  203. /&gt;</pre>
  204. <p>jars all files in the <code>${build}/classes</code> directory into a file
  205. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Files
  206. with the name <code>Test.class</code> are excluded.</p>
  207. <pre> &lt;jar file=&quot;${dist}/lib/app.jar&quot;
  208. basedir=&quot;${build}/classes&quot;
  209. includes=&quot;mypackage/test/**&quot;
  210. excludes=&quot;**/Test.class&quot;
  211. /&gt;</pre>
  212. <p>jars all files in the <code>${build}/classes</code> directory into a file
  213. called <code>app.jar</code> in the <code>${dist}/lib</code> directory. Only
  214. files under the directory <code>mypackage/test</code> are used, and files with
  215. the name <code>Test.class</code> are excluded.</p>
  216. <pre> &lt;jar file=&quot;${dist}/lib/app.jar&quot;&gt;
  217. &lt;fileset dir=&quot;${build}/classes&quot;
  218. excludes=&quot;**/Test.class&quot;
  219. /&gt;
  220. &lt;fileset dir=&quot;${src}/resources&quot;/&gt;
  221. &lt;/jar&gt;</pre>
  222. <p>jars all files in the <code>${build}/classes</code> directory and also
  223. in the <code>${src}/resources</code> directory together into a file
  224. called <code>app.jar</code> in the <code>${dist}/lib</code> directory.
  225. Files with the name <code>Test.class</code> are excluded.
  226. If there are files such as <code>${build}/classes/mypackage/MyClass.class</code>
  227. and <code>${src}/resources/mypackage/image.gif</code>, they will appear
  228. in the same directory in the JAR (and thus be considered in the same package
  229. by Java).</p>
  230. <pre> &lt;jar jarfile=&quot;test.jar&quot; basedir=&quot;.&quot;&gt;
  231. &lt;include name=&quot;build&quot;/&gt;
  232. &lt;manifest&gt;
  233. &lt;attribute name=&quot;Built-By&quot; value=&quot;${user.name}&quot;/&gt;
  234. &lt;section name=&quot;common/class1.class&quot;&gt;
  235. &lt;attribute name=&quot;Sealed&quot; value=&quot;false&quot;/&gt;
  236. &lt;/section&gt;
  237. &lt;/manifest&gt;
  238. &lt;/jar&gt;</pre>
  239. <p>
  240. This is an example of an inline manifest specification. Note that the Built-By
  241. attribute will take the value of the Ant property ${user.name}. The manifest
  242. produced by the above would look like this:
  243. </p>
  244. <pre><code>Manifest-Version: 1.0
  245. Built-By: conor
  246. Created-By: Apache Ant 1.5alpha
  247. Name: common/class1.class
  248. Sealed: false</code></pre>
  249. <hr>
  250. <p align="center">Copyright &copy; 2000,2001 Apache Software Foundation. All rights
  251. Reserved.</p>
  252. </body>
  253. </html>