Apply SQL from jar

This topic How to set up the SQL statement when calling ant sql task? helps me to build solution.

I need to recreate Spring Batch tables from Gradle task (as a part of bootstrapping process).

buildscript {
    dependencies {
        classpath 'com.oracle:ojdbc6:11.2.0.3'
    }
}
configurations {
    batch
}

dependencies {
    batch 'org.springframework.batch:spring-batch-core:3.0.8.RELEASE'
}

def extractBatchSql(path) {
    def zipFile = configurations.batch.find { it =~ /spring-batch-core/ }
    def zip = new java.util.zip.ZipFile(zipFile)
    def entry = zip.getEntry(path)
    return zip.getInputStream(entry).text
}

task tmp() {
    dependsOn configurations.batch

    doLast {
        def delSql = extractBatchSql("org/springframework/batch/core/schema-drop-oracle10g.sql")
        ant.sql(classpath: buildscript.configurations.classpath.asPath, driver: "oracle.jdbc.OracleDriver",
                url: "${dbConn}", userid: "${dbUser}", password: "${dbPass}", delSql)
        def createSql = extractBatchSql("org/springframework/batch/core/schema-oracle10g.sql")
        ant.sql(classpath: buildscript.configurations.classpath.asPath, driver: "oracle.jdbc.OracleDriver",
                url: "${dbConn}", userid: "${dbUser}", password: "${dbPass}", createSql)
    }
}

What’s your question?

I don’t have. Just shared my finding.

You never close() the ZipFile so the gradle daemon is likely holding onto file handles

def extractBatchSql(path) {
    def zipFile = configurations.batch.find { it =~ /spring-batch-core/ }
    def zip = new java.util.zip.ZipFile(zipFile)
    def entry = zip.getEntry(path)
    def sql = zip.getInputStream(entry).text
    zip.close()
    return sql
}

Slightly better but still not full marks… it’s not in a finally block :wink:

I am not sure about Closeable:

def extractBatchSql(path) {
    def zipFile = configurations.batch.find { it =~ /spring-batch-core/ }
    def sql = null;
    new java.util.zip.ZipFile(zipFile).withCloseable { zip ->
        def entry = zip.getEntry(path)
        sql = zip.getInputStream(entry).text
    }
    return sql;
}

Additional improvement:

dependencies {
    batch('org.springframework.batch:spring-batch-core:3.0.8.RELEASE') {
        transitive = false
    }
}