How Do I Resolve This Type Mismatch? - kotlin

So I was working on converting my build.gradle (Which uses Groovy) to a build.gradle.kts (Which uses Kotlin) and this is what I got so far:
Groovy Original:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
group 'com.smushytaco'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
url 'https://papermc.io/repo/repository/maven-public/'
}
}
dependencies {
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.3.72'
//https://papermc.io/javadocs/paper/1.16/overview-summary.html
compileOnly group: 'com.destroystokyo.paper', name: 'paper-api', version: '1.16.1-R0.1-SNAPSHOT'
}
shadowJar {
getArchiveClassifier().set('')
project.configurations.implementation.canBeResolved = true
configurations = [project.configurations.implementation]
relocate 'kotlin', 'com.smushytaco.src.main.kotlin.com.smushytaco.plugin'
}
build.dependsOn shadowJar
Kotlin Conversion:
plugins {
java
kotlin("jvm") version("1.3.72")
id("com.github.johnrengelman.shadow") version("5.2.0")
}
group = "com.smushytaco"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://papermc.io/repo/repository/maven-public/")
}
dependencies {
implementation("org.jetbrains.kotlin", "kotlin-stdlib", "1.3.72")
//https://papermc.io/javadocs/paper/1.16/overview-summary.html
compileOnly("com.destroystokyo.paper", "paper-api", "1.16.1-R0.1-SNAPSHOT")
}
tasks {
shadowJar {
archiveClassifier.set("")
project.configurations.implementation.get().isCanBeResolved = true
// Type Mismatch:
// Required: (Mutable)List<Configuration!>!
//Found: Array<NamedDomainObjectProvider<Configuration>>
configurations = [project.configurations.implementation]
relocate("kotlin", "com.smushytaco.src.main.kotlin.com.smushytaco.plugin")
}
build {
dependsOn(shadowJar)
}
}
I commented the error I'm getting with the Kotlin rewrite, why does it work with the original Groovy one and not with this one? How do I fix this error?

Use this:
configurations = listOf(project.configurations.implementation.get())

Related

JLink: Cannot find module-info.java in project

I was trying to create runtime for my project, but suddenly I got an error and I couldn't find any info about it. The project is setted up as kotlin+gradle and nothing more.
I use badass-jlink-plugin and log4j2. The error:
Execution failed for task ':prepareModulesDir'.
> Error while evaluating property 'moduleName' of task ':prepareModulesDir'
> Failed to query the value of extension 'jlink' property 'moduleName'.
> Cannot find module-info.java in [ProjectFolder\src\main\java]
My build.gradle (not full):
buildscript {
...
}
plugins {
id 'application'
id 'org.jetbrains.kotlin.jvm' version '1.8.0'
id 'org.beryx.jlink' version '2.26.0'
// id 'org.jetbrains.kotlin.multiplatform' version '1.8.0'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.0'
}
apply plugin: 'kotlin'
apply plugin: 'kotlinx-serialization'
group = 'com.adisalagic.codenames'
version = '1.0-SNAPSHOT'
mainClassName = 'com.adisalagic.codenames.Main'
repositories {
mavenCentral()
}
dependencies {
// implementation "org.jetbrains.kotlin:kotlin-serialization:1.8.0"
testImplementation 'org.jetbrains.kotlin:kotlin-test'
// implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0-RC")
implementation 'org.apache.logging.log4j:log4j-api:2.19.0'
implementation 'org.apache.logging.log4j:log4j-core:2.19.0'
implementation 'com.google.code.gson:gson:2.10.1'
}
test {
useJUnitPlatform()
}
jlink {
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
forceMerge 'log4j-api'
}
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
tasks.withType(Jar).configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes["Main-Class"] = "$mainClassName"
}
}
kotlin {
// jvmToolchain(8)
}
Should I create module-info myself?
I've discovered that it only happened when I use forceMerge. But everywhere it is suggested to be used.

Unresolved reference for gradle dependency

