Sample code of the Antwrap library looks like this;
#All options are optional. The defaults are as follows;
#{:name=>'', :basedir=>'.', :declarative=> true, :logger=> Logger.new(STDOUT), :loglevel=> Logger::DEBUG}
#so you can call AntProject.new() if you like.
@ant = AntProject.new({:name=>"FooProject",
:basedir=> current_dir,
:declarative=> true,
:logger=> Logger.new(STDOUT),
:loglevel=> Logger::DEBUG})
@ant.path(:id => "other.class.path"){
pathelement(:location => "classes")
pathelement(:location => "config")
}
@ant.path(:id => "common.class.path"){
fileset(:dir => "${common.dir}/lib"){
include(:name => "**/*.jar")
}
pathelement(:location => "${common.classes}")
}
@ant.javac(:srcdir => "test", :destdir => "classes"){
classpath(:refid => "common.class.path")
classpath(:refid => "foo.class.path")
}
Alternatively, you can declare your Ant project to run in non-declarative mode, so that it only executes tasks
upon the invocation of the execute() method (this is a more Object Oriented approach, and may be useful in some
circumstances):
@ant = AntProject.new({:name=>"FooProject", :declarative=> false})
javac_task = @ant.javac(:srcdir => "test", :destdir => "classes"){
classpath(:refid => "common.class.path")
classpath(:refid => "foo.class.path")
}
javac_task.execute
There are some reserved words that we have to work around. For example, Ant-Contrib tasks such as 'if' and 'else'
conflict with the Ruby reserved words. Under most circumstances, you won't need to use these tasks (indeed, the
awkwardness of conditional operations in Ant scripts is likely one of the reasons why you want to move to a build system
such as Rake). Nevertheless, there are that occasions demands it. Reserved words like this can be worked around by simply prepending an
underscore character ('_') to the task:
#This is an example of the Ant-Contrib tasks.
#note: I added an underscore to 'equals' even though it isn't a reserved word.
#This makes the code block more symmetrical (it's not required though). It also
#illustrates that you can apply an underscore to any task and it will be stripped by Antwrap.
@ant._if(){
_equals(:arg1 => "${bar}", :arg2 => "bar")
_then(){
echo(:message => "if 1 is equal")
}
_else(){
echo(:message => "if 1 is not equal")
}
}
Content data is added via a 'pcdata' attribute:
@ant.echo(:pcdata => "<foo&bar>")
Antwrap includes a conversion script to take an existing Ant build file and convert it to a rake file. It will
convert each Ant Target into a JRake task. It will also convert each and every Any Task into an Antwrap method call.
Ant tasks that conflict with Ruby keywords are prepended with an
underscore (see above).
Antwrap is a Beta release. I'd love any feedback you can provide on your experence with it.
There are no 3rd party jars required other than the Ant jar files in your JRuby Classpath.
Comments or Questions?
Caleb Powell
caleb.powell@gmail.com