!DOCTYPE html> JRuby Bridge Tutorial - Dealing with Gems

JRuby Bridge Tutorial - Dealing with Gems

Installing Gems

Where we would simply use in Ruby gem, we have to use either jruby -S gem, for instance

jruby -S gem install --install-dir INSTALLDIR GEMNAME

or, if we use only the java-complete file, without installying jruby.exe etc., we would write for instance

java -jar c:\dl\jruby-complete-1.7.23.jar -S gem install --install-dir INSTALLDIR GEMNAME

Keeping gems separate from the application

If the gem directory will not be deployed with the application - for example, because the deployment site maintains their own version of the gems -, it is sufficient to set the environment variable GEM_HOME to the gem install directory (INSTALLDIR in the previous chapter).

Integrating gems with the application

IsolatedScriptingContainer

If the application is delivered as a jar file, with the gems included, the application needs to create an IsolatedScriptingContainer instead of a ScriptingContainer:

// Java
import org.jruby.embed.IsolatedScriptingContainer;
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.LocalContextScope;
....
    ScriptingContainer container = new IsolatedScriptingContainer(LocalContextScope.CONCURRENT);

The IsolatedScriptingContainer creates environment variables GEM_HOME and GEM_PATH and has them point to the correct location inside the jar file.

generate_dir_info

Further, the following code needs to be run after each modification of the local gem repository (i.e. installation, updating or deleting of a gem). It is perhaps easiest (safest) to always do this just before creating the jar file:

cd INSTALLDIR # top level directory for gems
jruby -r jruby/commands -e "JRuby::Commands.generate_dir_info './gems'"
jruby -r jruby/commands -e "JRuby::Commands.generate_dir_info './specifications'"

This creates files with the name .jrubydir inside the gem tree. Technically, this is needed only on Java platforms (such as IBM Web Sphere) which do not allow the content of the embedding Jar file to be listed. Each .jrubydir is a simple text file, which just contains a list of the directory entries of the containing directory. If the JRuby gem loader needs to search the Gem tree (for locating a gem), it uses these files to navigate around.

Placing the Gems in the Jar

If you have installed the Gems with --install-dir INSTALLDIR, respectively -i INSTALLDIR, creating the jar must use the -C option for the gem directory, like this:

jar cvfm ...... -C INSTALLDIR .

Note the single period as final argument. The effect of this switch is, that jar does a cd to INSTALLDIR before including ., i.e. the working directory, which in this case is exactly the install directory. This is necessary, because the fact, that we have a local gem directory in our build environment, is NOT reflected inside the jar