Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Gradle from Groovy to Kotlin DSL (for liquibase-gradle-plugin)

Here's my Gradle file using liquibase-gradle-plugin in Groovy DSL:

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."
    }
}

Here's my attempt at converting the file over to Kotlin DSL instead:

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

It all falls apart at the line "liquibase". I'm not familiar enough with Gradle - in the groovy version of the file, how is liquibase being resolved? What is it resolving to - is it a function? How do I get it to be resolved the same in the Kotlin version? Then under that, I'll also need to resolve activities, main, changeLogFile, url, username, and password...

like image 277
ArtOfWarfare Avatar asked Dec 17 '22 16:12

ArtOfWarfare


1 Answers

try to move the configuration of the liquibase extension to the top level:

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")
}
like image 184
abendt Avatar answered Apr 26 '23 20:04

abendt