How can I use gems-in-a-jar with inline JRuby?

I would like to use excellent Sass as part of my build process. My build process is controlled by Gradle . This means creating a plugin to run Sass. Sass is Ruby, and Gradle is in Groovy, but since Groovy runs on the JVM, I can use JRuby to run it using the Java scripting API . Gradle scripts get dependencies as jar files; I can get JRuby from Maven Central , but I will need the Sass package itself as a jar.

I tried to complete the Nick Sieger procedure in a stone-in-bank for this, but was unsuccessful. No matter what I do, I just can't get JRuby to pick up the Sass stone.

To isolate the problem, I wrote a minimal Java program (not Gradle) that tries to use Sass through JRuby - I ingeniously call it JSass:

There are two or three interesting bits. The first is a script generate-gem-jar.sh that creates a gem jar:

gem install -i build/gems sass --no-rdoc --no-ri
jar cf lib/gemsass.jar -C build/gems .

The second class is so.demo.JSass , which runs JRuby:

ScriptEngine rubyEngine = new ScriptEngineManager().getEngineByName("jruby");
ScriptContext context = rubyEngine.getContext();
context.setAttribute("inputFile", inputFile, ScriptContext.ENGINE_SCOPE);
String script = IOUtils.toString(JSass.class.getResourceAsStream("sassdriver.rb"));
rubyEngine.eval(script, context);

The third is a Ruby script that the main class is trying to use to control Sass:

require 'sass'
template = File.load(inputFile)
sass_engine = Sass::Engine.new(template)
output = sass_engine.render
puts output

The program can load the JRuby engine and execute the script, but it does not work in require 'sass', saying:

LoadError: no such file to load -- sass
  require at org/jruby/RubyKernel.java:1033
   (root) at <script>:1
org.jruby.embed.EvalFailedException: (LoadError) no such file to load -- sass
    at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:132)
    at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:90)
    at so.demo.JSass.main(JSass.java:24)
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- sass
Exception in thread "main" javax.script.ScriptException: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- sass
    at org.jruby.embed.jsr223.JRubyEngine.wrapException(JRubyEngine.java:115)
    at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:93)
    at so.demo.JSass.main(JSass.java:24)
Caused by: org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- sass

What am I doing wrong?

+5
1

, , java -jar -rgems.jar , .jar, sass-xxx ( gem install sass) lib $LOAD_PATH... , , :

rubyEngine.eval("$LOAD_PATH << 'file://gemsass.jar!/gems/sass-3.1.15/lib'", context);

rubyEngine.eval(script, context);

, , , .

rubyEngine.eval("$LOAD_PATH << 'file:/home/42/gems/sass-3.1.15/lib'", context);

, , !

0

All Articles