From 6e6ec966639cc070853421dfa2acd2a17648db82 Mon Sep 17 00:00:00 2001 From: Robinson Date: Sun, 25 Apr 2021 16:14:04 +0200 Subject: [PATCH] Fixed kotlin module errors (the name must be the same throughout the project, otherwise it won't load correctly) --- src/dorkbox/gradle/StaticMethodsAndTools.kt | 3 +- src/dorkbox/gradle/jpms/JavaXConfiguration.kt | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/dorkbox/gradle/StaticMethodsAndTools.kt b/src/dorkbox/gradle/StaticMethodsAndTools.kt index 0c53d6c..5fac68f 100644 --- a/src/dorkbox/gradle/StaticMethodsAndTools.kt +++ b/src/dorkbox/gradle/StaticMethodsAndTools.kt @@ -716,7 +716,8 @@ open class StaticMethodsAndTools(private val project: Project) { println("\tAllowing kotlin internal access for $moduleName") project.tasks.withType(KotlinCompile::class.java).forEach { - it.kotlinOptions.moduleName = moduleName // must be the same module name for everything! + // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime + it.kotlinOptions.moduleName = moduleName } accessGroup.forEach { diff --git a/src/dorkbox/gradle/jpms/JavaXConfiguration.kt b/src/dorkbox/gradle/jpms/JavaXConfiguration.kt index 27a181d..d389cd7 100644 --- a/src/dorkbox/gradle/jpms/JavaXConfiguration.kt +++ b/src/dorkbox/gradle/jpms/JavaXConfiguration.kt @@ -49,7 +49,7 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) val nameX = "_$ver" // If the kotlin plugin is applied, and there is a compileKotlin task.. Then kotlin is enabled - val hasKotlin = StaticMethodsAndTools.hasKotlin(project) + val hasKotlin: Boolean val moduleFile = project.projectDir.walkTopDown().find { it.name == "module-info.java" } var moduleName: String @@ -95,6 +95,8 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) throw GradleException("The module name must be specified in the module-info file! Verify file: $moduleFile") } + hasKotlin = StaticMethodsAndTools.hasKotlin(project) + val info = when { hasKotlin -> "Initializing JPMS $ver, Java/Kotlin [$moduleName]" else -> "Initializing JPMS $ver, Java [$moduleName]" @@ -208,7 +210,8 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) sourceCompatibility = ver targetCompatibility = ver kotlinOptions.jvmTarget = ver - kotlinOptions.moduleName = compileMainKotlin.kotlinOptions.moduleName // must be the same module name + // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime + kotlinOptions.moduleName = project.name } compileTestXKotlin.apply { @@ -216,7 +219,8 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) sourceCompatibility = ver targetCompatibility = ver kotlinOptions.jvmTarget = ver - kotlinOptions.moduleName = compileTestKotlin.kotlinOptions.moduleName // must be the same module name + // must be the same module name as the regular one (which is the project name). If it is a different name, it crashes at runtime + kotlinOptions.moduleName = project.name } } @@ -234,22 +238,9 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) mainX.allSource.srcDirs ) - val allCompiled = if (hasKotlin) { - proj.files( - compileMainJava.destinationDir, - compileMainKotlin.destinationDir - ) - } else { - proj.files( - compileMainJava.destinationDir - ) - } - - source = allSource.asFileTree // the files live in this location include("**/module-info.java") - sourceCompatibility = ver targetCompatibility = ver @@ -261,6 +252,12 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) // modules require this! doFirst { + val allCompiled = if (hasKotlin) { + proj.files(compileMainJava.destinationDir, compileMainKotlin.destinationDir) + } else { + proj.files(compileMainJava.destinationDir) + } + // the SOURCE of the module-info.java file. It uses **EVERYTHING** options.sourcepath = allSource options.compilerArgs.addAll(listOf( @@ -315,6 +312,10 @@ class JavaXConfiguration(javaVersion: JavaVersion, private val project: Project) val length = details.path.length + 1 val sourceDir = absolutePath.substring(0, absolutePath.length - length) + +// println("checking file: $absolutePath") +// println("checking file: $sourceDir") + if (sourcePaths.contains(sourceDir)) { // println("Moving: " + absolutePath) // println(" : " + details.path)