http://rubyforge.org/projects/jruby-extras/
Quick start for Windows XP
Windows XP is the only currently tested environment. If you're a unix or macox user, you have initiativetm so you'll figure it out.
The Sun Java JDK version 5 is the only tested environment. But Sun JDK 1.4.2 also works.
First do a JRuby install!
Then do an AntBuilder install using ruby gems which is already installed with jruby
Open up a command prompt(Start Run... cmd.exe)
Run "gem install AntBuilder"
cd to the gem install directory e.g. "cd \C:\apps\jruby-0.9.8\lib\ruby\gems\1.8\gems\AntBuilder-0.4.3\examples\basic"
run "jruby check_install.rb"
you should see "AntBuilder installed ok"
Common optional extras:
If you want to use the 'javac' ant task to compile java code, then a java runtime is not enough. You will also need to modify your CLASSPATH environment variable to include a tools.jar from your java JDK.
If you want to use the 'junit' ant task to run junit tests, then you will also need to modify your CLASSPATH environment variable to include a junit.jar.
See the examples:
examples/basic/check_install.rb
examples/jruby/build.rb
If you do not want to subclass AntBuilder then you have to say:
require 'builder/antbuilder'
ant = Builder::AntBuilder.new
...
ant.copy(:todir => @jruby_classes_dir) {
ant.fileset(:dir => @src_dir, :includes => "**/*.properties")
}(You don't have to use the name 'ant'; its just an example)
If you *do* subclass AntBuilder then you can don't have to use the 'ant' prefix all over the place.
class Build < Builder::AntBuilder
def compile_tasks # Builds the Ant tasks that we need later on in the build
...
copy(:todir => @jruby_classes_dir) {
fileset(:dir => @src_dir, :includes => "**/*.properties")
}
end
...
endAfter every ant command is executed, AntBuilder retrieves the ant properties that have changed since the last command and copies them to ruby instance variables. (Remember that ant properties can only be assigned once).
Properties such as "build.dir" are converted to @build_dir. Any characters in the ant property that are not legal in a ruby instance variable are converted to underscores.
In ant xml build files you will often see:
<property file="default.build.properties"/>
You can try this in AntBulder with
ant.property(:file=>"default.build.properties") # you can omit the "ant." if you are subclassing AntBuilder
If the file contains
build.dir=build
classes.dir=${build.dir}/classes
Then the ruby instance variables defined will be the equivalent of:
@build_dir="build"
@classes_dir="build/classes"
(Of course you could always define build_dir and classes_dir rather than build.dir and classes.dir in the default.build.properties file)
You cannot use the ant properties in the ant commands in the ${property} form. ${property} works in ant xml files and replaced by the ant xml file preprocessor.
But you can use the #{@property} form instead in JRuby code. There are a couple of exceptions – firstly the ant.property task will process ant properties of the form ${property} in the properties file as shown above (classes.dir=${build.dir}/classes).
Another case where 'native' ant properties can be used is shown here:
available(:property=>"jdk1.4+", :classname=>"java.lang.CharSequence")
patternset(:id => "java.src.pattern") {
exclude(:unless=>"jdk1.4+", :name=>"**/AstPersistenceDelegates.java")
}
The above code will also create a ruby instance variable called @jdk1_4_
Ant build files were always originally intended to be declarative using <target name="xxx" depends="aaa, bbb, ccc"> extensivly where the dependencies are only exected once per build.
AntBuilder fully supports this declarative style of ant usage using the depends method. See the example in examples/jruby/build.rb
def compile # Compile the source files for the project
depends :compile_tasks, :check_for_optional_packages
javac(
:destdir => @jruby_classes_dir,
:debug => "true",
:source=> @javac_version,
:classpathref => "build.classpath"
) {
src(:path => @src_dir)
patternset(:refid => "java.src.pattern")
}
end
Circular dependencies are detected dynamically rather than statically as ant does.
The ant taskdef task can be used but the classpath attribute is ignored. If you actually want to use the declared task, you have to make sure that the class is on the classpath.
taskdef(:name =>"jruby_serialize", :classname=>"org.jruby.util.ant.JRubySerialize") {
classpath(:path => @jruby_classes_dir) # ignored
}
...
jruby_serialize(:destdir => @jruby_classes_dir, :verbose=>"true") {
fileset(:dir => @src_dir) {
patternset(:refid=>"ruby.src.pattern")
}
}If you subclass AntBuilder then you can end the class definition with run_targets_from_command_line and it will run tasks from the command line like ant does.
class Build < Builder::AntBuilder
def compile_all
...
end
def clean
...
end
...
run_targets_from_command_line
endThen from the command line you could say:
jruby build.rb clean compile_all
The AntBuilder project uses the excellent leafcutter api and is influenced by its features:
Simple ant references via the ant "refid" are supported. See build.rb for examples.
Some Ant tasks are configured by text content rather than attributes, for example the "echo" task, the "mail" "message" attribute, and the "javadoc" "bottom" attribute. For example for the equivalent of <echo>hello</echo> in AntBuilder: ant.echo(:text=>"hello")
"wrong number of arguments"
For example, you get an error:
foo..rb:283:in `test': wrong number of arguments(1 for 0) (ArgumentError)
A common explanation is that you have defined a method "test" (equivalent to an ant target called test), and somewhere else you are also trying to call the ant task called "test". This call to, what you think is the ant task "test", is interpreted as a call to the method test with some parameters. As your defined method does not (usually) take parameters, the jruby interpreter gives you the above error.
The solution is simply to call you method something that doesn't clash with an and task name, such as do_test.
TaskRunnerException: Can't determine class for 'create_api_docs' (NativeException)
Common explanation: you are trying to call a method called "create_apidocs" that you have defined in the build file, but you have got the name wrong, and antbuilder tries to get leafcutter to run it as an ant task.
The solution is simply to call you method using the correct name.
junitreport: If this task complains about not being able to find xalan you may have (a) a version of ant < 1.6.0 on the classpath before leafcutter.jar and (b) running under java5.
AntBuilder is just a tiny bit of JRuby glue, that sticks together apache-ant, leafcutter and Builder::XMLBase. It relies heavily on Builder::XMLBase to generate the correct syntax for the leafcutter ant api.
Very briefly: When you say "echo(:text => 'starting-build')" there is no such method as echo defined in the class, and this event gets trapped in Builder::XMLBase. The missing method and its parameters are then converted to a string "echo text=starting-build" by XMLBase and the AntBuilder subclass. This string is compatible with the leafcutter api syntax for executing ant code. The string is passed over to leafcutter for execution.
First came Groovy's markup concept: See http://groovy.codehaus.org/GroovyMarkup which was an inspired idea. This inspired Ruby's Builder(and ::XmlMarkup) (http://builder.rubyforge.org/). See http://www.onestepback.org/index.cgi/Tech/Ruby/BuilderObjects.rdoc) for the story and the subsequent namespace issue solution which stung Groovy in the tail big time recently. Basically the Builder concept can be applied to any semi hierarchical structure, such as Swing gui construction and ant scripts. Groovy uses this concept to very good effect with its AntBuilder, SwingBuilder, and SWTBuilder. See http://www.javaworld.com/javaworld/jw-10-2004/jw-1004-groovy.html for an excellent example of the Groovy AntBuilder concept in practice.
Use ant itself as nature intended!
Use the excellent java leafcutter api:
import
org.leafcutter.core.TaskRunner;
public
class MyBuild {
public
static void main(String[] args) {
TaskRunner.run("copy
file=hello.txt tofile=world.txt overwrite=true”);
}
}
Use Groovy's AntBuilder
Use Rake
Get eclipse 3.1 (or 3.2M3+)
Get the JRuby HEAD (The interpreter from version 0.8.2 will NOT work in RDT)
Install RDT nightly build from http://rubyeclipse.sourceforge.net/nightlyBuild/updateSite/
Restart Eclipse
Windows-Preferences-Ruby-Installed-Interpreters and add the JRuby jruby.bat as an interpreter.

Get eclipse 3.1 or 3.2M3+
Get the JRuby HEAD (The interpreter from version 0.8.2 will NOT work in RDT)
Install RDT nightly build from http://rubyeclipse.sourceforge.net/nightlyBuild/updateSite/
Restart Eclipse
Windows-Preferences-Ruby-Installed-Interpreters and add the JRuby jruby.bat as an interpreter.
Install cygwin (including ssh from category “Net” - ssltools)
Open eclipse
Menu-Window-Preferences-Team-CVS-Ext Connection Method
CVS_RSH:=C:\cygwin\bin\ssh.exe
Parameters: -l anonymous rubyforge.org
CVS Server:cvs
Then open the CVS perspective and set up a new repository:: :extssh:anonymous@rubyforge.org:/var/cvs/antbuilder
Expand HEAD and right click AntBuilder and check out as project.
Run the examples
If classes are missing, modify the jruby.bat classpath