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.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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>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
  27. kind of file it can parse, and which file name it expects as default input file.
  28. </p>
  29. <p>
  30. Ant's default <code>ProjectHelper</code>
  31. (<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
  33. line, it will 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
  37. understand other kind of descriptive languages than XML. Some experiments have been
  38. done around a pure Java frontend, and a Groovy one too (ask the dev mailing list for
  39. further info about these).
  40. </p>
  41. <p>
  42. <em>Since Ant 1.8.2</em>, the <a href="Tasks/import.html">import</a> task will also
  43. try to use the proper helper to parse the imported file. So it is possible to write
  44. different build files in different languages and have them import each other.
  45. </p>
  46. <h2 id="repository">How is Ant is selecting the proper ProjectHelper</h2>
  47. <p>
  48. Ant knows about several implementations of <code>ProjectHelper</code> and has to
  49. decide which to use for each build file.
  50. </p>
  51. <p>
  52. At startup Ant lists the all implementations found and keeps them in the same order
  53. they've been found in an internal 'repository':
  54. </p>
  55. <ul>
  56. <li>the first to be searched for is the one declared by the system property
  57. <code>org.apache.tools.ant.ProjectHelper</code> (see
  58. <a href="running.html#sysprops">Java System Properties</a>);</li>
  59. <li>then it searches with its class loader for a <code>ProjectHelper</code>
  60. service declarations in the <samp>META-INF</samp>: it searches in the classpath for a
  61. file <samp>META-INF/services/org.apache.tools.ant.ProjectHelper</samp>.
  62. This file will just contain the fully qualified name of the
  63. implementation of <code>ProjectHelper</code> to instantiate;</li>
  64. <li>it will also search with the system class loader for
  65. <code>ProjectHelper</code> service declarations in the <samp>META-INF</samp>;</li>
  66. <li>last but not least it will add its default <code>ProjectHelper</code>
  67. that can parse classical <samp>build.xml</samp> files.</li>
  68. </ul>
  69. <p>
  70. In case of an error while trying to instantiate a <code>ProjectHelper</code>, Ant will
  71. log an error but won't stop. If you want further debugging info about
  72. the <code>ProjectHelper</code> internal 'repository', use the <strong>system</strong>
  73. property <code>ant.project-helper-repo.debug</code> and set it to <code>true</code>;
  74. the full stack trace will then also be printed.
  75. </p>
  76. <p>
  77. When Ant is expected to parse a file, it will ask the <code>ProjectHelper</code>
  78. repository to find an implementation that will be able to parse the input
  79. file. Actually it will just iterate over the ordered list and the first implementation
  80. that returns <code>true</code> to <code>supportsBuildFile(File buildFile)</code> will
  81. be selected.
  82. </p>
  83. <p>
  84. When Ant is started and no input file has been specified, it will search for a default
  85. input file. It will iterate over list of <code>ProjectHelper</code>s and will select
  86. the first one that expects a default file that actually exist.
  87. </p>
  88. <h2 id="writing">Writing your own ProjectHelper</h2>
  89. <p>
  90. The class <code>org.apache.tools.ant.ProjectHelper</code> is the API expected to be
  91. implemented. So write your own <code>ProjectHelper</code> by extending that abstract
  92. class. You are then expected to implement at least the function <code>parse(Project
  93. project, Object source)</code>. Note also that your implementation will be
  94. instantiated by Ant, and it is expecting a default constructor with no arguments.
  95. </p>
  96. <p>
  97. There are some functions that will help you define what your helper is capable of and
  98. what is is expecting:
  99. </p>
  100. <ul>
  101. <li><code>getDefaultBuildFile()</code>: defines which file name is expected if
  102. none provided</li>
  103. <li><code>supportsBuildFile(File buildFile)</code>: defines if your parser
  104. can parse the input file</li>
  105. <li><code>canParseAntlibDescriptor(URL url)</code>: whether your
  106. implementation is capable of parsing a given Antlib
  107. descriptor. The base class returns <code>false</code></li>
  108. <li><code>parseAntlibDescriptor(Project containingProject, URL
  109. source)</code>: invoked to actually parse the Antlib
  110. descriptor if your implementation returned <code>true</code>
  111. for the previous method.</li>
  112. </ul>
  113. <p>
  114. Now that you have your implementation ready, you have to declare it to Ant. Three
  115. solutions here:
  116. </p>
  117. <ul>
  118. <li>use the system property <code>org.apache.tools.ant.ProjectHelper</code>
  119. (see also the <a href="running.html#sysprops">Java System Properties</a>);</li>
  120. <li>use the service file in <samp>META-INF</samp>: in the jar you will build with your
  121. implementation, add a file
  122. <samp>META-INF/services/org.apache.tools.ant.ProjectHelper</samp>.
  123. And then in this file just put the fully qualified name of your
  124. implementation</li>
  125. <li>use the <a href="Tasks/projecthelper.html">projecthelper</a> task (<em>since
  126. Ant 1.8.2</em>) which will install dynamically a helper in the internal helper
  127. 'repository'. Then your helper can be used on the next call to the
  128. <a href="Tasks/import.html">import</a> task.</li>
  129. </ul>
  130. </body>
  131. </html>