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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 to parse the build file
  26. and create java instances representing the build workflow. It also declares which
  27. kind of file it can parse, and which file name it expects as default input file.
  28. </p>
  29. <p>
  30. So in Ant there is a default <code>ProjectHelper</code>
  31. (<code>org.apache.tools.ant.helper.ProjectHelper2</code>) which will parse the
  32. usual build.xml files. And if no build file is specified on the command line, it
  33. will expect to find a build.xml file.
  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 language than XML. Some experiment 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 can now know about several implementations of <code>ProjectHelper</code>
  44. and have to decide which to use for each build file.
  45. </p>
  46. <p>So Ant at startup will list the found implementations and will keep it
  47. ordered as it finds them 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 error while trying to instanciate a <code>ProjectHelper</code>, Ant
  63. will log an error but still won't stop. If you want further debugging
  64. info about the <code>ProjectHelper</code> internal 'repository', use the system
  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. Then when Ant is expected to parse a file, it will ask the
  70. <code>ProjectHelper</code> repository to found an implementation that will be
  71. able to parse the input file. Actually it will just iterate on 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. And when Ant is launching and there is no input file specified, it will search for
  77. a default input file. It will iterate on the list of <code>ProjectHelper</code>
  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. Then 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. </ul>
  98. </p>
  99. <p>
  100. Now that you have your implementation ready, you have to declare it to Ant. Two
  101. solutions here:
  102. <ul>
  103. <li>use the system property <code>org.apache.tools.ant.ProjectHelper</code>
  104. (see also the <a href="running.html#sysprops">Java System Properties</a>);</li>
  105. <li>use the service file in META-INF: in the jar you will build with your
  106. implementation, add a file
  107. <code>META-INF/services/org.apache.tools.ant.ProjectHelper</code>.
  108. And then in this file just put the fully qualified name of your
  109. implementation</li>
  110. </ul>
  111. </p>
  112. </body>
  113. </html>