Unable to query a db from gradle build script

后端 未结 2 1556
春和景丽
春和景丽 2021-01-14 19:07

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.r         


        
2条回答
  •  旧时难觅i
    2021-01-14 19:20

    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" )
            }
        }
    }
    

提交回复
热议问题