messaliberty

about hulor and us

[JRuby on Rails on GAE/J] how-to put rubygems into a jar file to get around file limitations

GAE (Google App Engine) has a limit number to the number files you can upload.
To get around this limit, we need to compress rubygems into jar files.

There may not be easy way to load rubygems in jar files with JRuby.
But with JRuby on GAE/J, it’s easy to load.

  • JRuby on Rails on GAE/J, can load rubygems in RAILS_ROOT/vendor/gems, just do require “gem-name”.
  • But rubygems often contain a lot of files and that can eat into the quota pretty quickly.
  • JRuby on GAE/J, can load rubygems from jar files.
  • Therefore it makes sense to put rubygems into jar files, and I am going to show you how step by step.

Steps:

  • install rubygems into a specific temporary directory (not into the system dir)
    • use -i option to install rugygems into a temporary directory.
    • use –no-rdoc and –no-ri options, to skip to install rdoc and ri, to reduce jar file size.
  • compress rubygems by jar command in the temporary directory.
  • add require “installed-gem.jar” line in RAILS_ROOT/config/environment.rb

Example (scrapi):

% cd RAILS_ROOT
% cd lib/
% jgem search -r scrapi

*** REMOTE GEMS ***

assaf-scrapi (1.2.1)
scrapi (1.2.0)
scrapin-a-livin (0.1.1)
% jgem install -i scrapi-tmp scrapi --no-rdoc --no-ri
Successfully installed tidy-1.1.2
Successfully installed scrapi-1.2.0
2 gems installed
% cd scrapi-tmp/
% ls
cache  doc  gems  specifications
% ls gems/
scrapi-1.2.0  tidy-1.1.2
% ls specifications/
scrapi-1.2.0.gemspec  tidy-1.1.2.gemspec
% jar cf ../scrapi-1.2.0.jar gems/ specifications/
% cd ..
% ls
...  scrapi-1.2.0.jar  ...
% rm -rf scrapi-tmp/
% cd ..

add following line in config/environment.rb

require 'lib/scrapi-1.2.0.jar'

With dev_appserver.sh or appspot.com,  rubygems load fine with steps above.
But executing ruby script by jruby command, cannot load rubygems.

I found only one way, shown below, to load scrapi in jar file.

require "file:///path/to/rubygems/scrapi-1.2.0.jar!gems/scrapi-1.2.0/lib/scrapi"

Maybe there are better ways than this.  If there are, please share in the comments below, thanks!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Reddit
  • StumbleUpon
  • Technorati
  • MySpace
  • Tumblr
  • Yahoo! Buzz
  • Twitter

Related posts:

  1. [Rails] use DataTime class with ActiveRecord column ActiveRecord’s :datetime corresponds to ruby Time class. But Time class...
  2. Lucky Star on Rails A few months ago, suddenly I got a message with...

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply