Using JRuby in Maven Project
6 May 2014 by StefanIntegrating a ruby environment into a Java maven project is quite easy. I recently built BlenderCss, a java project that needed to use the Compass gem. To solution was to utilize the gem via jruby.
You will need..
1. Dependencies Section in pom.xml
...
<!-- The Jruby Dependency -->
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>1.7.12</version>
</dependency>
...
2. The ruby class you plan to use.
class MyApi
end
JRubyRunner.java
1 import org.jruby.Ruby;
2 import org.jruby.javasupport.JavaEmbedUtils;
3
4 import java.util.ArrayList;
5 import java.util.List;
6
7 public class JRubyRunner {
8 public static void main(String[] args) {
9 List paths = new ArrayList();
10 paths.add("classpath:gems/compass-0.12.2/lib");
11 Ruby ruby = JavaEmbedUtils.initialize(paths);
12 ruby.getLoadService().require("compass"); // require "compass"
13 ruby.evalScriptlet("Compass::Exec::SubCommandUI.perform! :compile"); // perform a compile command.
14 }
15 }
Note: jRuby is slow to initialize, so use it wisely.