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.

projecthelper.html 6.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="stylesheets/style.css">
  19. <title>The Apache Ant frontend: ProjectHelper</title>
  20. </head>
  21. <body>
  22. <h1>The Apache Ant frontend: ProjectHelper</h1>
  23. <h2 id="definition">What is a ProjectHelper?</h2>
  24. <p>
  25. The <code class="code">ProjectHelper</code> in Apache Ant is responsible for parsing the build file
  26. and creating Java instances representing the build workflow. It also signals which kind of file it
  27. can parse, and which file name it expects as default input file.
  28. </p>
  29. <p>
  30. Ant's default <code class="code">ProjectHelper</code>
  31. (<code class="code">org.apache.tools.ant.helper.ProjectHelper2</code>) parses the
  32. usual <code>build.xml</code> files. And if no build file is specified on the command line, it will
  33. expect to find a file named <code>build.xml</code>.
  34. </p>
  35. <p>
  36. The immediate benefit of a such abstraction it that it is possible to make Ant understand other kind
  37. of descriptive languages than XML. Some experiments have been done around a pure Java frontend, and
  38. a Groovy one too (ask the dev mailing list for further info about these).
  39. </p>
  40. <p>
  41. <em>Since Ant 1.8.2</em>, the <a href="Tasks/import.html">import</a> task will also try to use the
  42. proper helper to parse the imported file. So it is possible to write different build files in
  43. different languages and have them import each other.
  44. </p>
  45. <h2 id="repository">How is Ant is selecting the proper ProjectHelper</h2>
  46. <p>
  47. Ant knows about several implementations of <code class="code">ProjectHelper</code> and has to decide
  48. which to use for each build file.
  49. </p>
  50. <p>
  51. At startup Ant lists the all implementations found and keeps them in the same order they've been
  52. found in an internal 'repository':
  53. </p>
  54. <ul>
  55. <li>the first to be searched for is the one declared by the system
  56. property <code class="code">org.apache.tools.ant.ProjectHelper</code>
  57. (see <a href="running.html#sysprops">Java System Properties</a>);</li>
  58. <li>then it searches with its class loader for a <code>ProjectHelper</code> service declarations
  59. in the <samp>META-INF</samp>: it searches in the classpath for a
  60. file <samp>META-INF/services/org.apache.tools.ant.ProjectHelper</samp>. This file will just
  61. contain the fully qualified name of the implementation
  62. of <code class="code">ProjectHelper</code> to instantiate;</li>
  63. <li>it will also search with the system class loader for <code class="code">ProjectHelper</code>
  64. service declarations in the <samp>META-INF</samp>;</li>
  65. <li>last but not least it will add its default <code class="code">ProjectHelper</code> that can
  66. parse classical <samp>build.xml</samp> files.</li>
  67. </ul>
  68. <p>
  69. In case of an error while trying to instantiate a <code class="code">ProjectHelper</code>, Ant will
  70. log an error but won't stop. If you want further debugging info about
  71. the <code class="code">ProjectHelper</code> internal 'repository', use the <strong>system</strong>
  72. property <code>ant.project-helper-repo.debug</code> and set it to <q>true</q>; the full stack trace
  73. will then also be printed.
  74. </p>
  75. <p>
  76. When Ant is expected to parse a file, it will ask the <code class="code">ProjectHelper</code>
  77. repository to find an implementation that will be able to parse the input file. Actually it will
  78. just iterate over the ordered list and the first implementation that returns <q>true</q>
  79. to <code class="code">supportsBuildFile(File buildFile)</code> will be selected.
  80. </p>
  81. <p>
  82. When Ant is started and no input file has been specified, it will search for a default input
  83. file. It will iterate over list of <code class="code">ProjectHelper</code>s and will select the
  84. first one that expects a default file that actually exist.
  85. </p>
  86. <h2 id="writing">Writing your own ProjectHelper</h2>
  87. <p>
  88. The class <code class="code">org.apache.tools.ant.ProjectHelper</code> is the API expected to be
  89. implemented. So write your own <code class="code">ProjectHelper</code> by extending that abstract
  90. class. You are then expected to implement at least the function <code class="code">parse(Project
  91. project, Object source)</code>. Note also that your implementation will be instantiated by Ant, and
  92. it is expecting a default constructor with no arguments.
  93. </p>
  94. <p>
  95. There are some functions that will help you define what your helper is capable of and what is is
  96. expecting:
  97. </p>
  98. <ul>
  99. <li><code class="code">getDefaultBuildFile()</code>: defines which file name is expected if none
  100. provided</li>
  101. <li><code class="code">supportsBuildFile(File buildFile)</code>: defines if your parser can parse
  102. the input file</li>
  103. <li><code class="code">canParseAntlibDescriptor(URL url)</code>: whether your implementation is
  104. capable of parsing a given Antlib descriptor. The base class returns <q>false</q></li>
  105. <li><code class="code">parseAntlibDescriptor(Project containingProject, URL source)</code>:
  106. invoked to actually parse the Antlib descriptor if your implementation returned <q>true</q> for
  107. the previous method.</li>
  108. </ul>
  109. <p>
  110. Now that you have your implementation ready, you have to declare it to Ant. Three solutions here:
  111. </p>
  112. <ul>
  113. <li>use the system property <code class="code">org.apache.tools.ant.ProjectHelper</code> (see also
  114. the <a href="running.html#sysprops">Java System Properties</a>);</li>
  115. <li>use the service file in <samp>META-INF</samp>: in the jar you will build with your
  116. implementation, add a file <samp>META-INF/services/org.apache.tools.ant.ProjectHelper</samp>.
  117. And then in this file just put the fully qualified name of your implementation</li>
  118. <li>use the <a href="Tasks/projecthelper.html">projecthelper</a> task (<em>since Ant 1.8.2</em>)
  119. which will install dynamically a helper in the internal helper 'repository'. Then your helper
  120. can be used on the next call to the <a href="Tasks/import.html">import</a> task.</li>
  121. </ul>
  122. </body>
  123. </html>