Skip to content


banshee edited this page Oct 26, 2011 · 2 revisions
Clone this wiki locally

JRuby on Rails with Spring Support using Maven

Table of Contents

These steps will get you from zero to a working JRuby on Rails app that can access Spring configured beans using maven to manage your dependencies.

Initial Setup

Basic Steps:

  1. For Rails 2
    1. Download jruby somewhere
    2. tar xvfz jruby-bin-1.4.0.tar.gz
    3. mv jruby-1.4.0/ /Applications # I keep stuff here, but the location doesn't matter
    4. cd /Applications
    5. ln -s jruby-1.4.0/ jruby
    6. Add /Applications/jruby/bin to my path
    7. jruby -v # should work
    8. jruby -S gem list
    9. jruby -S gem install rails
    10. jruby -S gem install activerecord-jdbcmysql-adapter
    11. jruby -S gem install jruby-openssl
    12. jruby -S gem install warbler
    13. cd ~/workspace
    14. jruby -S rails app_name
    15. Commit to version control for easy rollbacks:
      1. git init .
      2. git commit -a -m 'initial version' # need a baseline in case we must rollback
    16. jruby -S script/generate resource yourApp
    17. If you don't want ActiveRecord: Add config.frameworks -= [ :active_record ] to environment.rb
    18. create app/views/benchmarks/index.html.erb
    19. remove public/index.html
    20. Add map.root :controller => "benchmarks" to routes.rb
    21. jruby -S script/server
    22. open http://localhost:4000 - your app is running
  2. For Rails 3
    1. Recommend you install [RVM|]
    2. rvm install jruby
    3. gem intall rails (Note that RVM handles the "jruby" stuff for you)
    4. rails new myapp -m -d mysql
    5. cd myapp
    6. Edit your Gemfile as follows:
      • source ''
      • gem 'rails', '3.0.9'
      • gem 'activerecord-jdbc-adapter'
      • gem 'activerecord-jdbcmysql-adapter'
      • gem 'jdbc-mysql'
      • gem 'jruby-openssl'
      • gem 'jruby-rack'
    7. bundle install
    8. Edit your config/database.yml, setting the the adapter: to jdbcmysql
    9. Edit your config/database.yml setting the appropriate database user/passwords
    10. Create the databases yourself - there is a bug that prevents rake db:create from working.
    11. rake db:migrate
    12. rails server
    13. Visit localhost:3000 and click on "About your Application's Environemnt". If you don't see any errors, you are good to go
    14. Commit to version control for easy rollbacks:
      1. git init .
      2. git commit -a -m 'initial version' # need a baseline in case we must rollback
    15. rails generate resource yourApp
    16. If you don't want ActiveRecord: Add config.frameworks -= [ :active_record ] to environment.rb
    17. create app/views/benchmarks/index.html.erb
    18. remove public/index.html
    19. Add map.root :controller => "benchmarks" to routes.rb
    20. rails server
    21. open http://localhost:3000 - your app is running
  3. Commit to version control


How to create Java classes in JRuby.

 blah =

Getting Deps from Maven

We don't need Maven a whole lot, if at all, for Rails development, however it is handy to use it to manage our dependencies, especially if you have parent poms or other re-usable components already in your organization.

Courtesy of this awesome blog post, we have a pom that will copy all of our needed jar files into the rails app's lib/ directory. Note that you have to run this manually whenever you change your dependencies. Your pom.xml will look something like:

                  mkdir -p src/main/webapp/WEB-INF
                  touch src/main/webapp/WEB-INF/web.xml
                  rm -f lib/*.jar
                  cp target/railsApp/WEB-INF/lib/*.jar lib
                  rm -rf target/railsApp*
                  rm -rf src
                  rm -rf *.war tmp/war
                  jruby -S warble && \
                  mv *.war target/railsApp.war

Then, to update dependencies:

 mvn package # downloads jars into target/ dir, 
             # then copies them to the lib dir

Finally, Rails needs to load all of these when it starts up. Create config/initializers/01_java_jars.rb:

 Dir.entries("#{RAILS_ROOT}/lib").sort.each do |entry|
   if entry =~ /.jar$/
     require entry

This basically "loads" all the libs you had maven copy into your app's lib directory. You'll need to restart rails to see this.

One thing that wasn't clear to me with JRuby is what "package roots" are available by default. For example, my company has code with a package rooted at poscore, and ruby code like just didn't work. It seems that only certain common top-level packages are automatically available without qualifying, so you can certainly do:

 c =

I opted to add some methods to application_controller.rb, since I'd be needing these a lot. There are other methods of simulating Java's import something.* hanging around, but this worked for my purposes:

 # Get the poscore package from Java
 def self.poscore; Java::poscore; end

Creating a .war

One of the coolest things about JRuby and Rails is that you can package up your Rails app as a .war and dump it into a J2EE app server:

 jruby -S warble config # this sets up stuff to make a war
 jruby -S warble        # creates blah.war in local dir

Setting up Spring

Basically, you need to get a list of all the spring configuration files you intend to load. If your XML configuration files are somewhere in the classpath in a directory called config, you can get a list of them as such

 # Our config files live in src/main/resources/config
 def beans
 ["configurationContext", "otherContext", "dataContext" ].map { |c|
 }.to_java :string

You can then load them as such:

 context =
 # We have a bean named "someService" configured, so we get it
 service = context.getBean("someService")

You should probably put this in an initializer, naming it to ensure it happens after your java jars are loaded. Using the naming scheme from above, the name config/initializers/02_spring.rb would work as such:


Again, this assumes that our config files are on the classpath in a config subdirectory. Your setup may need to be different.

Now, you can use SPRING_CONTEXT anywhere in your code to access beans. With Ruby's awesome meta-programming, it's not hard to envision some slick method_missing means of getting access to your beans:

 class << context
   alias old_method_missing method_missing
   def method_missing(sym,args)
     if args.empty?
 context.someService # gets our someService bean

That's it! You now have a JRuby on Rails application that can access your existing java-defined Spring beans.