On a few projects now I’ve used jruby with rake. I know that I can use rvm and just fetch the gems per project, but for developers stuck on windows that way is a little more than difficult. So here’s how I package up rubygems in a JAR.

#!/bin/bash
mkdir gemjar
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar haml --version 3.1.2 --no-rdoc --no-ri
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar net-scp --version 1.0.4 --no-rdoc --no-ri
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar jruby-openssl --version 0.7.4 --no-rdoc --no-ri
java -jar jruby-complete-1.6.3.jar -S gem install -i ./gemjar fpm --version 0.3.7 --no-rdoc --no-ri
jar cf ruby-gems.jar -C gemjar .
rm -rf gemjar

You then invoke jruby rake via:

#!/bin/bash
java -jar jruby-complete-1.6.3.jar -rruby-gems.jar -S rake build.rb $@

This means that you can then invoke the following useful little nuggets:

task :list_gems do
  require "rubygems/gem_runner"
  Gem::GemRunner.new.run ["list"]
end

task :sass_watch do
  require 'haml'
  require 'haml/exec'
  opts = Haml::Exec::Sass.new ['--watch', 'src/main/sass:src/main/webapp/static/css']
  opts.parse!
end

task :ssh_copy, :file do |t, args|
  require "net/ssh"
  require "net/scp"
  Net::SSH.start("repository.remote", "user") do |ssh|
    ssh.scp.upload!(args.file, "/tmp/files/")
  end
end

task :make_rpm => :make_war do
  fail "Please install rpm-build to make RPMs!" unless system("rpmbuild --version")

  require "fpm"
  require "fpm/program"
  exit_code = FPM::Program.new.run([
    "-s", "dir", "-t", "rpm", "-n", APPLICATION_NAME, "-v", RPM_VERSION_NUMBER, "-a", "all",
    "--post-install", "target/rpmbuild/opt/application/bin/post_install.sh",
    "--pre-uninstall", "target/rpmbuild/opt/application/bin/pre_uninstall.sh",
    "-C", "target/rpmbuild", "opt"
  ])

  fail "Build failed" if exit_code != 0
end

How awesome is this?