将 Gradle 从 Groovy 转换为 Kotlin DSL(用于 liquibase-gradle-plugin)
Convert Gradle from Groovy to Kotlin DSL (for liquibase-gradle-plugin)
这是我在 Groovy DSL 中使用 liquibase-gradle-插件的 Gradle 文件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.liquibase:liquibase-core:3.4.1'
classpath 'org.liquibase:liquibase-gradle-plugin:2.0.1'
classpath 'org.postgresql:postgresql:42.2.5'
}
}
apply plugin: 'liquibase'
repositories {
mavenCentral()
}
dependencies {
liquibaseRuntime 'org.liquibase:liquibase-core:3.4.1'
liquibaseRuntime 'org.liquibase:liquibase-gradle-plugin:2.0.1'
liquibaseRuntime 'org.postgresql:postgresql:42.2.5'
}
task('dev') {
doLast {
println "executing dev"
liquibase {
activities {
main {
changeLogFile 'C:\Users\redacted\IdeaProjects\Food\src\main\resources\changelog.xml'
url 'jdbc:postgresql://localhost/mydb'
username 'postgres'
password 'redacted'
}
}
}
println "Done running dev."
}
}
这是我尝试将文件转换为 Kotlin DSL 的尝试:
plugins {
id("org.liquibase.gradle") version "2.0.1"
}
repositories {
mavenCentral()
}
dependencies {
compile("org.liquibase:liquibase-core:3.4.1")
compile("org.liquibase:liquibase-gradle-plugin:2.0.1")
compile("org.postgresql:postgresql:42.2.5")
add("liquibaseRuntime", "org.liquibase:liquibase-core:3.4.1")
add("liquibaseRuntime", "org.liquibase:liquibase-gradle-plugin:2.0.1")
add("liquibaseRuntime", "org.postgresql:postgresql:42.2.5")
}
tasks.register("dev") {
doLast {
println("executing dev")
"liquibase" {
"activities" {
"main" {
"changeLogFile"("C:\Users\redacted\IdeaProjects\Food\src\main\resources\changelog.xml")
"url"("jdbc:postgresql://localhost/mydb")
"username"("postgres")
"password"("redacted")
}
}
}
println("Done running dev")
}
}
一切都在"liquibase"
行分崩离析。我对 Gradle 不够熟悉 - 在文件的 groovy 版本中, liquibase
是如何解析的?它解决了什么——它是一个函数吗?我如何让它在 Kotlin 版本中得到相同的解决?然后在那之下,我还需要解决 activities
、main
、changeLogFile
、url
、username
和 password
...
尝试将 liquibase 扩展的配置移动到顶层:
plugins {
id("org.liquibase.gradle") version "2.0.1"
}
...
liquibase {
activities.register("main") {
this.arguments = mapOf(
"logLevel" to "info",
"changeLogFile" to "src/main/resources/db.changelog.xml",
"url" to "jdbc:postgresql://localhost/dbName",
"username" to "userName",
"password" to "secret")
}
}
tasks.register("dev") {
// depend on the liquibase status task
dependsOn("update")
}
这是我在 Groovy DSL 中使用 liquibase-gradle-插件的 Gradle 文件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.liquibase:liquibase-core:3.4.1'
classpath 'org.liquibase:liquibase-gradle-plugin:2.0.1'
classpath 'org.postgresql:postgresql:42.2.5'
}
}
apply plugin: 'liquibase'
repositories {
mavenCentral()
}
dependencies {
liquibaseRuntime 'org.liquibase:liquibase-core:3.4.1'
liquibaseRuntime 'org.liquibase:liquibase-gradle-plugin:2.0.1'
liquibaseRuntime 'org.postgresql:postgresql:42.2.5'
}
task('dev') {
doLast {
println "executing dev"
liquibase {
activities {
main {
changeLogFile 'C:\Users\redacted\IdeaProjects\Food\src\main\resources\changelog.xml'
url 'jdbc:postgresql://localhost/mydb'
username 'postgres'
password 'redacted'
}
}
}
println "Done running dev."
}
}
这是我尝试将文件转换为 Kotlin DSL 的尝试:
plugins {
id("org.liquibase.gradle") version "2.0.1"
}
repositories {
mavenCentral()
}
dependencies {
compile("org.liquibase:liquibase-core:3.4.1")
compile("org.liquibase:liquibase-gradle-plugin:2.0.1")
compile("org.postgresql:postgresql:42.2.5")
add("liquibaseRuntime", "org.liquibase:liquibase-core:3.4.1")
add("liquibaseRuntime", "org.liquibase:liquibase-gradle-plugin:2.0.1")
add("liquibaseRuntime", "org.postgresql:postgresql:42.2.5")
}
tasks.register("dev") {
doLast {
println("executing dev")
"liquibase" {
"activities" {
"main" {
"changeLogFile"("C:\Users\redacted\IdeaProjects\Food\src\main\resources\changelog.xml")
"url"("jdbc:postgresql://localhost/mydb")
"username"("postgres")
"password"("redacted")
}
}
}
println("Done running dev")
}
}
一切都在"liquibase"
行分崩离析。我对 Gradle 不够熟悉 - 在文件的 groovy 版本中, liquibase
是如何解析的?它解决了什么——它是一个函数吗?我如何让它在 Kotlin 版本中得到相同的解决?然后在那之下,我还需要解决 activities
、main
、changeLogFile
、url
、username
和 password
...
尝试将 liquibase 扩展的配置移动到顶层:
plugins {
id("org.liquibase.gradle") version "2.0.1"
}
...
liquibase {
activities.register("main") {
this.arguments = mapOf(
"logLevel" to "info",
"changeLogFile" to "src/main/resources/db.changelog.xml",
"url" to "jdbc:postgresql://localhost/dbName",
"username" to "userName",
"password" to "secret")
}
}
tasks.register("dev") {
// depend on the liquibase status task
dependsOn("update")
}