Blogroll
25 May 10 Deploying a JRuby On Rails app in Tomcat
I’ve been curious to get a little deeper into Ruby On Rails to see what all the fuss is about, even though I personally have a strong preference for Apache Wicket.
On my Mac, I found that I was running into a bunch of problems running RoR from standard Ruby - basically your good old native-library dependency hell, so I decided to use JRuby instead.
I have to say, JRuby is excellent, probably more predictable and reliable than “natively built” Ruby (let’s call it “C-Ruby”), with the added boon that you can deploy your Rails app on any Java App-server, such as Tomcat, thus hopefully by-passing a lot of the scalability- and reliability issues that RoR has been associated with.
Installing and Understanding JRuby
Installing JRuby, assuming you already have Java installed, is as simple as downloading the binary package, unpacking the zip-file and adding the ${JRUBY}/bin folder to you PATH-variable.
One of my first fears with installing JRuby was that the JRuby installed “Gems” (equivalent to a Java jar/library) would clash with C-Ruby gems. You don’t have to worry about this: JRuby and C-Ruby store their gems in different places, JRuby has a sub-folder of it’s own that it uses for gem-storage.
Secondly, to know which version of ruby you are using is as simple: for JRuby, call “jruby”, for ruby it’s “ruby”. For any Ruby based applications/commands it’s a little more tricky, but not much: for using a “gem install [some gem]” to install a gem package, in Ruby you just call the command as mentioned. For JRuby you need to prefix the gem, rails etc commands with “jruby -S”, for instance “jruby -S gem install rails”.
Setting up Rails with JRuby
This is where Gem comes in, run the following commands to install the necessary packages:
jruby -S gem install rake
jruby -S gem install rack
jruby -S gem install rails
jruby -S gem install jdbc-mysql
jruby -S gem install activerecord-jdbcmysql-adapter
jruby -S gem install warbler
Setting up a simple App that uses MySQL
At this point, you can create an app by doing “jruby -S rails [myapp]”. To make the app use MySQL, open the [myapp]/config/database.yml file and change the appropriate development, test and production stanzas to something like this:
adapter: jdbcmysql
username: myuser
password: mypassword
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
pool: 5
timeout: 5000
Preparing for deployment
Did you wonder what the warble install did? Well, Warble happens to be a very useful little tool that will create a deployable WAR file from your RoR file. But first you need to run “warble config” in the project root to generate a config file, then edit the config/warble.rb file to add a line that looks something like this:
config.gems += [“activerecord-jdbcmysql-adapter”, “jruby-openssl”]
This will instruct warble to package the mysql and openssl dependencies with your RoR app, if you have more dependencies that you are using, you’ll need to add these in that line as well.
At this point, you can go back to your project root and simply run “warble” on the command line. At this point, warble should generate a war file with the name [yourproject].war. That’s it! You should now have a fully deployable WAR file that you can deploy into any Servlet container.
The ease of RoR with the power of Java. And what more, because you are using JRuby, you can interface with any other Java or Scala libraries with relative ease.
As for my take on Wicket vs. Rails? Well, that’s a matter of a different post, but I don’t necessarily see it as a “versus”, rather different horses for different courses..
