zoukankan      html  css  js  c++  java
  • Using JRuby with Maven

    Using JRuby with Maven - Musings of a Programming Addict

    Bookmark and Share

    One of the features new to Java 6 is its built-in support for scripting languages (an introductory article on that topic can be found here), which enables you to load and execute programs written in a scripting language directly from within your Java program.

    The scripting support in Java 6 is realized by providing an implementation of JSR 223 ("Scripting for the Java Platform"). While JavaScript is directly supported by the JDK itself, any other scripting language can be integrated as well by simply adding a JSR 223 compatible scripting engine to the class path.

    The first place to look for JSR 223 scripting engines is https://scripting.dev.java.net/, where engines for Ruby, Python, Groovy and a lot of other languages can be found.

    To give it a try, I wanted to include the JRuby engine into a Maven based project. That engine can be found in the Maven repo at java.net, but unfortunetaly its pom.xml is somewhat defective, as it contains the dependency script-api:javax.script, which neither exists in the java.net repository nor any other one I am aware of.

    But when running on Java 6, it isn't required at all – as the scripting API is part of the JDK. So I excluded the dependency in the pom.xml of my own project, which reads as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    
    <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/maven-v4_0_0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.gm</groupId>
        <artifactId>jruby-scripting</artifactId>
        <packaging>jar</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>jruby-scripting</name>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jruby</groupId>
                <artifactId>jruby</artifactId>
                <version>1.1.6</version>
            </dependency>
            <dependency>
                <groupId>com.sun.script.jruby</groupId>
                <artifactId>jruby-engine</artifactId>
                <version>1.1.6</version>
                <exclusions>
                    <exclusion>
                        <groupId>script-api</groupId>
                        <artifactId>javax.script</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <repositories>
            <repository>
                <id>maven2-repository.dev.java.net</id>
                <name>Java.net Repository for Maven 2</name>
                <url>http://download.java.net/maven/2/</url>
            </repository>
        </repositories>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>RELEASE</version>
                    <configuration>
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    Having fixed that, we can try the scripting API by evaluating a simple Ruby script, that receives a variable provided by the hosting Java program and returns the obligatory String "Hello, jruby!" ;-):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    package org.gm.jrubyscripting;
    
    import static org.junit.Assert.*;
    
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    
    import org.junit.Test;
    
    public class HelloJRuby {
    
        @Test
        public void helloJRuby() throws Exception {
    
            String engineName = "jruby";
    
            ScriptEngineManager manager = new ScriptEngineManager();
    
            ScriptEngine jRubyEngine = manager.getEngineByName(engineName);
            assertNotNull(jRubyEngine);
    
            jRubyEngine.put("engine", engineName);
    
            assertEquals(
                "Hello, jruby!",
                jRubyEngine.eval("return 'Hello, ' + $engine + '!'"));
        }
    }
    

    But what to do, if you are not running on Java 6? After some more searching I finally managed to find the missing dependency in the repo of the Mule project, but with groupId and artifactId interchanged.

    From there it can be added to the project, for example using a separate Maven build profile to be activated on JDK versions < 1.6:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    ...
    <profiles>
        <profile>
            <activation>
                <jdk>[1.3,1.6)</jdk>
            </activation> 
            <dependencies>
                <dependency>
                    <groupId>javax.script</groupId>
                    <artifactId>script-api</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
            <repositories>
                <repository>
                    <id>dist.codehaus.org/mule</id>
                    <name>Mule Repository for Maven 2</name>
                    <url>http://dist.codehaus.org/mule/dependencies/maven2/</url>
                </repository>
            </repositories>
        </profile>
    </profiles>
    ...
    
  • 相关阅读:
    使用 VisualVM 进行性能分析及调优
    数据库乱码,频繁中断
    mysql 查看数据库中所有表的记录数
    MySQL数据库设置远程访问权限方法小结
    详解使用DockerHub官方的mysql镜像生成容器
    Docker的安装和使用说明——Docker for Windows
    CentOS7安装Docker与使用篇
    正则提取字符串中的字符串
    java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)
    mysql function 中使用游标
  • 原文地址:https://www.cnblogs.com/lexus/p/2353521.html
Copyright © 2011-2022 走看看