如何在Java中使用Ruby与Maven?

时间:2016-02-28 10:51:49

标签: java maven jruby

我想在Java类(而不是Maven插件)中运行Ruby代码并将SCSS编译为CSS(我想我需要gems)。我已经阅读了一些关于如何在Maven Java项目中使用它的文章但我似乎没有注册ScriptEngine。

的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);
            }
        }
    }
}

控制台输出:

$ 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

1 个答案:

答案 0 :(得分:2)

您必须在运行时在类路径上拥有jar。 Maven在编译时看到它们是不够的。

当你使用&#34; -jar&#34;你应该建立一个可执行jar。有关如何通过在生成的jar中依赖代码进行烘焙,请参阅https://stackoverflow.com/a/574650/53897