All the classes of the library are shown in the libs folder but when I try to use any reference to it, it just says it is an unresolved reference.
Unresolved reference error
build.gradle.kts:
plugins {
kotlin("jvm") version "1.8.0"
kotlin("plugin.serialization") version "1.8.0"
id("com.github.johnrengelman.shadow") version "7.1.2"
id("io.papermc.paperweight.userdev") version "1.3.6"
}
group = "me.supehsloht"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://repo.rapture.pw/repository/maven-snapshots/")
}
dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.8.0")
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1")
compileOnly("com.charleskorn.kaml:kaml:0.49.0")
paperDevBundle("1.19.3-R0.1-SNAPSHOT")
compileOnly(files("libs/Core.jar"))
compileOnly(files("libs/AdvancedSlimeWorldManagerAPI.jar"))
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}
tasks {
compileJava {
options.encoding = Charsets.UTF_8.name()
options.release.set(17)
}
compileKotlin {
kotlinOptions.jvmTarget = "17"
}
reobfJar {
outputJar.set(layout.buildDirectory.file("libs/${project.name.capitalize()}.jar"))
}
}
I already had this problem, then it was fixed by not shading the kotlin stdlib into the jar but that didn't work this time.

Kotlin NoSuchMethodError after installing a library that enables a plugin

I want to experience a library called arrow analysis
My build.gradle.kts file looks as follows:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.10"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
implementation("io.arrow-kt:arrow-core:1.1.2")
}
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
after an attempt to build the project, I get the following error:
java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.psi.KtExpression org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt.getReceiverExpression(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)'
at arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.KotlinResolvedCall.getReceiverExpression(KotlinResolvedCall.kt:37)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.allArgumentExpressions(ResolvedCallUtils.kt:75)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.arg(ResolvedCallUtils.kt:119)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.controlFlowAnyFunction(Expressions.kt:438)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.checkCallExpression(Expressions.kt:397)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.fallThrough(Expressions.kt:260)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.access$fallThrough(Expressions.kt:1)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:244)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:157)
at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
I checked the source code, and everything seems to be in place. Did I something wrong as per the instructions to install this library which is denoted as follows:
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
You need to apply the plugin inside the plugins block and in the same fashion you're applying the Kotlin JVM plugin.
They mention to use it like this on their official docs
plugins {
kotlin("multiplatform") version "1.6.21"
// other plugins
id("io.arrow-kt.analysis.kotlin") version "2.0.2"
}
buildscript {
repositories {
mavenCentral()
}
}

How to package a kotlin project with JavaFX in it?

my team got a kotlin project with JavaFX as frontEnd. The problem we have is using jlink to produce a package.
Here is my build.gradle for the project.
Could anyone help me take a look at how to change this?
ps: I have already tried with
command-line javac
jlink from the intellij
create a new Main method and use the shadow plugin for jlink.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
}
}
plugins {
id "java"
id "application"
id "org.jetbrains.kotlin.jvm" version "1.6.10"
id "org.openjfx.javafxplugin" version "0.0.12"
id "org.beryx.jlink" version "2.25.0"
// Json
id "org.jetbrains.kotlin.plugin.serialization" version "1.6.10"
}
group "com.yyil"
version "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
ext {
junitVersion = "5.8.1"
}
sourceCompatibility = "16"
targetCompatibility = "16"
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
application {
mainClass = 'com.yyil.noteapp.NewMain'
}
[compileKotlin, compileTestKotlin].forEach {
it.kotlinOptions {
jvmTarget = "16"
}
}
javafx {
version = "17.0.2"
modules = ["javafx.controls", "javafx.fxml", "javafx.web", "javafx.swing", "javafx.graphics", "javafx.base"]
}
dependencies {
implementation("org.controlsfx:controlsfx:11.1.0")
implementation("net.synedra:validatorfx:0.1.13") {
exclude(group: "org.openjfx")
}
implementation("org.kordamp.ikonli:ikonli-javafx:12.2.0")
implementation("eu.hansolo:tilesfx:11.48") {
exclude(group: "org.openjfx")
}
// JSON
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
// testing JavaFX
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testImplementation("org.testfx:testfx-core:4.0.16-alpha")
testImplementation("org.testfx:testfx-junit5:4.0.16-alpha")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
// database (sqlite)
implementation "org.xerial:sqlite-jdbc:3.30.1"
// svg support
implementation("org.apache.xmlgraphics:batik-transcoder:1.14")
}
test {
useJUnitPlatform()
}
When I try to build it, I got errors like:

Linking dependencies with Gradle 6.5 in a Kotlin Multiplatform Multimodule project

