I have a project that mixes Java and Kotlin.
The Java code in src/main/java
calls Kotlin code in src/main/kotlin
.
The app is launched from the Kotlin part.
In the build.gradle.kts
, the full content
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
id("project-report")
id("org.springframework.boot") version "3.2.4"
id("io.spring.dependency-management") version "1.1.4"
kotlin("jvm") version "1.9.23"
kotlin("plugin.spring") version "1.9.23"
}
group = "com.aaaaa"
version = "x.y.z-SNAPSHOT"
sourceSets {
// /questions/38131237/mixing-java-and-kotlin-in-gradle-project-kotlin-cannot-find-java-class
main {
java.srcDirs("src/main/java","src/main/kotlin")
kotlin.srcDir("src/main/java")
// resources.srcDir("src/main/resources")
}
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jsoup:jsoup:1.17.2")
implementation("io.github.jbock-java:either:1.5.2")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.htmlunit:htmlunit:4.2.0")
developmentOnly("org.springframework.boot:spring-boot-devtools")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation ("org.junit.jupiter:junit-jupiter-api:5.8.1")
// testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
The Java code cannot find Kotlin code. And I get the error during the build ./gradlew build
Execution failed for task ‘:compileJava’.
Compilation failed;
Strange thing, everything was working without explicitly sourceSets
in Gradle before I updated to IntelliJ 2024
What is wrong in the sourceSets
configuration ? In order for the Java code to find Kotlin code. The app is launched from Kotlin part.