# Apply SQL from jar

**URL:** <https://discuss.gradle.org/t/apply-sql-from-jar/23613>\
**Category:** Help/Discuss\
**Created:** [August 6, 2017, 1:20pm UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613 "2017-08-06T13:20:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gavenkoa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gavenkoa/32/6263_2.png) [@gavenkoa](https://discuss.gradle.org/u/gavenkoa)\
**Post date:** [August 6, 2017, 1:20pm UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/1 "2017-08-06T13:20:34Z")

</div>

This topic [How to set up the SQL statement when calling ant sql task?](https://discuss.gradle.org/t/how-to-set-up-the-sql-statement-when-calling-ant-sql-task/5743) 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)
    }
}
```

---

<div class="post-metadata">

**Author:** ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)\
**Post date:** [August 7, 2017, 7:24am UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/2 "2017-08-07T07:24:53Z")

</div>

What’s your question?

---

<div class="post-metadata">

**Author:** ![gavenkoa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gavenkoa/32/6263_2.png) [@gavenkoa](https://discuss.gradle.org/u/gavenkoa)\
**Post date:** [August 7, 2017, 11:09am UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/3 "2017-08-07T11:09:45Z")

</div>

I don’t have. Just shared my finding.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 7, 2017, 11:38am UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/4 "2017-08-07T11:38:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gavenkoa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gavenkoa/32/6263_2.png) [@gavenkoa](https://discuss.gradle.org/u/gavenkoa)\
**Post date:** [August 7, 2017, 2:50pm UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/5 "2017-08-07T14:50:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 7, 2017, 4:20pm UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/6 "2017-08-07T16:20:06Z")

</div>

Slightly better but still not full marks… it’s not in a `finally` block 😉

---

<div class="post-metadata">

**Author:** ![gavenkoa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gavenkoa/32/6263_2.png) [@gavenkoa](https://discuss.gradle.org/u/gavenkoa)\
**Post date:** [August 7, 2017, 5:54pm UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/7 "2017-08-07T17:54:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gavenkoa](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/gavenkoa/32/6263_2.png) [@gavenkoa](https://discuss.gradle.org/u/gavenkoa)\
**Post date:** [August 7, 2017, 5:56pm UTC](https://discuss.gradle.org/t/apply-sql-from-jar/23613/8 "2017-08-07T17:56:21Z")

</div>

Additional improvement:

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