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 KiB

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