I have the following structure and i want to include integration-jvm-1 as a dependency inside module-mp-1 and inside application-jvm-1, i want to include module-mp-1's JVM output as dependency.
See the sample project on Github:
https://github.com/JVAAS/kotlin-multiplatform-multi-module-setup
Here's the overview of what i've done so far:
generic-project
+-- applications
+-- application-jvm-1
+-- integrations
+-- integration-jvm-1
+-- modules
+-- module-mp-1
build.gradle.kts
settings.gradle.kts
my build.gradle.kts is empty, settings.gradle.kts is as follow:
rootProject.name = "generic-project"
include("applications:application-jvm-1")
include("modules:module-mp-1")
include("integrations:integration-jvm-1")
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
maven {
url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
}
}
}
applications/application-jvm-1/build.gradle.kts is as follow
(note the api(project(":modules:module-mp-1")) dependency)
plugins {
val kotlinVersion = "1.4-M2"
application
kotlin("multiplatform") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
}
group = "com.generic.applications"
version = "1.0.0"
repositories {
jcenter()
mavenCentral()
maven {
url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
}
maven {
url = uri("https://kotlin.bintray.com/kotlinx")
}
gradlePluginPortal()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
}
}
sourceSets {
val serializationVersion = "0.20.0-1.4-M2"
val coroutinesVersion = "1.3.7-1.4-M2"
val jvmMain by getting {
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
api(project(":modules:module-mp-1"))
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit"))
}
}
all {
languageSettings.enableLanguageFeature("InlineClasses")
}
}
}
application {
mainClassName = "Application"
}
integrations/integration-jvm-1/build.gradle.kts is as follow:
plugins {
val kotlinVersion = "1.4-M2"
kotlin("multiplatform") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
}
group = "com.generic.integrations"
version = "1.0.0"
repositories {
jcenter()
mavenCentral()
maven {
url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
}
maven {
url = uri("https://kotlin.bintray.com/kotlinx")
}
gradlePluginPortal()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
}
}
sourceSets {
val serializationVersion = "0.20.0-1.4-M2"
val coroutinesVersion = "1.3.7-1.4-M2"
val jvmMain by getting {
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
// logging
val slf4jVersion = "1.7.25"
val logbackVersion = "1.2.3"
implementation("org.slf4j:slf4j-api:$slf4jVersion")
implementation("org.slf4j:jcl-over-slf4j:$slf4jVersion")
implementation("org.slf4j:jul-to-slf4j:$slf4jVersion")
implementation("org.slf4j:log4j-over-slf4j:$slf4jVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit"))
}
}
all {
languageSettings.enableLanguageFeature("InlineClasses")
}
}
}
/modules/module-mp-1/build.gradle.kts is as follow:
(note the dependency api(project(":integrations:integration-jvm-1")))
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.4-M2"
}
group = "com.generic.modules"
version = "1.0.0"
repositories {
mavenCentral()
jcenter()
maven {
url = uri("https://dl.bintray.com/kotlin/kotlin-eap")
}
}
dependencies {
testImplementation(kotlin("test-junit5"))
implementation(kotlin("stdlib-jdk8"))
// ktor
val ktorVersion = "1.3.1"
//implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.ktor:ktor-server-cio:$ktorVersion")
implementation("io.ktor:ktor-html-builder:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.1")
// logging
val slf4jVersion = "1.7.25"
val logbackVersion = "1.2.3"
implementation("org.slf4j:slf4j-api:$slf4jVersion")
implementation("org.slf4j:jcl-over-slf4j:$slf4jVersion")
implementation("org.slf4j:jul-to-slf4j:$slf4jVersion")
implementation("org.slf4j:log4j-over-slf4j:$slf4jVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
api(project(":integrations:integration-jvm-1"))
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
When i pull Generic Project into IntelliJ and do a gradle refresh, i'm not getting error, but i'm also unable to get it to compile when i use module dependencies inside application
import com.generic.modules.Module1 <<-- unresolved reference modules
object Application {
#JvmStatic
fun main(args: Array<String>) {
println("TEST")
println(Module1().toString())
}
}
Any ideas?
I've made a sample project on Github with all of the above
https://github.com/JVAAS/kotlin-multiplatform-multi-module-setup
If this directory structure is making it more difficult than it should be, then i'm happy to change that too. Just want dependencies working one way or another.
Building Multiplatform Projects with Gradle is the best resource on this topic. Specifically in Adding Dependencies it says:
a project('...') dependency on another multiplatform project is resolved to an appropriate target automatically. It is enough to specify a single project('...') dependency in a source set's dependencies, and the compilations that include the source set will receive a corresponding platform-specific artifact of that project, given that it has a compatible target.
The article also contains examples on how to configure project dependencies.