Running JRuby tasks

If your use-case is to simply run some JRuby scripts, you can use the base plugin. This provides a basic task to prepare GEMs and you can register JRubyExec tasks to perform execution from prepared GEMs.

import org.ysb33r.gradle.jruby.api.base.tasks.JRubyExec

plugins {
  id 'org.ysb33r.jruby.base' version '2.0.0'
}

tasks.register('myTask', JRubyExec) { (1)
  script {
      path = file('path/to/my-script.rb') (2)
      name = 'gem' (3)
  }
  process {
      forwardOutput() // If you want to see output in console
      forwardErrorOutput() //If you want to see error output in console
  }
}
1 JRubyExec implements ExecutableScript. Read the Grolifant docs for all the possible configuration.
2 A path to a script on the file system. Use this when you have written a script which you want to execute.
3 A system script. This is equivalent to running with the -S command-line parameter. If not name and path is specified, the latter specified one will overwrite the other.

Modifying the GEM preparation

The plugin also creates a task called gemPrepare which is of type JRubyPrepare. It uses a configuration called gems to declare GEMs.

It is possible to change this to another configuration.

import org.ysb33r.gradle.jruby.api.base.tasks.JRubyPrepare

tasks.named('gemPrepare', JRubyPrepare) {
  gemConfiguration = configurations.rubocop (1)
}
1 Use other configuration. This needs to be a resolvable configuration.

JRubyPrepare-JRubyExec pairs

It is possible to register a task pairing along with a configuration. This takes care of some of the internal wiring.

import org.ysb33r.gradle.jruby.api.base.tasks.JRubyPrepare
import org.ysb33r.gradle.jruby.api.base.tasks.JRubyExec

gemConfigurations {
  register(
    'rubocop', (1)
    JRubyPrepare,  (2)
    JRubyExec,  (3)
    'runRubocop' (4)
   )
}

tasks.named('runRubocop', JRubyExec) {
    script {
        name = 'rubocop'
    }
    process {
        forwardOutput()
        forwardErrorOutput()
    }
}
1 The name of the declarative configuration used to declare GEMs.
2 The type of prepare task to create.
3 The type of execution task to create.
4 The name of the execution task. If this is not specified the name will effectively be run + capitalized version of the configuration name.

Advanced configuration

It is possible to configure execution for JRubyExec in many ways as it is based upon Grolifant’s /https://ysb33rorg.gitlab.io/gradle/grolifant/grolifant-plugin-development5.1.0/project-artifacts/_attachments/-grolifant5-core/groovydoc/org/ysb33r/grolifant5/api/core/runnable/ForkedJvmScript.html[ForkedJvmScript].

import org.ysb33r.gradle.jruby.api.core.JRubyEnvironmentInheritance
import org.ysb33r.grolifant5.api.core.jvm.ExecutionMode

tasks.named('runRubocop', JRubyExec) {
    jvm { (1)
       environment FOO: 'BAR' (2)
       environmentProvider( provider { -> [ foo: 'bar'] } ) (3)
       systemProperties sysFoo : 'sysBar' (4)
    }
    runnerSpec { (5)
      args 'a', 'b'
      addCommandLineArgumentProviders( provider { -> ['a','b']}) (6)
    }
    script { (7)
        name = 'rubocop' (8)
        args 'c', 'd' (9)
    }
    process { (10)
        ignoreExitValue = true (11)
        output {
          captureAndForward() (12)
        }
        errorOutput {
            capture()  (13)
        }
        afterExecute { result -> (14)
           /* ... */
        }
    }

    inheritRubyEnv = JRubyEnvironmentInheritance.EXCLUDE_JRUBY (15)
    executionMode = ExecutionMode.OUT_OF_PROCESS (16)
}
1 This configures the JVM itself. Configuration is essentially the same as for JavaForkOptions.
2 Configure the environment.
3 Add a provider of environment variables.
4 Configure system properties.
5 Configure JRuby arguments.
6 Adds providers of arguments. Each provider must return a list of strings.
7 This configures the Ruby script.
8 Name of the script. Can also use path instead of name when a path to the script is required.
9 Script arguments.
10 Configures the post-processing of the result.
11 Whether the exit value should be ignored.
12 Capture the standard output for post-processing but also forward it to the console.. Only when the execution mode is JAVA_EXEC.
13 Capture the error output for post-processing. Only when the execution mode is JAVA_EXEC.
14 Run this action on the output of the script once execution has completed. This will run even if the script exited with an error. Only when the execution mode is JAVA_EXEC.
15 Set how much of the system Ruby environment will be exposed to the running process.
16 Change the execution mode. The default is JAVA_EXEC.