I would like to run Ruby code inside of a Java class (not as Maven plugin) and compile SCSS to CSS (I guess I need gems here). I already read some articles about how to use it in a Maven Java project but I it seems the ScriptEngine didn't get registered.
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jruby-test</groupId>
<artifactId>jruby-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>rubygems-releases</id>
<url>http://rubygems-proxy.torquebox.org/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>9.0.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.script.jruby</groupId>
<artifactId>jruby-engine</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>jruby.test.App</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>1.1.4</version>
<configuration>
<includeRubygemsInResources>true</includeRubygemsInResources>
</configuration>
<executions>
<execution>
<goals>
<goal>initialize</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
jruby.test.App:
package jruby.test;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
public class App {
public static void main(String[] args) {
ScriptEngineManager mgr = new ScriptEngineManager();
for (ScriptEngineFactory factory : mgr.getEngineFactories()) {
System.out.println("ScriptEngineFactory Info");
System.out.printf("\tScript Engine: %s (%s)\n",
factory.getEngineName(), factory.getEngineVersion());
System.out.printf("\tLanguage: %s (%s)\n",
factory.getLanguageName(), factory.getLanguageVersion());
for (String name : factory.getNames()) {
System.out.printf("\tEngine Alias: %s\n", name);
}
}
}
}
Console output:
$ java -jar target/jruby-test-0.0.1-SNAPSHOT.jar
ScriptEngineFactory Info
Script Engine: Rhino (Rhino 1.7 release 4 2013 08 27)
Language: ECMAScript (1.7)
Engine Alias: js
Engine Alias: rhino
Engine Alias: JavaScript
Engine Alias: javascript
Engine Alias: ECMAScript
Engine Alias: ecmascript
You must have the jars on your classpath at runtime. It is not enough for Maven to see them at compile time.
As you use "-jar" you should build an executable jar. See https://stackoverflow.com/a/574650/53897 for how to do it by baking in dependent code in your resulting jar.