如何在 Maven 中使用 Java 中的 Ruby?
How to use Ruby in Java with Maven?
我想 运行 Ruby 在 Java class 中编写代码(不是作为 Maven 插件)并将 SCSS 编译为 CSS(我想我这里需要宝石)。我已经阅读了一些关于如何在 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
您必须在运行时将 jar 放在您的类路径中。 Maven 在编译时看到它们是不够的。
当您使用“-jar”时,您应该构建一个可执行的 jar。请参阅 了解如何通过在生成的 jar 中烘焙相关代码来完成此操作。
我想 运行 Ruby 在 Java class 中编写代码(不是作为 Maven 插件)并将 SCSS 编译为 CSS(我想我这里需要宝石)。我已经阅读了一些关于如何在 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
您必须在运行时将 jar 放在您的类路径中。 Maven 在编译时看到它们是不够的。
当您使用“-jar”时,您应该构建一个可执行的 jar。请参阅 了解如何通过在生成的 jar 中烘焙相关代码来完成此操作。