I want to execute sql script (SQLite) by Gradle’s custom task. I try this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.xerial:sqlite-jdbc:3.36.0.3'
}
}
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
// Build uber-jar
id 'com.github.johnrengelman.shadow' version '7.1.2' apply false
}
apply plugin: "com.github.johnrengelman.shadow"
dependencies {
implementation 'log4j:log4j:1.2.17'
implementation 'org.xerial:sqlite-jdbc:3.36.0.3'
implementation 'com.toedter:jcalendar:1.4'
dependencies { implementation name: 'ssp-0.5.6' }
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}
def db = [
driver: "org.sqlite.JDBC",
url: "jdbc:sqlite:c:\\dev\\m2cm\\data\\m2cm.db",
user: '',
password: ''
]
configurations {
sql
}
task createEmptyDB {
configurations.sql.each { file ->
println "Adding URL: $file"
gradle.class.classLoader.addURL(file.toURI().toURL())
}
def sql = groovy.sql.Sql.newInstance(db.url, db.user, db.password, db.driver)
sql.execute("create-table.sql")
String sqlFilePath = "c:\\dev\\temp\\m2cm\\app\\build\\resources\\main\\db\\sqlite\\create-tables.sql"
String sqlString = new File(sqlFilePath).text
sql.execute(sqlString)
sql.close()
}
When I try
gradle createEmptyDB
I get error:
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\dev\temp\m2cm\app\build.gradle' line: 142
* What went wrong:
A problem occurred evaluating project ':app'.
> No suitable driver found for jdbc:sqlite:c:\dev\m2cm\data\m2cm.db