Apply SQL from jar


(Oleksandr Gavenko) #1

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

(René Groeschke) #2

What’s your question?


(Oleksandr Gavenko) #3

I don’t have. Just shared my finding.


(uklance) #4

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


(Oleksandr Gavenko) #5
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
}

(uklance) #6

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


(Oleksandr Gavenko) #7

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;
}

(Oleksandr Gavenko) #8

Additional improvement:

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