How to execute SQL file using gradle


(Surbhi Jain) #1

I tried using using following syntax:
task test << {
gradle.class.classLoader.addURL(new File(‘SQL/ojdbc6.jar’).toURI().toURL())

def sql = groovy.sql.Sql.newInstance(JDBCURL, DB_PROP_SCHEMA, DB_PROP_PASSWORD, ‘oracle.jdbc.driver.OracleDriver’)

String sqlFilePath = “testsql.sql”

String sqlString = new File(sqlFilePath).text

sql.execute(sqlString)

sql.close()
}


(Stefan Oehme) #2

What you are trying to do is modifying the builscript classpath:

buildscript {
  dependencies {
    classpath files('SQL/ojdbc6.jar')
  }
}

(nathan.niesen) #3

Two other approaches:

via buildscript:

buildscript {
    dependencies {
        classpath("net.sourceforge.jtds:jtds:1.3.1")
    }
}
// Configure the classloader to contain JTDS driver (i.e., first jar starting with jtds)
gradle.class.classLoader.addURL(buildscript.configurations.classpath.find { it.name.startsWith('jtds') }.toURI().toURL())

via configuration:

configurations {
    driver
}
dependencies {
    driver "com.h2database:h2:1.3.171"
}
configurations.driver.each {File file ->
    gradle.class.classLoader.addURL(file.toURI().toURL())
}

** Updated to use Gradle classloader


(Stefan Oehme) #4

You shouldn’t do either of those, the Groovy classloader is an implementation detail that can break at any point.