未找到`org.postgresql.Driver`的任务类执行失败

时间:2014-05-13 09:57:20

标签: gradle

我正在尝试运行多个*.sql脚本。我的gradle任务看起来像这样

task initdb(dependsOn: initProperties) << {
Class.forName(props.getProperty("db.driver"))
sql = Sql.newInstance(
        props.getProperty("db.url"),
        props.getProperty("db.admin"),
        props.getProperty("db.password"),
        props.getProperty("db.driver")
);
FileTree tree = null
//create database
fileTree(dir: 'src/main/sql/db', include: "0010_create_data_base.sql").each {
    sql.execute(it.text)
}
//create schema
fileTree(dir: 'src/main/sql/db', include: "0020_create_schema.sql").each {
    sql.execute(it.text)
}
//create tables
fileTree(dir: 'src/main/sql/tables', include: "**.sql").each {
    sql.execute(it.text)
}
//load data
fileTree(dir: 'src/main/sql/data', include: "**.sql").each {
    sql.execute(it.text)
}
}

属性文件包含

db.admin=admin
db.admin.password=pass
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432

我对驱动程序的依赖

dependencies {
    //jdbc postresql
    compile 'org.postgresql:postgresql:9.2-1004-jdbc4'
}

还有一个类路径

buildscript {
    dependencies {
        classpath 'org.postgresql:postgresql:9.2-1004-jdbc4'
    }
}

但我有错误

enter image description here

有什么问题?它找不到.jar

1 个答案:

答案 0 :(得分:0)

这是如何在我的机器上运行的。

我添加了配置

configurations {
    driver
}

我的依赖关系看起来像这样

dependencies {
    driver 'org.postgresql:postgresql:9.2-1003-jdbc4'
}

我的buildscript依赖

buildscript {
    dependencies {
         classpath 'org.postgresql:postgresql:9.2-1003-jdbc4'
    }
}

这是我的initdb任务

task initdb(dependsOn: initProperties) << {
URLClassLoader loader = GroovyObject.class.classLoader as URLClassLoader
configurations.driver.each { 
    File file -> loader.addURL(file.toURI().toURL())
}
Class driver = loader.loadClass(props.getProperty("db.driver")) //org.postgresql.Driver
def sql = Sql.newInstance(
        props.getProperty("db.url") + "/postgres",
        props.getProperty("db.admin"),
        props.getProperty("db.admin.password"),
        props.getProperty("db.driver")
);
FileTree tree = null
//create database
fileTree(dir: 'src/main/sql/db', include: "0010_create_data_base.sql").each {
    sql.execute(it.text)
}
//initalizing it again to new database
sql = Sql.newInstance(
        props.getProperty("db.url") + "/" + props.getProperty("db.name"),
        props.getProperty("db.admin"),
        props.getProperty("db.admin.password"),
        props.getProperty("db.driver")
);
//create schema
fileTree(dir: 'src/main/sql/db', include: "0020_create_schema.sql").each {
    sql.execute(it.text)
}
//create tables
fileTree(dir: 'src/main/sql/tables', include: "**.sql").each {
    sql.execute(it.text)
}
//load data
fileTree(dir: 'src/main/sql/data', include: "**.sql").each {
    sql.execute(it.text)
}
}