Hello! I'm trying to use MapStruct on Kotlin Mult...
# kapt
j
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:
Copy code
dependencies {
    kapt("org.mapstruct:mapstruct-processor:latest.release")
}
b
Try wrapping kapt bit in quotes like "kapt"("gradlecoordinatesversion")
j
Understand, I generate with success the implementation of my Mapper with this configuration:
Copy code
configurations["kapt"].dependencies.add(
    project.dependencies.create("org.mapstruct:mapstruct-processor:latest.release")
)
in my
koltin.sourceSets.jvmMain.dependencies
b
Ah! You need to use global dependencies block for kapt (and ksp), not sourceSets
j
Using the global dependencies block returns an error:
Configuration with name 'kapt' not found.
Same with your suggestion and my workaround
b
Odd, it really shouldn't. Are you able to share your full buildscript?
j
Copy code
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
you should not need anything that complex; I think
Copy code
dependencies {
    "kaptJvm"("...mapstruct...")
}
is all you need
Kapt only works with Java btw
j
@ephemient do you mean in the global block or in the sourceSets?
e
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
@ephemient I used "kapt" instead of "kaptJvm" and move down my depenencies block after the kotlin block
Copy code
kotlin {
    jvm()
    ...
}

dependencies {
    "kapt"("org.mapstruct:mapstruct-processor:latest.release")
}
What do you mean @ephemient with "nice generated accessor" and "convention plugin"?
e
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
Uhh cool task, thanks a lot
e
yes, it and
./gradlew outgoingVariants
are useful for debugging Gradle issues between multiplatform library producers and consumers
j
really cool stuffs, it will help a lot
406 Views