Fork me on GitHub

JRuby/Gradle

jruby gradle

JRuby/Gradle is a collection of Gradle plugins which make it easy to build, test, manage and package Ruby applications. By combining the portability of JRuby with Gradle’s excellent task and dependency management, JRuby/Gradle provides high quality build tooling for Ruby and Java developers alike.

Plugins available:

Quick Start

Running some Ruby

The simplest example is a hello-world from Gradle, using JRuby/Gradle we can execute a Ruby script, which has Ruby-based dependencies:

build.gradle
apply plugin: "com.github.jruby-gradle.base"

import com.github.jrubygradle.JRubyExec

dependencies {
    /* Using the built-in `jrubyExec` configuration to describe the
     * dependencies our JRubyExec-based tasks will need
     */
    jrubyExec "rubygems:colorize:0.7.7+"
}

task printSomePrettyOutputPlease(type: JRubyExec) {
    description "Execute our nice local print-script.rb"
    script "${projectDir}/print-script.rb"
}
print-script.rb
require 'colorize'

puts "-" * 20
puts "Ruby version: #{RUBY_VERSION}"
puts "Ruby platform: #{RUBY_PLATFORM}"
puts "-" * 20

puts "Roses are red".red
puts "Violets are blue".blue
puts "I can use JRuby/Gradle".green
puts "And now you can too!".yellow

Executing ./gradlew printSomePrettyOutputPlease results in the following:

print script output

Packaging some Ruby

build.gradle
apply plugin: "com.github.jruby-gradle.jar"

repositories { jcenter() }

dependencies {
    /* Using the built-in `jrubyJar` configuration to describe the
     * dependencies our jrubyJar task will need, so the gem is properly
     * included in the resulting .jar file
     */
    jrubyJar "rubygems:colorize:0.7.7+"
    jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
}

jrubyJar {
    /* We want to use this Ruby script as our start point when the jar executes
     */
entrypoint.rb
require 'colorize'

java_import 'org.slf4j.Logger'
java_import 'org.slf4j.LoggerFactory'

logger = LoggerFactory.getLogger('demo')

puts "-" * 20
logger.info "Ruby version: #{RUBY_VERSION}"
logger.info "Ruby platform: #{RUBY_PLATFORM}"
logger.info "Current file: #{__FILE__}"
puts "-" * 20

puts "Roses are red".red
puts "Violets are blue".blue
puts "I can use JRuby/Gradle".green
puts "And now you can too!".yellow

.

Executing ./gradlew jrubyJar will build a .jar file inside of the build/libs directory which can then be invoked:

% java -jar build/libs/self-executing-jar-jruby.jar

self executing jar output