我正在尝试运行多个*.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'
}
}
但我有错误
有什么问题?它找不到.jar
?
答案 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)
}
}