0

When trying to include the Oracle JDBC driver (ojdbc7.jar) in my JRuby Gradle project, I always get a "cannot load Java class oracle.jdbc.OracleDriver" at runtime. Here's my basic build.gradle:

buildscript {
    repositories { jcenter() }

    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:[1.2.2,2.0)'

        classpath 'com.github.jruby-gradle:jruby-gradle-plugin:%%VERSION%%'
        classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.3.3'
    }
}

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

repositories { jcenter() }

dependencies {
    jrubyJar "rubygems:colorize:0.7.7+"
    jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
}

jrubyJar {
    initScript "${projectDir}/entrypoint.rb"
}

And here's my entrypoint.rb:

require 'java'
java_import 'java.sql.DriverManager'
java_import 'oracle.jdbc.OracleDriver'

puts "Hello world"

Output of build + run steps:

frank$ ./gradlew jrubyJar
:prepareJRubyJar UP-TO-DATE
:jrubyJar UP-TO-DATE

BUILD SUCCESSFUL

Total time: 2.027 secs

frank$ java -jar build/libs/plsql-unit-tester-jruby.jar
NameError: cannot load Java class oracle.jdbc.OracleDriver
     ...

Following the advice in How to use oracle jdbc driver in gradle project, I tried adding this to my build.gradle:

dependencies {
    compile files('lib/ojdbc7.jar')
}

But this causes an error at compile time:

Could not find method compile() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I then tried to add it as a runtime dependency as suggested in How to add OJDBC6.jar in build.gradle file?:

 dependencies {
    runtime files('lib/odjbc7.jar')
}

But this again raises a compile error:

Could not find method runtime() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

So I'm stuck - how can I correctly add odjbc7.jar (or any external jar) as a dependency in my JRuby Gradle project?

Community
  • 1
  • 1
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107

1 Answers1

1

A workaround for the problem is to install the Oracle JDBC driver into the local Maven repository and add the mavenLocal() as repository and the driver jar as an additional dependency.

The driver can be installed like so:

mvn install:install-file -Dfile=ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar

The final build.gradle looks like this:

buildscript {
    repositories { jcenter() }

    dependencies {
        classpath 'com.github.jruby-gradle:jruby-gradle-plugin:1.3.3'
        classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.3.3'        
    }
}

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

repositories { 
    mavenLocal()
    jcenter()
}

dependencies {
    jrubyJar "rubygems:colorize:0.7.7+"
    jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
    jrubyJar 'com.oracle:ojdbc7:12.1.0.2'
}

jrubyJar {
    initScript "${projectDir}/entrypoint.rb"
}