jruby-gradle/jruby-gradle-plugin

Build script is not downloading/installing gems needed by JRubyExec

amimas opened this issue · 5 comments

Hello,

I'm relatively new to this plugin. I've been going through the documentation as well as the examples in this repository but I cannot seem to get a simple build script setup. Below is my build.gradle file, which is taken from the one of the examples in this repository that has a Gradle task for executing a ruby script, that has a dependency on a ruby gem.

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
      classpath "com.github.jruby-gradle:jruby-gradle-plugin:2.0.0"
  }
}

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

import com.github.jrubygradle.JRubyExec

repositories {
  ruby.gems()
}

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

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

task runGradleTest {
    dependsOn printSomePrettyOutputPlease
}

When I run ./gradlew runGradleTest, I get the following error:

* What went wrong:
A problem occurred evaluating root project 'sup-zendesk-help-center'.
> Could not find method jrubyExec() for arguments [rubygems:colorize:0.7.7+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

In the dependencies block of the Gradle script, I tried swapping the configuration from jrubyExec to gems and that produces the following error:

* What went wrong:
Execution failed for task ':printSomePrettyOutputPlease'.
> Could not resolve all files for configuration ':detachedConfiguration1'.
   > Could not find org.jruby:jruby-complete:9.2.9.0.
     Required by:
         project :

The above error goes away if I add a repository. For example:

repositories {
  ruby.gems()
  jcenter()
}

But I still can't get the ruby script executed. It comes with following error, which shows that the required gem hasn't been downloaded:

LoadError: no such file to load -- colorize
  require at org/jruby/RubyKernel.java:978
  require at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:54
   <main> at /Users/amimas/projects/jruby-gradle-test/print-script.rb:1

It's also not very clear from the documentation on when gems configurations should be used. What did I miss in the above build script?

After a lot of trial and error, I managed to get the example working. Here's my complete build.gradle script:

buildscript {
  repositories {
    jcenter()
  }

  dependencies {
      classpath "com.github.jruby-gradle:jruby-gradle-plugin:2.0.0"
  }
}

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

import com.github.jrubygradle.JRubyExec

repositories {
  ruby.gems()
  jcenter()
}

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

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


/*
 * This task is only here for the execution fo the gradleTest
 */
task runGradleTest {
    dependsOn printSomePrettyOutputPlease
}

I'm not 100% sure, but pretty sure I had tried this but it didn't work. Not knowing what to do, I decided to look into my gradle cache, ~/.gradle/caches/modules-2/files-2.1/rubygems/ and I did see the colorize gem in there. Since it wasn't working, I decided to delete the entire rubygems directory from the gradle cache.

After that, couple of attempts and the above build script snippet worked successfully. 🤷‍♂

Thanks for filing this and I'm sorry we never got around to helping out! I'm going to work with the owner of jruby-gradle to move this under the jruby organization, so we can help maintain and respond to requests.

It looks like this may be mostly a matter of documentation? I'm glad you were able to figure out how to make it work, but perhaps we should have more examples that help the next person.

Thanks for your reply. Glad to see some movement in this repo. Ultimately I couldn't use this in my use-case because of issues/bugs but I think this has a lot of potential.

I think I am also seeing something similar to this. I was trying to add a JRuby constraint-programming example (invoked via Gradle) as per here:
https://github.com/paulk-asert/groovy-constraint-programming/tree/master/subprojects/SendMoreMoneyJRuby

The gem free SendMoreMoneyBruteForce.rb example works fine as does the PrintScript.rb (copy of print-script) colorize example (after working around #288) but the SendMoreMoneyConstraints.rb script fails with:

Exception `Gem::MissingSpecError' at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/rubygems/dependency.rb:311 - Gem::MissingSpecError
Exception `LoadError' at org/jruby/RubyKernel.java:978 - no such file to load -- csp-solver
LoadError: no such file to load -- csp-solver
  require at org/jruby/RubyKernel.java:978
  require at uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:54
   <main> at D:\projects\groovy-constraint-programming\subprojects\SendMoreMoneyJRuby\src\main\ruby\SendMoreMoneyConstraints.rb:15

GEM_HOME seems fine and the gems are sitting under the gradle cache and build/.gems/gems/ albeit in slightly different layouts.

/CC @ysb33r

i have try, before i get error too. but after i add jcenter() in repositories. it work as expected. this is full my build.gradle

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn more about Gradle by exploring our samples at https://docs.gradle.org/6.7.1/samples
 */
import com.github.jrubygradle.JRubyExec


plugins {
    id "com.github.jruby-gradle.base" version "2.1.0-alpha.2"
}

repositories {
    ruby.gems()
    jcenter()
}

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

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

/*
 * This task is only here for the execution of the gradleTest
 */
task runGradleTest {
    dependsOn printSomePrettyOutputPlease
}