require 'rake' => 'rubygems'

Earlier this week I was starting work on a custom rakefile.  For those of you unfamiliar, Rake is something akin to Make but written in Ruby.  The context most people use for this is with their Rails apps which come standard with a pre-built rakefile of common commands to perform any number of tasks.  If you have rails application already setup to run on your computer, you can see the list of available tasks by typing:

mark-mac:vo2spot mrampton$ rake -T
(in /Users/mrampton/Documents/Aptana Studio/vo2spot)
rake db:data:dump              # Dump data to the db/ directory. Use MODEL=ModelName and LIMIT (optional)
rake db:data:load              # Load data from the db/ directory. Use MODEL=ModelName
rake db:fixtures:dump          # Dump data to the test/fixtures/ directory. Use MODEL=ModelName and LIMIT (optional)
rake db:fixtures:load          # Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y
rake db:migrate                # Migrate the database through scripts in db/migrate. Target specific version with VERSION=x

I want to use rake outside the context of Rails, however.  What I want is to use rake in a simple script I'll use for deploying and configuring a Java app.

It turns out this is pretty simple.  Simply write the script as you normally would and require the rake gem.

#!/usr/bin/ruby

require 'rake'

Pretty simple.  Now you can begin writing your tasks. Or at least you should be able to.  If I attempt to run just the above I get the following at the command prompt:

./test.rb:3:in `require': no such file to load -- rake (LoadError)
        from ./test.rb:3

Now, if you have confirmed that rake is installed on your machine (which rake) this becomes irritating since the file DOES exist.  But the solution is simple, tell ruby that you're using gems a la:

#!/usr/bin/ruby

require 'rubygems'
require 'rake'

SERVER_DIR = "/home/bin/unix"

desc "Install and start the server"
task :install do
    # perform installation      
end