如何在JRuby Gradle项目中避免“无法加载Java类oracle.jdbc.OracleDriver”?

时间:2016-12-11 22:05:33

标签: oracle jdbc gradle jruby

当尝试在我的JRuby Gradle项目中包含Oracle JDBC驱动程序(ojdbc7.jar)时,我总是在运行时获得“无法加载Java类oracle.jdbc.OracleDriver”。这是我的基本build.gradle:

token.erase(
    std::remove_if(
        token.begin(), 
        token.end(), 
        [](char x){ return ispunct(x) && x != ':'; }), 
    token.end());

这是我的entrypoint.rb:

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"
}

构建+运行步骤的输出:

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

puts "Hello world"

按照How to use oracle jdbc driver in gradle project中的建议,我尝试将其添加到我的build.gradle:

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
     ...

但这会在编译时导致错误:

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

然后我尝试将其添加为How to add OJDBC6.jar in build.gradle file?中建议的运行时依赖项:

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

但这又引发了编译错误:

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

所以我被卡住了 - 如何才能在我的JRuby Gradle项目中正确添加odjbc7.jar(或任何外部jar)作为依赖?

1 个答案:

答案 0 :(得分:1)

该问题的解决方法是将Oracle JDBC驱动程序安装到本地Maven存储库中,并将mavenLocal()添加为存储库,将驱动程序jar添加为附加依赖项。

可以像这样安装驱动程序:

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

最终的build.gradle看起来像这样:

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"
}