Sqldelight: Kotlin Gradle DSL: Provide complete example

Created on 17 Feb 2020  路  2Comments  路  Source: cashapp/sqldelight

Hi,

I'm having problems using the sqldelight Gradle plugin with the Kotlin DSL.

Here is my build.gradle.kts file:

import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.squareup.sqldelight:gradle-plugin:1.2.1")
    }
}

apply(plugin = "com.squareup.sqldelight")

plugins {
    kotlin("jvm") version "1.3.50"
}

repositories {
    google()
    mavenLocal()
    mavenCentral()
    jcenter()
}

sourceSets["main"].withConvention(KotlinSourceSet::class) {
    kotlin.srcDir("src/main/kotlin")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

sqldelight {
    database("CtxDatabase") {
        packageName = "de.matthjes.ctx.persistence"
        schemaOutputDirectory = file("build/dbs")
    }
    linkSqlite = false
}

dependencies {
   ...
}

The sqldelight configuration is highlighted red in IntelliJ and I get the following error on build:

e: /Users/mjeschke/Development/private/ctx/build.gradle.kts:41:1: Unresolved reference: sqldelight
e: /Users/mjeschke/Development/private/ctx/build.gradle.kts:42:5: Unresolved reference: database
e: /Users/mjeschke/Development/private/ctx/build.gradle.kts:43:9: Unresolved reference: packageName
e: /Users/mjeschke/Development/private/ctx/build.gradle.kts:44:9: Unresolved reference: schemaOutputDirectory
e: /Users/mjeschke/Development/private/ctx/build.gradle.kts:46:5: Unresolved reference: linkSqlite

Could someone please provide an example for using sqldelight with the Kotlin Gradle DSL?

Most helpful comment

I'm now using the approach detailed in #1496, i.e. listing the repositories inside settings.gradle.kts and defining a custom resolution strategy.

Is this the currently preferred approach? It wasn't clear for me from the ticket whether a resolution strategy is still required or not (does not seem to work without it, though).

For reference, this is now working for me:

settings.gradle.kts:

pluginManagement {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        google()
        gradlePluginPortal()
    }

    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "com.squareup.sqldelight") {
                useModule("com.squareup.sqldelight:gradle-plugin:1.2.1")
            }
        }
    }
}

build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.3.61"
    id("com.squareup.sqldelight")
}

repositories {
    google()
    mavenLocal()
    mavenCentral()
    jcenter()
}

sourceSets["main"].withConvention(KotlinSourceSet::class) {
    kotlin.srcDir("src/main/kotlin")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

sqldelight {
    database("CtxDatabase") {
        packageName = "de.matthjes.ctx.db"
        schemaOutputDirectory = file("build/dbs")
    }
    linkSqlite = false
}

dependencies {
 ...
}

All 2 comments

I'm now using the approach detailed in #1496, i.e. listing the repositories inside settings.gradle.kts and defining a custom resolution strategy.

Is this the currently preferred approach? It wasn't clear for me from the ticket whether a resolution strategy is still required or not (does not seem to work without it, though).

For reference, this is now working for me:

settings.gradle.kts:

pluginManagement {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        google()
        gradlePluginPortal()
    }

    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "com.squareup.sqldelight") {
                useModule("com.squareup.sqldelight:gradle-plugin:1.2.1")
            }
        }
    }
}

build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.3.61"
    id("com.squareup.sqldelight")
}

repositories {
    google()
    mavenLocal()
    mavenCentral()
    jcenter()
}

sourceSets["main"].withConvention(KotlinSourceSet::class) {
    kotlin.srcDir("src/main/kotlin")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

sqldelight {
    database("CtxDatabase") {
        packageName = "de.matthjes.ctx.db"
        schemaOutputDirectory = file("build/dbs")
    }
    linkSqlite = false
}

dependencies {
 ...
}

I'm now using the approach detailed in #1496, i.e. listing the repositories inside settings.gradle.kts and defining a custom resolution strategy.
...

Thank you @matthjes this resolved my issue as well. :)

Including other relevant errors to help with SEO:

Gradle can't resolve plugin

./gradlew build

> Configure project :
e: /home/rgmz/dev/sqldelight-project/build.gradle.kts:58:1: Unresolved reference: sqldelight

FAILURE: Build failed with an exception.

* Where:
Build file '/home/rgmz/dev/sqldelight-project/build.gradle.kts' line: 58

* What went wrong:
Script compilation error:

  Line 58: sqldelight {
           ^ Unresolved reference: sqldelight

1 error

Gradle not generating models

./gradlew build

> Task :compileKotlin FAILED
e: /home/rgmz/dev/sqldelight-project/src/main/kotlin/com/example/sqldelight/App.kt: (6, 31): Unresolved reference: hockey
e: /home/rgmz/dev/sqldelight-project/src/main/kotlin/com/example/sqldelight/App.kt: (23, 20): Unresolved reference: Database
e: /home/rgmz/dev/sqldelight-project/src/main/kotlin/com/example/sqldelight/App.kt: (25, 24): Unresolved reference: PlayerQueries

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
1 actionable task: 1 executed
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tellypresence picture tellypresence  路  4Comments

jrodbx picture jrodbx  路  4Comments

JiongBull picture JiongBull  路  3Comments

aegis123 picture aegis123  路  4Comments

kuhnroyal picture kuhnroyal  路  4Comments