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.

depend.html 9.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Depend Task</title>
  20. </head>
  21. <body>
  22. <h2>Depend</h2>
  23. <p>A task to manage Java class file dependencies.</p>
  24. <h3>Description</h3>
  25. <p>The <code>depend</code> task works by determining which classes are out of date with respect to
  26. their source and then removing the class files of any other classes which depend on the out-of-date
  27. classes.</p>
  28. <p>To determine the class dependencies, the <code>depend</code> task analyzes the class files of all
  29. class files passed to it. The task does not parse your source code in any way but relies upon the
  30. class references encoded into the class files by the compiler. This is generally faster than parsing
  31. the Java source files.</p>
  32. <p>To learn more about how this information is obtained from the class files, please refer
  33. to <a href="https://docs.oracle.com/javase/specs/" target="_top">the Java Virtual Machine
  34. Specification</a></p>
  35. <p>Since a class' dependencies only change when the class itself changes, the
  36. <code>depend</code> task is able to cache dependency information. Only those class files which have
  37. changed will have their dependency information re-analysed. Note that if you change a class'
  38. dependencies by changing the source, it will be recompiled anyway. You can examine the dependency
  39. files created to understand the dependencies of your classes. Please do not rely, however, on the
  40. format of the information, as it may change in a later release.</p>
  41. <p>Once <code>depend</code> discovers all of the class dependencies, it &quot;inverts&quot; this
  42. relation to determine, for each class, which other classes are dependent upon it. This
  43. &quot;affects&quot; list is used to discover which classes are invalidated by the out of date
  44. class. The class files of the invalidated classes are removed, triggering the compilation of the
  45. affected classes.</p>
  46. <p>The <code>depend</code> task supports an attribute, <var>closure</var>, which controls
  47. whether <code>depend</code> will only consider direct class-class relationships or whether it will
  48. also consider transitive, indirect relationships. For example, say there are three classes, A, which
  49. depends on B, which in-turn depends on C. Now say that class C is out of
  50. date. Without <var>closure</var>, only class B would be removed
  51. by <code>depend</code>. With <var>closure</var> set, class A would also be removed. Normally direct
  52. relationships are sufficient&mdash;it is unusual for a class to depend on another without having a
  53. direct relationship. With <var>closure</var> set, you will notice that <code>depend</code> typically
  54. removes far more class files.</p>
  55. <p>The <var>classpath</var> attribute for <code>&lt;depend&gt;</code> is optional. If it is
  56. present, <code>depend</code> will check class dependencies against classes and jars on this
  57. classpath. Any classes which depend on an element from this classpath and which are older than that
  58. element will be deleted. A typical example where you would use this facility would be where you are
  59. building a utility jar and want to make sure classes which are out of date with respect to this jar
  60. are rebuilt. In this classpath, you should <strong>not</strong> include jars that you do not expect
  61. to change, such as the JDK runtime jar or third party jars, since doing so will just slow down the
  62. dependency check. This means that if you do use a classpath for the <code>depend</code> task it may
  63. be different from the classpath necessary to actually compile your code.</p>
  64. <h3>Performance</h3>
  65. <p>The performance of the <code>depend</code> task is dependent on a number of factors such as class
  66. relationship complexity and how many class files are out of date. The decision about whether it is
  67. cheaper to just recompile all classes or to use the <code>depend</code> task will depend on the size
  68. of your project and how interrelated your classes are.</p>
  69. <h3>Limitations</h3>
  70. <p>There are some source dependencies which <code>depend</code> will not detect:</p>
  71. <ul>
  72. <li>If the Java compiler optimizes away a class relationship, there can be a source dependency
  73. without a class dependency.</li>
  74. <li>Non-public classes cause two problems. Firstly, depend cannot relate the class file to a source
  75. file. In the future this may be addressed using the source file attribute in the
  76. classfile. Secondly, neither <code>depend</code> nor the compiler tasks can detect when a non-public
  77. class is missing. Inner classes are handled by the <code>depend</code> task.</li>
  78. </ul>
  79. <p>The most obvious example of these limitations is that the task can't tell which classes to
  80. recompile when a constant primitive data type exported by other classes is changed. For example, a
  81. change in the definition of something like</p>
  82. <pre>
  83. public final class Constants {
  84. public final static boolean DEBUG=false;
  85. }</pre>
  86. <p>will not be picked up by other classes.</p>
  87. <h3>Parameters</h3>
  88. <table class="attr">
  89. <tr>
  90. <th scope="col">Attribute</th>
  91. <th scope="col">Description</th>
  92. <th scope="col">Required</th>
  93. </tr>
  94. <tr>
  95. <td>srcDir</td>
  96. <td>This is the directory where the source exists. <code>depend</code> will examine this to
  97. determine which classes are out of date. If you use multiple source directories you can pass
  98. this attribute a path of source directories.</td>
  99. <td>Yes</td>
  100. </tr>
  101. <tr>
  102. <td>destDir</td>
  103. <td>This is the root directory of the class files which will be analysed.</td>
  104. <td>No; defaults to <var>srcdir</var></td>
  105. </tr>
  106. <tr>
  107. <td>cache</td>
  108. <td>This is a directory in which <code>depend</code> can store and retrieve dependency
  109. information.</td>
  110. <td>No; defaults to no cache</td>
  111. </tr>
  112. <tr>
  113. <td>closure</td>
  114. <td>This attribute controls whether <code>depend</code> only removes classes which directly
  115. depend on out of date classes. If this is set to <q>true</q>, <code>depend</code> will
  116. traverse the class dependency graph deleting all affected classes.</td>
  117. <td>No; defaults to <q>false</q></td>
  118. </tr>
  119. <tr>
  120. <td>dump</td>
  121. <td>If true the dependency information will be written to the debug level log</td>
  122. <td>No; default is <q>false</q></td>
  123. </tr>
  124. <tr>
  125. <td>classpath</td>
  126. <td>The classpath containing jars and classes for which <code>&lt;depend&gt;</code> should also
  127. check dependencies</td>
  128. <td>No</td>
  129. </tr>
  130. <tr>
  131. <td>warnOnRmiStubs</td>
  132. <td>Flag to disable warnings about files that look like <kbd>rmic</kbd> generated
  133. stub/skeleton classes and have no <samp>.java</samp> source. Useful when doing RMI
  134. development.</td>
  135. <td>No; default <q>true</q></td>
  136. </tr>
  137. </table>
  138. <h3>Parameters specified as nested elements</h3>
  139. <p>The <code>depend</code> task's <var>classpath</var> attribute is
  140. a <a href="../using.html#path">path-like structure</a> and can also be set via a
  141. nested <code>&lt;classpath&gt;</code> element.</p>
  142. <p>Additionally, this task forms an implicit <a href="../Types/fileset.html">FileSet</a> and
  143. supports most attributes of <code>&lt;fileset&gt;</code> (<var>dir</var> becomes <var>srcdir</var>),
  144. as well as the nested <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code>,
  145. and <code>&lt;patternset&gt;</code> elements.</p>
  146. <h3>Examples</h3>
  147. <p>Remove any classes in the <samp>${build.classes}</samp> directory that depend on out-of-date
  148. classes. Classes are considered out-of-date with respect to the source in
  149. the <samp>${java.dir}</samp> directory, using the same mechanism as the <code>&lt;javac&gt;</code>
  150. task. In this example, the <code>&lt;depend&gt;</code> task caches its dependency information in
  151. the <samp>depcache</samp> directory.</p>
  152. <pre>
  153. &lt;depend srcdir=&quot;${java.dir}&quot;
  154. destdir=&quot;${build.classes}&quot;
  155. cache=&quot;depcache&quot;
  156. closure=&quot;yes&quot;/&gt;</pre>
  157. <p>Do the same as the previous example, but explicitly include all <samp>.java</samp> files, except
  158. those that match the list given in <samp>${java.dir}/build_excludes</samp>.</p>
  159. <pre>
  160. &lt;depend srcdir=&quot;${java.dir}&quot; destdir=&quot;${build.classes}&quot;
  161. cache=&quot;depcache&quot; closure=&quot;yes&quot;&gt;
  162. &lt;include name=&quot;**/*.java&quot;/&gt;
  163. &lt;excludesfile name=&quot;${java.dir}/build_excludes&quot;/&gt;
  164. &lt;/depend&gt;</pre>
  165. </body>
  166. </html>