在 Gradle 任务中执行 SQL?

发布于 12-09 05:34 字数 713 浏览 0 评论 0原文

如何在 Gradle 任务中执行 SQL?

configurations {
    compile
}
repositories {
    mavenCentral()
}
dependencies {
    compile 'postgresql:postgresql:9.0-801.jdbc4'
}
task sql << {
    driverName = 'org.postgresql.Driver'
    Class.forName(driverName)
    groovy.sql.Sql sql = Sql.newInstance(
        'jdbc:postgresql://localhost:5432/postgres', 
        'username', 
        'password', 
        driverName
    )
    sql.execute 'create table test (id int not null)'
    sql.execute 'insert into test (id) values(1)'
    sql.eachRow 'select * from test' {
        println it
    }
}

我得到一个 执行sql任务时出现java.lang.ClassNotFoundException: org.postgresql.Driver异常。

How can I execute SQL in a Gradle task?

configurations {
    compile
}
repositories {
    mavenCentral()
}
dependencies {
    compile 'postgresql:postgresql:9.0-801.jdbc4'
}
task sql << {
    driverName = 'org.postgresql.Driver'
    Class.forName(driverName)
    groovy.sql.Sql sql = Sql.newInstance(
        'jdbc:postgresql://localhost:5432/postgres', 
        'username', 
        'password', 
        driverName
    )
    sql.execute 'create table test (id int not null)'
    sql.execute 'insert into test (id) values(1)'
    sql.eachRow 'select * from test' {
        println it
    }
}

I get a
java.lang.ClassNotFoundException: org.postgresql.Driver exception when executing the sql task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可爱暴击2024-12-16 05:34:28

要定义 构建脚本的外部依赖项,您必须将将其放入构建脚本的类路径中。您可以通过在 buildscript 闭包中定义它来做到这一点。

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'postgresql:postgresql:9.0-801.jdbc4'
    }
}

To define external dependencies for the build script itself you got to put it into the build scripts' classpath. You can do that by defining it within the buildscript closure.

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'postgresql:postgresql:9.0-801.jdbc4'
    }
}
枉心2024-12-16 05:34:28

发现 runsql-gradle-plugin 允许执行自定义 Gradle 任务中定义的 SQL 脚本文件。

将这些字符串添加到我的 build.gradle.kts 后:

plugins {
    id("com.nocwriter.runsql") version ("1.0.3")
}

task<RunSQL>("initData") {
    dependencies {
        implementation("org.postgresql:postgresql")
    }
    config {
        url = "jdbc:postgresql://localhost:5432/test"
        driverClassName = "org.postgresql.Driver"
        username = "test"
        password = "test"
        scriptFile = "data.sql"
    }
}

我可以通过以下方式执行 data.sql

./gradlew initData

Found runsql-gradle-plugin that allows executing SQL script files defined in custom Gradle task.

After adding these strings to my build.gradle.kts:

plugins {
    id("com.nocwriter.runsql") version ("1.0.3")
}

task<RunSQL>("initData") {
    dependencies {
        implementation("org.postgresql:postgresql")
    }
    config {
        url = "jdbc:postgresql://localhost:5432/test"
        driverClassName = "org.postgresql.Driver"
        username = "test"
        password = "test"
        scriptFile = "data.sql"
    }
}

I could execute data.sql by:

./gradlew initData
陌若浮生2024-12-16 05:34:28

如果您不介意依赖其他工具,则可以在项目中利用 dbdeploy。还有一个 gradle 插件,可让您按原样导入 SQL 脚本 。

If you don't mind depending on yet another tool, you can leverage dbdeploy in your project. There is also a gradle plugin that let you import SQL scripts as is.

旧街凉风2024-12-16 05:34:28
buildscript {
    dependencies {
        classpath 'com.oracle:ojdbc6:11.2.0.3'
    }
}

task tmp() {
    dependsOn configurations.batch
    doLast {
        ant.sql(classpath: buildscript.configurations.classpath.asPath,
            driver: "oracle.jdbc.OracleDriver",
            url: "${dbConn}", userid: "${dbUser}", password: "${dbPass}",
            "select 1 from dual")
    }
}

或者:

ant.sql(classpath: buildscript.configurations.classpath.asPath,
        driver: "oracle.jdbc.OracleDriver",
        url: "${dbConn}", userid: "${dbUser}", password: "${dbPass}") {
    fileset(dir: dir) {
        include(name: "**/*.sql")
    }
}
buildscript {
    dependencies {
        classpath 'com.oracle:ojdbc6:11.2.0.3'
    }
}

task tmp() {
    dependsOn configurations.batch
    doLast {
        ant.sql(classpath: buildscript.configurations.classpath.asPath,
            driver: "oracle.jdbc.OracleDriver",
            url: "${dbConn}", userid: "${dbUser}", password: "${dbPass}",
            "select 1 from dual")
    }
}

Alternatively:

ant.sql(classpath: buildscript.configurations.classpath.asPath,
        driver: "oracle.jdbc.OracleDriver",
        url: "${dbConn}", userid: "${dbUser}", password: "${dbPass}") {
    fileset(dir: dir) {
        include(name: "**/*.sql")
    }
}
南…巷孤猫2024-12-16 05:34:28

这是一种方法:

gradle.class.classLoader.addURL(new File('../../../../lib/server/mssql/sqljdbc4.jar').toURI().toURL())
def Sql sql = Sql.newInstance(dbConnectionURL, dbUserName, dbPassword, dbDriverName)
String sqlString = new File(dbSchemaFile as String).text

sql.execute(sqlString)

Here is one way:

gradle.class.classLoader.addURL(new File('../../../../lib/server/mssql/sqljdbc4.jar').toURI().toURL())
def Sql sql = Sql.newInstance(dbConnectionURL, dbUserName, dbPassword, dbDriverName)
String sqlString = new File(dbSchemaFile as String).text

sql.execute(sqlString)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文