Java Tools

Ruby wrappers for javac and jar that don’t just exec.

Ant is a nice tool for writing Java build scripts, but Rake is nicer. The only thing missing from Rake is a way to run javac and jar, and although it’s easy to run these with exec you have to wait for the JVM to start for each invocation. In combination with JRuby this gem lets you run javac and jar in your Rake scripts without exec’ing, by using the programmatic interface to Javac and Java’s ZIP file creation capabilities.

Example

require 'java_tools'

task :compile do
  javac FileList['src/**/*.java'], :destination => 'build, :class_path => FileList['lib/*.jar']
end

task :dist => :compile do
  jar 'dist/my-awsome-app.jar', FileList['build/**/*.class'], :base_dir => 'build'
end

There are more examples in the examples directory.

Command style

Many Rake add-ons look like this:

Spec::Rake::SpecTask.new(:spec) do |spec|
  spec.spec_opts << '--options' << 'spec/spec.opts'
  # ...
end

I think it ruins the DSL illusion, and I prefer to write tasks that contain commands, more like how cp, rm and sh work in Rake.

Nailgun

Don’t forget that since JRuby 1.3 you can minimize the startup by using the built-in Nailgun support. Run

jruby --ng-server &

to start a Nailgun server and then run Rake with this command

jruby --ng -S rake

you’ll notice that the startup time decreases significantly the second time you run it. To avoid having to write all that every time you want to build create an alias, I call mine jrk.

Upcomming

Even though the whole rationale behind Java Tools is to avoid exec it wouldn’t be much effort to support non-JRuby runtimes since at least the javac command needs to build the command string anyway.