Unable to query a db from gradle build script

2019-02-19 18:01发布

问题:

I am trying to query a db from within a gradle script task. I started with a groovy script to verify the code

import groovy.sql.Sql

this.class.classLoader.rootLoader.addURL(new URL('file:<..>/jtds-1.2.2.jar'))
def driver = 'net.sourceforge.jtds.jdbc.Driver'
def dburl = "jdbc:jtds:sqlserver://ITSVIL:1433/APPDB"
def first
Sql.withInstance(dburl, '<..>', '<..>', driver) {
    sql ->
        first = sql.firstRow( "SELECT * FROM PROJECT" )
}

I launched with groovy QueryTest.groovy and verified that it worked. Then I moved the script within a gradle task, no changes on code but loading handled by gradle buildscript statement

import groovy.sql.Sql

defaultTasks 'queryTest'

buildscript {
    dependencies {
        classpath files('<..>/jtds-1.2.2.jar')
    }
}

task queryTest () {

    // l'update va su REPOSITORYURL + '/' + alm.project.vcrProjectName + '/' + Reference

    doLast {

        def driver = 'net.sourceforge.jtds.jdbc.Driver'
        def dburl = "jdbc:jtds:sqlserver://ITSVIL:1433/APPDB"
        def first
        Sql.withInstance(dburl, '<..>', '<..>', driver) {
            sql ->
                first = sql.firstRow( "SELECT * FROM PROJECT" )
        }
    }
}

Launched with gradle -b QueryTest.gradle, this time I am getting

Execution failed for task ':queryTest'.
> java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver://ITSVIL:1433/IKALM_APP

Tried any possible advice I found in forums without success. Would ask for help.

回答1:

Due to groovy dynamic nature, classloading within gradle is quite complex. I would like to thank @cptwonton that pointed me to a great explanation here

I had to modify the gradle script a little and this is the working version. The jdts-1.2.2.jar is in the lib directory referred by flatDir.

import groovy.sql.Sql

defaultTasks 'queryTest'

task queryTest () {
    repositories {
        flatDir {
            dirs 'lib'
        }
    }
    configurations {
        jdbc
    }
    dependencies {
        jdbc 'net.sourceforge.jtds:jtds:1.2.2'
    }

    doLast {
        def sqlClassLoader = Sql.classLoader
        configurations.jdbc.each { sqlClassLoader.addURL it.toURI().toURL() }

        def driver = 'net.sourceforge.jtds.jdbc.Driver'
        def dburl = "jdbc:jtds:sqlserver://ITSVIL:1433/APPDB"
        def first
        Sql.withInstance(dburl, '<..>', '<..>', driver) {
            sql ->
                first = sql.firstRow( "SELECT * FROM PROJECT" )
        }
    }
}


回答2:

It appears that your net.sourceforge.jtds.jdbc.Driver is not available. This is likely because of an issue with your path in

buildscript {
    dependencies {
        classpath files('..../jtds-1.2.2.jar')
    }
}

The path you provide to classpath files( path ) needs to be a relative path to the root of your gradle project directory. I'd suggest moving your jtds jar into the libs directory, and taking a look at this doc: https://docs.gradle.org/current/userguide/dependency_management.html#sub:file_dependencies

Also, the jtds jar is on the maven repo, why not use that instead of a local jar? The beauty of build tools like gradle and maven is that you do not need to locally store or manage many popular jars.