No suitable driver found for jdbc:sqlite

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

Your example seems close, but you did not add any dependencies to the sql configuration.

configurations {
    sql
}
dependencies {
    sql 'org.xerial:sqlite-jdbc:3.36.0.3'
}

Also, you are creating the database during Gradle’s configuration phase, not during the execution of the createEmptyDB task. Code to be executed during Gradle’s task execution phase should be configured in a task action. This can be achieved by calling doLast.

task createEmptyDB  {
    doLast {
        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()
    }
}

I found another solution and it’s work just fine:

plugins {
    id 'application'
    id "com.nocwriter.runsql" version "1.0.3"
}


ext {
    log4jVersion = "1.2.17"
    toedterVersion = "1.4"

    javaMainClass = "${group}.Main"
    buildResources = "${buildDir}/resources"
    buildResourcesMain = "${buildResources}/main"
    buildResourcesDbSqlitePath = "${buildResourcesMain}/db/sqlite"
    distArchiveFileName = "${projectShortName}-${version}-${distributeVersionType}"
}

task createTablesInDB(type: RunSQL) {
    group 'distDB'
    description 'Create tables in m2cm2.db'
    config {
        username = ""
        password = ""
        url = "jdbc:sqlite:${buildResourcesDbSqlitePath}/${projectShortName}.db"
        driverClassName = "org.sqlite.JDBC"
        scriptFile = "./build/resources/main/db/sqlite/create-tables.sql"
    }
}

And run like this:

./gradlew createTablesInDB