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 5.8 KiB

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