Fork me on GitHub

Base plugin

Table of Contents
Build Status
download

Getting Started

Compatibility

This plugin requires Gradle 2.0 or better

Installing

build.gradle
buildscript {
    repositories { jcenter() }

    dependencies {
        /* check jruby-gradle.org for the latest release */
        classpath "com.github.jruby-gradle:jruby-gradle-plugin:[1.1.4,2.0)"
    }
}

apply plugin: 'com.github.jruby-gradle.base'

Adding gems

You can also add Ruby gem dependencies in your build.gradle file under the gems configuration which is provided by plugin, e.g.:

build.gradle
dependencies {
    gems group: 'rubygems', name: 'sinatra', version: '1.4.5'
    /* alternatively */
    gems 'rubygems:rake:10.3.+'
    /* prereleased gems needs to pick a specific version */
    gems 'rubygems:rails:4.2.3.rc1'
}
Prereleased gems can only be a specific version due to limitations of mapping a rubygems repository to a maven repository.
This functionality currently relies on a deployed version of the rubygems-servlets software to help proxy a Rubygem repository into a Maven repository format. This ensures that Gradle can resolve gem-based dependencies. Currently the plugin release on a Rubygems Maven proxy operated by R. Tyler Croy

Tasks

JRubyExec

In a similar vein to JavaExec and RhinoShellExec, the JRubyExec allows for Ruby scripts to be executed in a Gradle script using JRuby.

build.gradle
import com.github.jrubygradle.JRubyExec

dependencies {
    jrubyExec 'rubygems:credit_card_validator:1.2.0'
}

task runMyScript(type: JRubyExec) {
    script 'scripts/runme.rb'
    scriptArgs '-x', '-y'
}

Common methods for JRubyExec for executing a script

  • script - Object (Usually File or String). Path to the script.

  • scriptArgs - List. List of arguments to pass to script.

  • workingDir - Object (Usually File or String). Working directory for script.

  • environment - Map. Environment to be set. Do not set GEM_HOME or GEM_PATH with this method.

  • standardInput - InputStream. Set an input stream to be read by the script.

  • standardOutput - OutputStream. Capture the output of the script.

  • errorOutput - OutputStream. Capture the error output of the script.

  • ignoreExitValue - Boolean. Ignore the JVm exit value. Exit values are only effective if the exit value of the Ruby script is correctly communicated back to the JVM.

  • configuration - String. Configuration to copy gems from. (*)

  • classpath - List. Additional Jars/Directories to place on classpath.

  • jrubyVersion - String. JRuby version to use if not the same as ```project.jruby.execVersion```.

  • gemWorkDir - File. Provide a custom working directory for unpacking GEMs. By default each JRubyExec task uses it’s own work directory. Use this to set a common work directory for a number of tasks.

(*) If jrubyVersion has not been set, jrubyExec will used as configuration. However, if jrubyVersion has been set, a configuration must also be provded to maintain dependency isolation, see this page for more details.

Additional JRubyExec methods for controlling the JVM instance

jrubyexec extension

Similar to javaexec and exec it is possible to add the execution of a JRuby script to another task:

build.gradle
task needSomeRubyLove {
  jrubyexec {
    script 'scripts/runme.rb'
    scriptArgs '-x', '-y'
  }
}

The behaviour of project.jrubyexec is slightly different to that of JRubyExec.

  • The version of jruby-complete is strictly tied to the jruby.execVersion. Therefore trying to set jrubyVersion in the ```jrubyexec``` closure will cause a failure

  • GEMs and additional JARs are only taken from the jrubyExec configuration.

  • It is not possible to supply a configuration parameter to the jrubyexec closure.

  • GEMs will be installed to jruby.gemInstallDir. Existing gems will not be overwritten.

As with JRubyExec, args, setArgs and main are illegal within the jrubyexec closure. All other methods should work.

Running a Ruby PATH command

Because JRubyExec checks for the existence of the script, it might look at first whether running Ruby commands from PATH could be difficult. However, this is totally possible by utilising jrubyArgs and passing -S as one would do when using ruby or jruby on the command-line. Here is an example of running rake as task.

build.gradle
task rake(type : JRubyExec) {
    script 'rake'
    scriptArgs '/path/to/Rakefile', 'target1', 'target2'
}

or even

build.gradle
ext {
    rake = { String target ->
        jrubyexec {
            jrubyArgs '-S'
            script 'rake'
            scriptArgs '/path/to/Rakefile', target
        }
    }
}

JRubyPrepare

Unpacking occurs using the default jruby version as set by jruby.execVersion.

build.gradle
import com.github.jrubygradle.JRubyPrepare

task unpackMyGems(type : JRubyPrepare) {

  // Parent directory for unpacking GEMs.
  // Gems will end up in a subdirectory 'gems/GemName-GemVersion'
  outputDir buildDir

  // Add one or more gems
  // Can be String(s), File(s), FileCollection(s) or Configuration(s)
  dependencies project.configurations.gems
}

Advanced Usage

Using the built-in Gem proxy

As of version 1.1.0, the JRuby/Gradle base plugin supports embedding a rubygems-servlets process inside the Gradle process itself. This obviates the need for a third-party proxy to present the Maven-compatible dependency information that JRuby/Gradle relies on.

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

/* Disable our default repositories */
jruby.defaultRepositories false

repositories {
    jcenter()
    rubygems('https://rubygems.org')
}

dependencies {
    gems 'rubygems:sinatra:1.4.5'
}

Using a custom Gem repository

By default the jruby plugin will use rubygems.lasanga.io as its source of Ruby gems. This is a server operated by R. Tyler Croy and it presents a Maven repository of the data from rubygems.org.

If you do not wish to use this repository, you can run your own Maven proxy repository for either rubygems.org or your own gem repository by running the rubygems-servlets server.

You can then use that custom Gem repository with:

build.gradle
jruby {
    defaultRepositories false
}

repositories {
    maven { url 'http://localhost:8989/releases' }
}

dependencies {
    gems 'rubygems:my-custom-gem:1.0.1'
}

Using the JRuby/Gradle without Gradle

There are still plenty of cases, such as for local development, when you might not want to create a full .war file to run some tests. In order to use the same gems and .jar based dependencies, add the following to the entry point for your application:

# Hack our GEM_HOME to make sure that the `rubygems` support can find our
# unpacked gems in build/gems/
vendored_gems = File.expand_path(File.dirname(__FILE__) + '/build/gems')
if File.exists?(vendored_gems)
  ENV['GEM_HOME'] = vendored_gems
end
The .rb file is assuming it’s in the top level of the source tree, i.e. where build.gradle is located

improve this page