https://kotlinlang.org logo
Title
j

Jerv Norsk

02/21/2023, 12:21 PM
Hello! I'm trying to use MapStruct on Kotlin Multiplatform. At the moment I'm using Kotlin Multiplatform 1.8.0, same version for kotlin-kapt with Gradle 8.0 and OpenJDK 19. I had try to use kapt in the dependencies block of my gradle but I can not found the function. Is kapt dependency function removed from plugin? If yes: how I can tell kapt to use the mapstruct-processor? My not buildable gradle build file block:
dependencies {
    kapt("org.mapstruct:mapstruct-processor:latest.release")
}
b

Big Chungus

02/21/2023, 12:43 PM
Try wrapping kapt bit in quotes like "kapt"("gradle:coordinates:version")
j

Jerv Norsk

02/21/2023, 12:48 PM
Understand, I generate with success the implementation of my Mapper with this configuration:
configurations["kapt"].dependencies.add(
    project.dependencies.create("org.mapstruct:mapstruct-processor:latest.release")
)
in my
koltin.sourceSets.jvmMain.dependencies
b

Big Chungus

02/21/2023, 12:49 PM
Ah! You need to use global dependencies block for kapt (and ksp), not sourceSets
j

Jerv Norsk

02/21/2023, 12:51 PM
Using the global dependencies block returns an error:
Configuration with name 'kapt' not found.
Same with your suggestion and my workaround
b

Big Chungus

02/21/2023, 12:52 PM
Odd, it really shouldn't. Are you able to share your full buildscript?
j

Jerv Norsk

02/21/2023, 12:53 PM
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    // Kotlin
    // ------------------------------------------------------------------------
    kotlin("multiplatform")
    kotlin("kapt")

    // Kotlin Plugin
    // ------------------------------------------------------------------------
    kotlin("plugin.serialization")
    kotlin("plugin.spring")

    // Spring
    // ------------------------------------------------------------------------
    id("org.springframework.boot")
    id("io.spring.dependency-management")
}

dependencies {
    configurations["kapt"].dependencies.add(
        project.dependencies.create("org.mapstruct:mapstruct-processor:latest.release")
    )
}

kotlin {
    jvm {
        withJava()
        tasks.withType<KotlinCompile> {}
        tasks.withType<Test> {
            useJUnitPlatform()
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("reflect"))
            }
        }
        val jvmMain by getting {
            dependencies {
                // Spring
                // ------------------------------------------------------------
                implementation("org.springframework.boot:spring-boot-starter")
                implementation("org.springframework.boot:spring-boot-starter-actuator")
                implementation("org.springframework.boot:spring-boot-starter-webflux")
                implementation("org.springframework.boot:spring-boot-starter-data-jpa")
                implementation("org.springframework.boot:spring-boot-starter-validation")

                // Blueprint
                // ------------------------------------------------------------
                implementation("org.mapstruct:mapstruct:latest.release")

                // Datasource
                // ------------------------------------------------------------
                implementation("org.liquibase:liquibase-core")
                implementation("mysql:mysql-connector-java:8.0.30")

                // Documentation
                // ------------------------------------------------------------
                implementation("org.springdoc:springdoc-openapi-starter-common:${extra["springdoc.openapi.version"]}")
                implementation("org.springdoc:springdoc-openapi-starter-webflux-ui:${extra["springdoc.openapi.version"]}")
            }
        }
        val jvmTest by getting {
            dependencies {
                // Spring
                // ------------------------------------------------------------
                implementation("org.springframework.boot:spring-boot-starter-test")
            }
        }
    }
}
Here I'm using the global dependencies block an it returns the error above
e

ephemient

02/21/2023, 1:10 PM
you should not need anything that complex; I think
dependencies {
    "kaptJvm"("...mapstruct...")
}
is all you need
Kapt only works with Java btw
j

Jerv Norsk

02/21/2023, 1:13 PM
@ephemient do you mean in the global block or in the sourceSets?
e

ephemient

02/21/2023, 1:13 PM
global
also has to come after
kotlin { jvm() }
(you'd get nice generated accessors if you set up the platforms in a convention plugin)
j

Jerv Norsk

02/21/2023, 1:16 PM
@ephemient I used "kapt" instead of "kaptJvm" and move down my depenencies block after the kotlin block
kotlin {
    jvm()
    ...
}

dependencies {
    "kapt"("org.mapstruct:mapstruct-processor:latest.release")
}
What do you mean @ephemient with "nice generated accessor" and "convention plugin"?
e

ephemient

02/21/2023, 1:20 PM
generated accessor: being able to write
kapt
instead of
"kapt"
convention plugin: https://docs.gradle.org/current/samples/sample_convention_plugins.html
ah, my earlier suggestion was a bit wrong. I just added
plugins { kotlin("kapt") }
to a multiplatform project I had and
./gradlew resolvableConfigurations
does print out
kapt
and
kaptTest
, not sourceset-specific as I expected
but regardless of the name, it will only apply to the `jvmMain`/`jvmTest` sourcesets
j

Jerv Norsk

02/21/2023, 1:52 PM
Uhh cool task, thanks a lot
e

ephemient

02/21/2023, 1:54 PM
yes, it and
./gradlew outgoingVariants
are useful for debugging Gradle issues between multiplatform library producers and consumers
j

Jerv Norsk

02/21/2023, 1:55 PM
really cool stuffs, it will help a lot