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.

desc.xml 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?xml version="1.0"?>
  2. <document>
  3. <properties>
  4. <author email="conor@apache.org">Conor MacNeill</author>
  5. <title>Mutant Design Notes</title>
  6. </properties>
  7. <body>
  8. <section name="Mutant Design Notes">
  9. <p>
  10. This is a brief, albeit rambling description of Mutant.
  11. Mutant has many experimental ideas which may or may not prove useful.
  12. I'll try to describe what is there and let anyone who is interested
  13. comment. Mutant is still immature. You'll notice that there is, at this
  14. time, just one task, a hacked version of the echo task, which I have
  15. been using to test out ideas. Most tasks would end up being pretty
  16. similar to their Ant 1.x version.
  17. </p>
  18. <p>
  19. OK, let me start with some of the motivating requirements. There are of
  20. coure many Ant2 requirements but I want to focus on these two for now.
  21. Mutant does also address many of the other Ant2 requirements.
  22. </p>
  23. <p>
  24. I'll use the terms Ant and mutant somewhat interchangeably - just
  25. habit, not an assumption of any sort.
  26. </p>
  27. <p>
  28. One of the things which is pretty difficult in Ant 1.x is the
  29. management of classpaths and classloaders. For example, today the
  30. antlr task requires the antlr classes in the classpath used to start
  31. ant. I'm talking here about the classpath built up in the ant.bat/ant
  32. script launchers. At the same time, the checkstyle task
  33. which uses antlr won't run if the antlr classes are in the classpath
  34. because then those classes cannot &quot;see&quot; the classes in the taskdef's
  35. classpath.
  36. </p>
  37. <p>
  38. Another requirement I have is extensibility. In Ant 1.x this is
  39. difficult because whenever a new type is created, each task which
  40. needs to support this type must be changed to provide the new addXXX
  41. method. The ejbjar task is on example of this problem with its concept of vendor
  42. specific tools. The zip/jar task, with its support for different types
  43. of fileset, is another. The addition of the classfileset to Ant requires
  44. a change to the zip task.
  45. </p>
  46. </section>
  47. <section name="Mutant Initialization">
  48. <p>
  49. Mutant defines a classloader hierarchy somewhat similar to that used
  50. in Tomcat 4. Tasks join into this hierarchy at a particular point to
  51. ensure they have visibility of the necessary interface classes and no
  52. visibility of the Ant core itself. There is nothing particularly novel
  53. about this approach, but tasks are able to request certain additional resources
  54. as we will see later.
  55. </p>
  56. <p>
  57. Mutant starts with two jars. One is the start.jar which contains just
  58. one class, Main.java which establishes the initial configuration and
  59. then runs the appropriate front end command line class. If a different
  60. front end was desired, a different launch class, in its own jar, would
  61. be used. This would perhaps configure the classloader hierarchy somewhat
  62. differently and start the approriate GUI front end class.
  63. </p>
  64. <p>
  65. The second jar, init.jar, provides a number of initialisation utilities. These
  66. are used by Main.java to setup Ant and would also be used by any other front end
  67. to configure Ant. The important class here is the
  68. InitConfig which communicates the state of Ant at startup into the the core of
  69. Ant when it starts up. Main determines the location of ANT_HOME based on the
  70. location of the start classes and then populates the InitConfig with both
  71. classloaders and information about the location of various jars and config
  72. files.
  73. </p>
  74. <p>
  75. At the top of the classloader hierarchy
  76. are the bootstrap and system classloaders. I won't really
  77. distinguish between these in mutant. Combined they provide the JDK
  78. classes, plus the classes from the init and start jars. One objective is
  79. to keep the footprint of the init and start jars small so they do not
  80. require any external classes, which may then become visible lower in the
  81. hierarchy. Main does not explicitly create these loaders, of course, but
  82. just adds a reference to the init config as system class loader
  83. </p>
  84. <p>
  85. The next jar is for the common area. This provides interface definitions
  86. and utility classes for use by both the core and by tasks/types etc. It
  87. is loaded from ANT_HOME/lib/common/*.jar. Typically this is just
  88. lib/common/common.jar but any other jars in here are loaded. This
  89. pattern is used in the construction of all of the classloaders.
  90. </p>
  91. <p>
  92. Next up is the core loader. It includes the lib/antcore/antcore.jar plus
  93. any others including the XML parser jars. Mutant's core does not assume that
  94. the project model will come from an XML description but XML facilities
  95. are needed in the core for reading in Ant library defs and config files.
  96. The parser jar locations are also stored in the init config. This lets
  97. the jars be added to any Ant library that wants to use Ant's XML parser
  98. rather than providing its own. Similarly tools.jar's location is
  99. determined automatically and added to the config for use by tasks which
  100. request it. I'll go into more detail when discussing the antlib processing.
  101. </p>
  102. <p>
  103. The final jar that is loaded is the jar for the frontend - cli.jar. This
  104. is not passed in init config since these classes are not visible to the
  105. core and are not needed by it. So the hierarchy is
  106. <pre>
  107. jdk classes
  108. |
  109. start/init
  110. |
  111. common
  112. |
  113. antcore
  114. |
  115. cli
  116. </pre>
  117. </p>
  118. <p>
  119. Task classloaders generally will come in at common, hiding the core classes, front
  120. end and XML parser classes from tasks.
  121. </p>
  122. <p>
  123. Once Main has setup the initConfig, it creates the front end commandline
  124. class and launches mutant proper, passing it the command line args and
  125. the init config.
  126. </p>
  127. <p>
  128. A GUI would typically replace start.jar and the cli.jar with its own
  129. versions which manage model construction from GUI processes rather than
  130. from XML files. It may be possible to move some of Main.java's
  131. processing into init.jar if it is useful to other front ends. I haven't
  132. looked at that balance.
  133. </p>
  134. </section>
  135. <section name="Mutant Frontend">
  136. <p>
  137. The front end is responsible for coordinating execution of Ant. It
  138. manages command line arguments, builds a model of the Project to be
  139. evaluated and coordinates the execution services of the core. cli.jar
  140. contains not only the front-end code but also the XML parsing code for
  141. building a project model from an XML description. Other front ends may
  142. choose to build project models in different ways. Commandline is pretty
  143. similar to Ant 1.x's Main.java - it handles arguments, building loggers,
  144. listeners, defines, etc - actually I haven't fully implemented
  145. command line defines in
  146. mutant yet but it would be similar to Ant 1.x.
  147. </p>
  148. <p>
  149. Commandline then moves to building a project model from the XML
  150. representation. I have just expanded the approach in Ant 1's
  151. ProjectHelper for XML parsing, moving away from a stack of inner classes.
  152. The classes in the front end XML parsing use some XML utility base
  153. classes from the core.
  154. </p>
  155. <p>
  156. The XML parsing handles two elements at parse time. One is the &lt;ref&gt;
  157. element which is used for project references - that is relationships
  158. between project files. The referenced project is parsed as well. The
  159. second is the &lt;include&gt; element which includes either another complete
  160. project or a project &lt;fragment&gt; directly into the project. All the other
  161. elements are used to build a project model which is later processed in
  162. the core.
  163. </p>
  164. <p>
  165. The project model itself is organized like this
  166. </p>
  167. <p>
  168. <ul>
  169. <li>A project contains</li>
  170. <ul>
  171. <li>named references to other projects</li>
  172. <li>targets</li>
  173. <li>build elements (tasks, type instances)</li>
  174. </ul>
  175. <li>A target contains</li>
  176. <ul>
  177. <li>build elements (tasks, type instances)</li>
  178. </ul>
  179. <li>A build element contains</li>
  180. <ul>
  181. <li>build elements (nested elements)</li>
  182. </ul>
  183. </ul>
  184. </p>
  185. <p>
  186. So, for now the project model contains top level tasks and type
  187. instances. I'm still thinking about those and property scoping
  188. especially in the face of project refs and property overrides. Anyway,
  189. the running of these tasks is currently disabled.
  190. </p>
  191. <p>
  192. Once the model is built, the commandline creates an execution manager
  193. instance, passing it the initConfig built by Main.jar. It adds build
  194. listeners and then starts the build using the services of the
  195. ExecutionManager.
  196. </p>
  197. </section>
  198. <section name="Ant Libraries">
  199. <p>
  200. Before we get into execution proper, I'll deal with the structure of an
  201. ant library and how it works. An antlibrary is a jar file with a library
  202. descriptor located in META-INF/antlib.xml. This defines what
  203. typedefs/taskdefs/converters the library makes available to Ant. The
  204. classes or at least some of the classes for the library will normally be
  205. available in the jar. The descriptor looks like this (I'll provide two
  206. examples here)
  207. </p>
  208. <p>
  209. <pre>
  210. &lt;antlib libid=&quot;ant.io&quot;
  211. home=&quot;http://jakarta.apache.org/ant&quot;
  212. isolated=&quot;true&quot;&gt;
  213. &lt;typedef name=&quot;thread&quot; classname=&quot;java.lang.Thread&quot;/&gt;
  214. &lt;taskdef name=&quot;echo&quot; classname=&quot;org.apache.ant.taskdef.io.Echo&quot;/&gt;
  215. &lt;converter classname=&quot;org.apache.ant.taskdef.io.FileConverter&quot;/&gt;
  216. &lt;/antlib&gt;
  217. &lt;antlib libid=&quot;ant.file&quot;
  218. home=&quot;http://jakarta.apache.org/ant&quot;
  219. reqxml=&quot;true&quot; reqtools=&quot;true&quot; extends=&quot;ant.io&quot;
  220. isolated=&quot;true&quot;&gt;
  221. &lt;taskdef name=&quot;copy&quot; classname=&quot;org.apache.ant.file.copy&quot;/&gt;
  222. &lt;/antlib&gt;
  223. </pre>
  224. </p>
  225. <p>
  226. the &quot;libid&quot; attribute is used to globally identify a library. It is used
  227. in Ant to pick which tasks you want to make available to a build file.
  228. As the number of tasks available goes up, this is used to prevent name
  229. collisions, etc. The name is constructed similarly to a Java package name -
  230. i.e Reverse DNS order.
  231. </p>
  232. <p>
  233. The &quot;home&quot; attribute is a bit of fluff unused by mutant to allow tools
  234. to manage libraries and update them etc. More thought could go into
  235. this.
  236. </p>
  237. <p>
  238. &quot;reqxml&quot; allows a library to say that it wants to use Ant's XML parser
  239. classes. Note that these will be coming from the library's classloader
  240. so they will not, in fact, be the same runtime classes as used in Ant's core,
  241. but it saves tasks packaging their own XML parsers.
  242. </p>
  243. <p>
  244. &quot;reqtools&quot; allows a library to specify that it uses classes from Sun's
  245. tools.jar file. Again, if tools.jar is available it will be added to the
  246. list of classes in the library's classloader
  247. </p>
  248. <p>
  249. &quot;extends&quot; allows for a single &quot;inheritance&quot; style relationship between
  250. libraries. I'm not sure how useful this may be yet but it seems
  251. important for accessing common custom types. It basically translates
  252. into the class loader for this library using the one identified in
  253. extends as its parent.
  254. </p>
  255. <p>
  256. &quot;isolate&quot; specifies that each task created from this libary comes from
  257. its own classloader. This can be used with tasks derived from Java
  258. applications which have static initialisers. This used to be an issue
  259. with the Anakia task, for example. Similarly it could be used to ensure that
  260. tool.jar classes are unloaded to stop memory leaks. Again this is
  261. experimental so may not prove ultimately useful.
  262. </p>
  263. <p>
  264. The &lt;typedef&gt; in the example creates a &lt;thread&gt; type. That is just a bit of fun which
  265. I'll use in an example later. It does show the typedefing of a type from
  266. outside the ant library however.
  267. </p>
  268. <p>
  269. &lt;taskdef&gt; is pretty obvious. It identifies a taskname with a class from
  270. the library. The import task, which I have not yet implemented will
  271. allow this name to be aliased - something like
  272. </p>
  273. <p>
  274. &lt;import libid=&quot;ant.file&quot; task=&quot;echo&quot; alias=&quot;antecho&quot;/&gt;
  275. </p>
  276. <p>
  277. Tasks are not made available automatically. The build file must state
  278. which tasks it wants to use using an &lt;import&gt; task. This is similar to
  279. Java's import statement. Similarly classes whose ids start with &quot;ant.&quot;
  280. are fully imported at the start of execution.
  281. </p>
  282. </section>
  283. <section name="Mutant Configuration">
  284. <p>
  285. When mutant starts execution, it reads in a config file. Actually it
  286. attempts to read two files, one from $ANT_HOME/conf/antconfig.xml and
  287. another from $HOME/.ant/antconfig.xml. Others could be added even
  288. specified in the command line. These config files are used to provide
  289. two things - libpaths and task dirs.
  290. </p>
  291. <p>
  292. Taskdirs are locations to search for additional ant libraries. As people
  293. bundle Ant tasks and types with their products, it will not be practical
  294. to bundle all this into ANT_HOME/lib. These additional dirs are scanned
  295. for ant libraries. All .zip/.jar/.tsk files which contain the
  296. META-INF/antlib.xml file will be processed.
  297. </p>
  298. <p>
  299. Sometimes, of course, the tasks and the libraries upon which they depend
  300. are not produced by the same people. It is not feasible to go in and
  301. edit manifests to connect the ant library with its required support
  302. jars, so the libpath element in the config file is used to specify
  303. additional paths to be added to a library's classloader. An example
  304. config would be
  305. </p>
  306. <p>
  307. <pre>
  308. &lt;antconfig&gt;
  309. &lt;libpath libid=&quot;ant.file&quot; path=&quot;fubar&quot;/&gt;
  310. &lt;libpath libid=&quot;ant.file&quot; url=&quot;http://fubar&quot;/&gt;
  311. &lt;/antconfig&gt;
  312. </pre>
  313. </p>
  314. <p>
  315. Obviously other information can be added to the config - standard
  316. property values, compiler prefs, etc. I haven't done that yet. User
  317. level config override system level configs.
  318. </p>
  319. <p>
  320. So, when a ant library creates a classloader, it will take a number of
  321. URLS. One is the task library itself, the XML parser classes if
  322. requested, the tools.jar if requested, and any additional libraries
  323. specified in the &lt;antconfig&gt;. The parent loader is the common loader
  324. from the initconfig. unless this library is an extending library.
  325. </p>
  326. </section>
  327. <section name="Mutant Execution">
  328. <p>
  329. Execution of a build is provided by the core through two key classes.
  330. One if the ExecutionManager and the other is the ExecutionFrame. An
  331. execution frame is created for each project in the project model
  332. hierarchy. It represents the execution state of the project - data
  333. values, imported tasks, typedefs, taskdefs, etc.
  334. </p>
  335. <p>
  336. The ExecutionManager begins by reading configs, searching for ant
  337. libraries, configuring and appending any additional paths, etc. It then
  338. creates a root ExecutionFrame which represents the root project. when a
  339. build is commenced, the project model is validated and then passed to
  340. the ExecutionFrame.
  341. </p>
  342. <p>
  343. the ExecutionFrame is the main execution class. When it is created it
  344. imports all ant libraries with ids that start with ant.*. All others are
  345. available but must be explicitly imported with &lt;import&gt; tasks. When the
  346. project is passed in, ExecutionFrames are created for any referenced
  347. projects. This builds an ExecutionFrame hierarchy which parallels the
  348. project hierarchy. Each &lt;ref&gt; uses a name to identify the referenced
  349. project. All property and target references use these reference names to
  350. identify the particular frame that hold the data. As an example, look at
  351. this build file
  352. </p>
  353. <p>
  354. <pre>
  355. &lt;project default=&quot;test&quot; basedir=&quot;..&quot; doc:Hello=&quot;true&quot;&gt;
  356. &lt;ref project=&quot;test.ant&quot; name=&quot;reftest&quot;/&gt;
  357. &lt;target name=&quot;test&quot; depends=&quot;reftest:test2&quot;&gt;
  358. &lt;echo message=&quot;hello&quot;/&gt;
  359. &lt;/target&gt;
  360. &lt;/project&gt;
  361. </pre>
  362. </p>
  363. <p>
  364. Notice the depends reference to the test2 target in the test.ant project
  365. file. I am still using the &quot;:&quot; as a separator for refs. It doesn't
  366. collide with XML namespaces so that should be OK.
  367. </p>
  368. <p>
  369. Execution proceeds by determining the targets in the various frames
  370. which need to be executed. The appropriate frame is requested to execute
  371. the target's tasks and type instances. The imports for the frame are
  372. consulted to determine what is the approrpiate library and class from
  373. that library. A classloader is fetched, the class is instantiated,
  374. introspected and then configured from the corresponding part of the
  375. project model. Ant 1.x's IntrospectionHelper has been split into two -
  376. the ClassIntrospector and the Reflector. When the task is being
  377. configured, the context classloader is set. Similarly it is set when the
  378. task is being executed. Types are handled similarly. When a type in
  379. instantiated or a task executed, and they support the appropriate
  380. interface, they will be passed a context through which they can access
  381. the services of the core. Currently the context is an interface although
  382. I have wondered if an abstract class may be better to handle expansion
  383. of the services available over time.
  384. </p>
  385. </section>
  386. <section name="Introspection and Polymorphism">
  387. <p>
  388. Introspection is not a lot different from Ant 1.x. After some thought I
  389. have dropped the createXXX method to allow for polymorphic type support, discussed
  390. below. setXXX methods, coupled with an approriate string to
  391. type converter are used for attributes. addXXX methods are used for
  392. nested elements. All of the value setting has been moved to a Reflector
  393. object. Object creation for addXXX methods is no longer provided in the
  394. reflector class, just the storage of the value. This allows support for
  395. add methods defined in terms of interfaces. For example, the hacked Echo
  396. task I am using has this definition
  397. </p>
  398. <p>
  399. <pre>
  400. /**
  401. * testing
  402. *
  403. * @param runnable testing
  404. */
  405. public void addRun(Runnable runnable) {
  406. log(&quot;Adding runnable of type &quot;
  407. + runnable.getClass().getName(), MessageLevel.MSG_WARN);
  408. }
  409. </pre>
  410. </p>
  411. <p>
  412. So when mutant encounteres a nested element it does the following checks
  413. </p>
  414. <p>
  415. Is the value specified by reference?
  416. </p>
  417. <p>
  418. &lt;run ant:refid=&quot;test&quot;/&gt;
  419. </p>
  420. <p>
  421. Is it specified by as a polymorphic type?
  422. </p>
  423. <p>
  424. &lt;run ant:type=&quot;thread&quot;/&gt;
  425. </p>
  426. <p>
  427. or is it just a normal run o' the mill nested element, which is
  428. instantiated by a zero arg constructor.
  429. </p>
  430. <p>
  431. Note the use of the ant namespace for the metadata. In essence the
  432. nested element name &lt;run&gt; identifies the add method to be used, while
  433. the refId or type elements specify the actual instance or type to be
  434. used. The ant:type identifies an Ant datatype to be instantiated. If
  435. neither is specified, the type that is expected by the identified
  436. method, addRun in this case, is used to create an instance. In this case
  437. that would fail.
  438. </p>
  439. <p>
  440. Polymorphism, coupled with typedefs is one way, and a good way IMHO, of
  441. solving the extensibility of tasks such as ejbjar.
  442. </p>
  443. <p>
  444. OK, that is about the size of it. Let me finish with two complete build
  445. files and the result of running mutant on them.
  446. </p>
  447. <h3>build.ant</h3>
  448. <p>
  449. <pre>
  450. &lt;project default=&quot;test&quot; basedir=&quot;..&quot; doc:Hello=&quot;true&quot;&gt;
  451. &lt;ref project=&quot;test.ant&quot; name=&quot;reftest&quot;/&gt;
  452. &lt;target name=&quot;test&quot; depends=&quot;reftest:test2&quot;&gt;
  453. &lt;echo message=&quot;hello&quot;/&gt;
  454. &lt;/target&gt;
  455. &lt;/project&gt;
  456. </pre>
  457. </p>
  458. <h3>test.ant</h3>
  459. <p>
  460. <pre>
  461. &lt;project default=&quot;test&quot; basedir=&quot;.&quot; doc:Hello=&quot;true&quot;&gt;
  462. &lt;target name=&quot;test2&quot;&gt;
  463. &lt;thread ant:id=&quot;testit&quot;/&gt;
  464. &lt;echo message=&quot;hello2&quot;&gt;
  465. &lt;run ant:refid=&quot;testit&quot;&gt;
  466. &lt;/run&gt;
  467. &lt;/echo&gt;
  468. &lt;echo message=&quot;hello3&quot;&gt;
  469. &lt;run ant:type=&quot;thread&quot;&gt;
  470. &lt;/run&gt;
  471. &lt;/echo&gt;
  472. &lt;/target&gt;
  473. &lt;/project&gt;
  474. </pre>
  475. </p>
  476. <p>
  477. If I run mutant via a simple script which has just one line
  478. </p>
  479. <p>
  480. java -jar /home/conor/dev/mutant/dist/lib/start.jar $*
  481. </p>
  482. <p>
  483. I get this
  484. </p>
  485. <p>
  486. <pre>
  487. test2:
  488. [echo] Adding runnable of type java.lang.Thread
  489. [echo] hello2
  490. [echo] Adding runnable of type java.lang.Thread
  491. [echo] hello3
  492. test:
  493. [echo] hello
  494. BUILD SUCCESSFUL
  495. Total time: 0 seconds
  496. </pre>
  497. </p>
  498. <p>
  499. Lets change the &lt;run&gt; definition to
  500. </p>
  501. <p>
  502. &lt;run/&gt; in test.ant and the result becomes
  503. </p>
  504. <p>
  505. <pre>
  506. test2:
  507. [echo] Adding runnable of type java.lang.Thread
  508. [echo] hello2
  509. BUILD FAILED
  510. /home/conor/dev/mutant/test/test.ant:10:
  511. No element can be created for nested element &lt;run&gt;.
  512. Please provide a value by reference or specify the value type
  513. </pre>
  514. </p>
  515. </section>
  516. </body>
  517. </document>