Powered by | Sweet Ant Tools 1.0Michel CASABIANCA - casa@sweetohm.netSweet Ant Tools (SAT) are a set of Ant tools under the Apache license. They are mostly dedicated to HTML page generation from XML files. Install SATTo install SAT, you must first install a Java Virtual Machine version 1.2 or 1.3. You can download one for free on Sun web site. The latest version of SAT is available on my personal web page. To install SAT, unzip the archive in a directory where you want to install it. Then copy files in the lib directory into the lib directory of Ant. Compile SATTo compile SAT, you must install Ant version 1.3 or 1.4. You can download it on Apache web site. You must also install jar files of Ant (ant.jar) and a JAXP XML parser (for instance the one you can download on Sun's web site) in the lib directory of SAT. To generate the SAT jar file, type ant jar in the prj directory. Then put the generated jar file (sat.jar in the lib directory) in the lib directory of Ant. You will then be able to generate the documentation and zip archive by typing ant on the command line (SAT uses itself to generate it's documentation and the zip archive). Ant ConsoleThis tool runs Ant in a console (in a terminal or within your favorite IDE) and lets you type commands to run targets of the loaded build file. This method runs much faster than Ant because it saves time to launch the Java Virtual Machine, JIT compilation time and build file loading. You can run builds up to 5 times faster than running Ant on the command line. It is obvious than you save more time on small builds (when time spent in usefull code is small compared to startup time). InstallationGo in the bin directory of your Ant installation, copy the ant script (or ant.bat for OS disabled people) to file antc (or antc.bat). Replace org.apache.tools.ant.Main with net.cafebabe.sat.ant.Console. This script will launch the Ant console instead of traditional Ant. Make sure that this script is in your PATH. You must, of course, install SAT by dropping the file sat.jar in the lib directory of your Ant installation. UsageTo run the Ant console, go in the directory of your project (where is living your build.xml file) ant type antc. You will display an help screen typing antc -help on the command line :
When you launch the Ant console, you will be promped as following :
Figure 1: Ant Console running To display an help screen about the console commands, type help in the console :
Note that you can launch a given target by typing its name (this is a shortcut for the target command) provided that this target hasn't the same name than a console command. You can repeat the last command by typing ENTER. You can load a build file using the load command (the path is then relative to the current directory) and search for a build file recursively in the file system with the find command (that behaves like the -find command line argument for Ant). You can also reload the current project with reload. This may be necessary when the build file changes on disk or when the project is corrupted (which happens sometimes after a build error because some tasks don't work after an error). The desc command describes the current project (behaves like the -projecthelp command line parameter for Ant). I have also implemented a timer (to display execution time for builds) that you can switch on or off using the timer command. The result is in milliseconds (because the builds are so fast :o) NotesBuilds in the console are faster because you save startup times, thus performance gains are more important for small builds. For instance, for an empty build (that doesn't do anything), the build in the console takes about 30 ms and 5 seconds in Ant (more than 100 times slower). Nevertheless, performance boost should not be neglected for real world project. For instance, rebuilding the whole SAT project takes about 12 seconds in the console and 25 seconds running Ant (more than 2 times slower). Furthermore, this console supercedes my server task which is far more complex to use and I don't integrate it from the 0.9 release (you can still download it in version 0.7. The console was developped and tested using the latest stable Ant release, that is version 1.5.3-1. Le me know if you have tested with other versions. Tasks descriptionSAT is a set of optional tasks for Ant, thus for each task there is a taskdef element (to tell Ant which class is associated to a given task) and an XML element. File, dir et filesetAll those tasks select files using file and dir attributes and the fileset element. These attributes and element use the following syntax:
Files or directories with relative path are relative to the Ant script. Thus, the attribute file="foo" in the script /home/casa/build.xml selects the file /home/casa/foo. It is possible to mix the file and dir attributes and fileset elements to select files. For instance:
Selects the file file, files in the directory dir and XML files in the dir2 directory. BshDescriptionThis task launch a Beanshell script. This script may be nested in the element or written in a file. It is also possible to give arguments to the script with the args attribute or a nested script. The declarative element (after the <project> one) is the following:
This task was successfully tested with Beanshell version 1.1a18 (which jar file is included in this package). It doesn't work with version 1.1a12 or older (the interface bsh.ConsoleInterface was different for those versions) and with version 1.2b1 (due to a bug). Parameters
Nested elementsYou may select scripts to launch using <fileset> nested elements. See Ant documentation for more details about this element. Nested textA script may be nested within a <bsh> element. Ant propertiesA Beanshell script can access Ant properties using setAntProperty() and getAntProperty() commands. See below for a short sample script. Beanshell CommandsThis package also provides Beanshell commands to access Ant properties (defined using property elements) from within a Beanshell script. Those commands have the following signature:
Another command resolves a file name according to the directory where the Ant script is running. In an Ant script, a file name is relative to the directory of the script (instead of the current directory). The signature of this method is the following:
This task defines a variable called antProject in the Beanshell interpreter. This variable contains a reference to the running Ant project. ExamplesTo launch a script bsh.bsh in the test directory, you may write:
You can also write the script in the <bsh> element:
Note that a script nested in a bsh element is parsed when the Ant script is executed. Thus it should not contain < nor & characters or it must be nested in a in a CDATA declaration as following:
To pass a foo argument to the script (as if it was was passed on the command line), you may use an args attribute:
Now a more complex sample:
Where the script test/bsh.bsh is the following:
Will produce the following output:
This trace shows the evaluation order of the beanshell code:
The following script displays the value of foo property and give a value to the bar property.
XTaskDescriptionThis task uses the XT XSLT processor from James Clark to transform an XML document. This processor is, according to many benchmarks, the fastest Java processor (but the author doesn't develop it anymore). The task declaration (after the <project> element) is the following:
Parameters
Nested elementsYou may select files to transform with a nested <fileset> element. See Ant documentation for more details about this element. It is also possible to pass arguments to the XSLT processor using <arg> elements. For instance, to assign the value bar to the parameter foo, you should nest within the <xtask> element:
This element may not contain text. ExamplesTo transform a file foo.xml to bar.html using the transfo.xsl stylesheet, you may write:
To assign the value bar to the parameter named foo, you will write the element:
NoteThis task was previously distributed alone on my page (version 0.1). It is now part of the SAT package. ValidDescriptionThis task validates XML files. You may validate a file against its DTD or simply check the XML syntax (check that the document is well formed). You can choose to stop Ant compilation while an error is encountered, the error level or the maximum number of parsing errors to display for each file. The declarative element (after the <project> one) is the following:
Parameters
Nested elementsThe files to merge can be selected using a <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExamplesTo validate all XML files of the a directory, copy the following buildfile in that directory:
Then launch Ant typing ant on the command line. Note that attributes have their default values. Adapt this file to meet your needs. If you want to validate all Ant buildfile on your disk, launch in the root directory the following file:
The dtd attribute is false because buildfile do not have a DTD, you ask the parser not to stop when it encounters an error to scan the whole disk, you put the error level on the lowest value to detect any problem and you ask to display only one error per file in order to avoid too many error messages. MergeDescriptionThis element is for merging XML files into one. This file contains the root elements within a nesting one. The task declaration (after the <project> element) is the following:
For instance, this element may merge the following XML files:
and:
In a single file like this one:
This element is useful to generate composite documents. For instance, the index page of my site is generated using this task. A presentation text, news and links (that are small distinct files) are merged in a single XML file. This file is transformed into an HTML page using XSLT. Parameters
Nested elementsThe files to merge can be selected using a <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExamplesTo merge XML files in the index directory using ISO 8859-1 encoding into a file called index.xml with no doctype (and a root element index), you may write:
To merge the XML files in the xml directory and subdirectories, in a single index.xml file, you may write:
InsertDescriptionThis task is for replacing a processing instruction with the content of a given file. You can thus include an HTML file fragment in an HTML file. This is the way menus are included in the web pages of my site. The task declaration (after the <project> element) is the following:
Parameters
Nested elementsYou can select the files to process using a <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExamplesTo replace <?menu ?> processing instructions of files in the html directory with the content of the menu.html file, you may write the following element:
NestDescriptionUsing this task, you can nest files within a given one, replacing a given processing instruction. You may this way nest HTML fragments in a page template. The task declaration (after the <project> element) is the following:
Parameters
Nested elementsYou can select the files to process using a <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExemplesTo nest files in the html directory within the file page.html, replacing the <?body ? processing instruction, you may write the following element:
SplitDescriptionThis task splits files using processing instructions. The name of the generated files is written in the processing instruction. The task declaration (after the <project> element) is the following:
For instance, the following file:
Will be split in two files (page1.html and page2.html):
Most of XSLT processors (such as XT or Xalan) implement extensions to produce more than one file but this task allows you to avoid to pollute your XSLT code with proprietary one. Parameters
Nested elementsYou can choose the files to process with a nested <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExamplesTo split a file called foo using processing instructions that look like <?cut file="bar"?>, you would write:
XML Word CountDescriptionThis task counts words in an XML document. Useful when you write an XML file with word count constraints (for an article for instance). The text in any element of the document is taken into account while the text in attributes is ignored. This default behavior may be changed using the task parameters. The task declaration (after the <project> element) is the following:
Parameters
Nested elementsYou can choose the files to process with a nested <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExamplesLet's say you want to count words in the XML files of the current directory except build.xml and want to exclude the source element of the word count. You may write the following target:
This will produce the following output:
Now let's suppose that we want to write the count rules in a properties file named document.properties:
This file indicates the following rules:
To count words in XML documents of the xml directory using those rules and put the result in the wc Ant property, we might write:
LinkDescriptionThis task checks links in HTML files. It distincts two kinds of links: local links (that point to a file in the same site and are relatives) and external ones (that point to other sites and start with a protocol indicator such as http: or ftp:). The task declaration (after the <project> element) is the following:
Parameters
Nested elementsYou can choose the files to process with a nested <fileset> element. See Ant documentation for more details about this element. This element may not contain text. ExamplesLet's suppose that you want to check HTML files in the html directory and it's subdirectories and want to stop processing if a broken link is met. You would write:
If a broken link is met, the compilation will stop with an error message that gives the file and link. If you want to to check external links (which may be quite long) without stopping on error and log checks in a file named links.txt, you may write:
Emacs modeAnt is widely used to build Java projects, and thus is integrated to many Java development tools (such as JDE, an Emacs mode for Java development). Nevertheless, Ant also proves to be helpful in many other occasions, such as generate HTML documentation from XML source files. That's what lead me to develop this standalone Ant mode for Emacs. To install this Ant mode, copy el/ant.el file in a location where Emacs will find it and add the following lines in your .emacs configuration file:
When you restart Emacs, an Ant menu appears, with the following entries:
The result of the build is printed in the compilation buffer and you may thus click on an error message to open the responsible file in an Emacs buffer. It may be useful to bind keys to Ant calls. For instance, my .emacs file has the following lines:
LicenseThis software is under Apache Software License. You may find a copy of this license (and of the licenses of other software used in this program) in the LICENSE file of SAT installation directory. HistoryVersion 0.9 (2003-07-14)Upadtes are the following:
Version 0.8 (2003-07-13)Updates are the following:
Version 0.7 (2002-11-11)The documentation uses my new DTD and has been updated. New tasks:
Bug fix and enhancements:
Version 0.6 (2001-10-27)The task Marge has been optimized (using StringBuffer). The task is about 30 times faster ! Version 0.5 (2001-10-17)New Valid task to validate XML files. Version 0.4 (2001-09-22)New Link task to check links in an HTML file. Version 0.3 (2001-09-18)Test tasks attributes. File attributes (such as file and dir) are checked. Bug correction (file path are now relative to the project file location instead of the current directory). It is now possible to set and get Ant properties from within a Beanshell script using setAntProperty() and getAntProperty() commands. Tasks are no more executable outside Ant (this was much more simple within Ant and painful to maintain). The code was ported to the last versions of Beanshell (version 1.1a18 works, but 1.2b1 doesn't due to a bug). Added unit tests (in the prj/test directory). Version 0.2 (2001-08-20)The JAXP API is now used in the XTask. Permits to instanciate the XML parser in a generic way (independent from the parser implementation, provided that it implements the JAXP API). This class also search correctly for files (in the directory of the build.xml file). Version 0.1 (2001-06-06)The tasks have been modified (after intensive usage to generate my web site) to be more efficient/user friendly. Changes are the following:
There should not be any syntax modification in future versions, that should only stabilize the code (attributes have to be tested to produce informative error messages). Version 0.0 (2001-05-28)First distributed version of SAT. The tasks Bsh, XTask, Merge, Insert and Split have been implemented. Enjoy ! Last update: 2003-07-14 |