Gradle:无法执行sql,找不到驱动程序 [英] Gradle: cannot execute sql, driver not found

查看:469
本文介绍了Gradle:无法执行sql,找不到驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了下面的 gradle 脚本。它导致找不到合适的驱动程序我发现这个错误。

  buildscript {
repositories {
maven {
url ='http:// localhost:8090 / nexus / content / groups / public /'
}
}
依赖项{
classpath'com.oracle:ojdbc6:11.2.0.4.0'
}
}

任务sql<< {
def url ='jdbc:oracle:thin:@'+ project.properties ['db_hostname'] +':'+ project.properties ['db_port'] +':'+ project.properties ['db_sid ']
println'sql,db url:'+ url
def driverName ='oracle.jdbc.OracleDriver'
Class.forName(driverName).newInstance();
groovy.sql.Sql sql = groovy.sql.Sql.newInstance(
url,
project.properties ['db_username'],
project.properties ['db_password'] ,
driverName

}

错误是:

 要执行的任务:[task':sql'] 
:sql(Thread [main,5,main])开始。
:sql
由于:
执行任务':sql'(最新检查花费了0.0秒)任务没有声明任何输出。
truncating,db url:jdbc:oracle:thin:@LOCALHOST:1521:orcl
:sql失败
:sql(Thread [main,5,main])完成。花了0.105秒。

失败:构建失败,出现异常。

*其中:
构建文件'/home/fran/projects/jua/build.gradle'行:49

*出错:
任务':sql'的执行失败。
>找不到适合jdbc的驱动程序:oracle:thin:@LOCALHOST:1521:orcl

*尝试:
使用--debug选项运行以获取更多日志输出。


解决方案

驱动程序jar被加载到不同的类加载器上下文中比Sql类。为了解决这个问题,试着添加下面的代码:

  //将通过buildscript classpath配置解析的jar添加到Sql将使用的类加载器
URLClassLoader loader = groovy.sql.Sql.class.classLoader
project.buildscript.configurations.classpath.each {File file - >
loader.addURL(file.toURL())
}

之前调用 Sql.newInstance()。它将使加载 Sql 类的Groovy类加载器可以使用所有类路径依赖项。 Class.forName()不应该是必须的。



有关更多信息,请参阅此处的讨论:
http:// gradle。

I have got the following gradle script. It results in No suitable driver found error which I found strange.

buildscript {
    repositories{
        maven {
            url = 'http://localhost:8090/nexus/content/groups/public/'
        }
    }
    dependencies {
        classpath 'com.oracle:ojdbc6:11.2.0.4.0'
    }
}

task sql << {
    def url = 'jdbc:oracle:thin:@' + project.properties['db_hostname'] + ':' + project.properties['db_port'] + ':' + project.properties['db_sid']
    println 'sql, db url:' + url
    def driverName = 'oracle.jdbc.OracleDriver'
    Class.forName(driverName).newInstance();
    groovy.sql.Sql sql = groovy.sql.Sql.newInstance(
            url,
            project.properties['db_username'],
            project.properties['db_password'],
            driverName
    )
}

The error is:

Tasks to be executed: [task ':sql']
:sql (Thread[main,5,main]) started.
:sql
Executing task ':sql' (up-to-date check took 0.0 secs) due to:
  Task has not declared any outputs.
truncating, db url:jdbc:oracle:thin:@LOCALHOST:1521:orcl
:sql FAILED
:sql (Thread[main,5,main]) completed. Took 0.105 secs.

FAILURE: Build failed with an exception.

* Where:
Build file '/home/fran/projects/jua/build.gradle' line: 49

* What went wrong:
Execution failed for task ':sql'.
> No suitable driver found for jdbc:oracle:thin:@LOCALHOST:1521:orcl

* Try:
Run with --debug option to get more log output.

The driver jar is being loaded in a different classloader context than the Sql class. To fix it, try adding this:

// Add jars resolved by buildscript classpath configuration to the classloader which Sql will use 
URLClassLoader loader = groovy.sql.Sql.class.classLoader
project.buildscript.configurations.classpath.each { File file ->
  loader.addURL(file.toURL())
}

prior to the call to Sql.newInstance(). It will make all your classpath dependencies available to the Groovy classloader which loads the Sql class. The Class.forName() should not be neccesary.

See the discussion here for more information: http://gradle.1045684.n5.nabble.com/using-jdbc-driver-in-a-task-fails-td1435189.html

这篇关于Gradle:无法执行sql,找不到驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