1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import groovy.sql.Sql import java.sql.Driver import java.sql.DriverManager configurations { driver } buildscript { ext.postgresVersion = '42.1.1' dependencies { classpath("org.postgresql:postgresql:${postgresVersion}") } } dependencies { driver "org.postgresql:postgresql:${postgresVersion}" } // register jdbc drivers: URLClassLoader loader = GroovyObject.class.classLoader configurations.driver.each { File file -> loader.addURL(file.toURI().toURL()) } Class driverClass = loader.loadClass('org.postgresql.Driver') Driver driverInstance = driverClass.newInstance() DriverManager.registerDriver(driverInstance) task fooBar() { doLast { def sql = Sql.newInstance('jdbc:postgresql://localhost:5432/database', 'user', 'password') sql.execute(/* ... */) sql.close() } } |
im Wesentlichen von hier.