|
| 1 | +import org.gradle.api.* |
| 2 | +import org.gradle.api.file.* |
| 3 | +import org.gradle.api.tasks.bundling.* |
| 4 | +import org.gradle.api.tasks.compile.* |
| 5 | +import org.gradle.kotlin.dsl.* |
| 6 | +import org.gradle.util.GUtil.* |
| 7 | +import org.jetbrains.kotlin.gradle.dsl.* |
| 8 | +import org.jetbrains.kotlin.gradle.plugin.* |
| 9 | +import org.jetbrains.kotlin.gradle.plugin.mpp.* |
| 10 | +import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.* |
| 11 | +import org.jetbrains.kotlin.gradle.targets.jvm.* |
| 12 | +import java.io.* |
| 13 | +import java.lang.module.* |
| 14 | +import java.util.spi.* |
| 15 | + |
| 16 | +object Java9Modularity { |
| 17 | + |
| 18 | + @JvmStatic |
| 19 | + @JvmOverloads |
| 20 | + fun Project.configureJava9ModuleInfo(multiRelease: Boolean = true) { |
| 21 | + val kotlin = extensions.findByType<KotlinProjectExtension>() ?: return |
| 22 | + val jvmTargets = kotlin.targets.filter { it is KotlinJvmTarget || it is KotlinWithJavaTarget<*> } |
| 23 | + if (jvmTargets.isEmpty()) { |
| 24 | + logger.warn("No Kotlin JVM targets found, can't configure compilation of module-info!") |
| 25 | + } |
| 26 | + jvmTargets.forEach { target -> |
| 27 | + target.compilations.forEach { compilation -> |
| 28 | + val defaultSourceSet = compilation.defaultSourceSet.kotlin |
| 29 | + val moduleInfoSourceFile = defaultSourceSet.find { it.name == "module-info.java" } |
| 30 | + |
| 31 | + if (moduleInfoSourceFile == null) { |
| 32 | + logger.info("No module-info.java file found in ${defaultSourceSet.srcDirs}, can't configure compilation of module-info!") |
| 33 | + } else { |
| 34 | + val targetName = toCamelCase(target.targetName) |
| 35 | + val compilationName = if (compilation.name != KotlinCompilation.MAIN_COMPILATION_NAME) toCamelCase(compilation.name) else "" |
| 36 | + val compileModuleInfoTaskName = "compile${compilationName}ModuleInfo$targetName" |
| 37 | + val checkModuleInfoTaskName = "check${compilationName}ModuleInfo$targetName" |
| 38 | + |
| 39 | + val compileKotlinTask = compilation.compileKotlinTask as AbstractCompile |
| 40 | + val modulePath = compileKotlinTask.classpath |
| 41 | + val moduleInfoClassFile = compileKotlinTask.destinationDirectory.file("module-info.class").get().asFile |
| 42 | + |
| 43 | + val compileModuleInfoTask = registerCompileModuleInfoTask(compileModuleInfoTaskName, modulePath, compileKotlinTask.destinationDirectory, moduleInfoSourceFile) |
| 44 | + tasks.getByName(compilation.compileAllTaskName).dependsOn(compileModuleInfoTask) |
| 45 | + |
| 46 | + val checkModuleInfoTask = registerCheckModuleInfoTask(checkModuleInfoTaskName, modulePath, moduleInfoClassFile) |
| 47 | + checkModuleInfoTask.configure { dependsOn(compilation.compileAllTaskName) } |
| 48 | + tasks.getByName("check").dependsOn(checkModuleInfoTask) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (multiRelease) { |
| 53 | + tasks.getByName<Jar>(target.artifactsTaskName) { |
| 54 | + rename("module-info.class", "META-INF/versions/9/module-info.class") |
| 55 | + manifest { |
| 56 | + attributes("Multi-Release" to true) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun Project.registerCompileModuleInfoTask(taskName: String, modulePath: FileCollection, destinationDir: DirectoryProperty, moduleInfoSourceFile: File) = |
| 64 | + tasks.register(taskName, JavaCompile::class) { |
| 65 | + dependsOn(modulePath) |
| 66 | + source(moduleInfoSourceFile) |
| 67 | + classpath = files() |
| 68 | + destinationDirectory.set(destinationDir) |
| 69 | + sourceCompatibility = JavaVersion.VERSION_1_9.toString() |
| 70 | + targetCompatibility = JavaVersion.VERSION_1_9.toString() |
| 71 | + doFirst { |
| 72 | + options.compilerArgs = listOf( |
| 73 | + "--release", "9", |
| 74 | + "--module-path", modulePath.asPath, |
| 75 | + "-Xlint:-requires-transitive-automatic" |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private fun Project.registerCheckModuleInfoTask(taskName: String, modulePath: FileCollection, moduleInfoClassFile: File) = |
| 81 | + tasks.register(taskName) { |
| 82 | + dependsOn(modulePath) |
| 83 | + doLast { |
| 84 | + val jdeps = ToolProvider.findFirst("jdeps").orElseThrow { IllegalStateException("Tool 'jdeps' is not available") } |
| 85 | + val moduleDescriptor = moduleInfoClassFile.inputStream().use { ModuleDescriptor.read(it) } |
| 86 | + val moduleName = moduleDescriptor.name() |
| 87 | + val expectedOutput = moduleDescriptor.toJdepsOutput(moduleInfoClassFile) |
| 88 | + |
| 89 | + val outputCaptureStream = ByteArrayOutputStream() |
| 90 | + val printStream = PrintStream(outputCaptureStream, true, Charsets.UTF_8) |
| 91 | + jdeps.run( |
| 92 | + printStream, printStream, |
| 93 | + "--multi-release", "9", |
| 94 | + "--module-path", (modulePath + files(moduleInfoClassFile.parentFile)).asPath, |
| 95 | + "--check", moduleName |
| 96 | + ) |
| 97 | + val actualOutput = outputCaptureStream.toString(Charsets.UTF_8).trim() |
| 98 | + |
| 99 | + if (actualOutput != expectedOutput) { |
| 100 | + throw IllegalStateException("Module-info requirements section does not match!\n$actualOutput") |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private fun ModuleDescriptor.toJdepsOutput(file: File, separator: String = System.lineSeparator()) = |
| 106 | + "${name()} (${file.parentFile.toURI().toString().replace("file:", "file://")})$separator [Module descriptor]$separator" + |
| 107 | + requires().sortedBy { it.name() }.joinToString(separator) { requirement -> " requires $requirement;" } |
| 108 | +} |
0 commit comments