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