@@ -27,6 +27,13 @@ import kotlin.reflect.*
2727import kotlin.reflect.full.*
2828
2929object Java9Modularity {
30+ private val KotlinProjectExtension .targets: Iterable <KotlinTarget >
31+ get() = when (this ) {
32+ is KotlinSingleTargetExtension <* > -> listOf (this .target)
33+ is KotlinMultiplatformExtension -> targets
34+ else -> error(" Unexpected 'kotlin' extension $this " )
35+ }
36+
3037
3138 @JvmStatic
3239 @JvmOverloads
@@ -49,16 +56,19 @@ object Java9Modularity {
4956 }
5057
5158 target.compilations.forEach { compilation ->
52- val compileKotlinTask = compilation.compileKotlinTask as KotlinCompile
59+ @Suppress(" UNCHECKED_CAST" )
60+ val compileKotlinTask = compilation.compileTaskProvider as TaskProvider <KotlinCompile >
5361 val defaultSourceSet = compilation.defaultSourceSet
5462
5563 // derive the names of the source set and compile module task
5664 val sourceSetName = defaultSourceSet.name + " Module"
5765
5866 kotlin.sourceSets.create(sourceSetName) {
5967 val sourceFile = this .kotlin.find { it.name == " module-info.java" }
60- val targetDirectory = compileKotlinTask.destinationDirectory.map {
61- it.dir(" ../${it.asFile.name} Module" )
68+ val targetDirectory = compileKotlinTask.flatMap { task ->
69+ task.destinationDirectory.map {
70+ it.dir(" ../${it.asFile.name} Module" )
71+ }
6272 }
6373
6474 // only configure the compilation if necessary
@@ -109,37 +119,38 @@ object Java9Modularity {
109119 * but it currently won't compile to a module-info.class file.
110120 */
111121 private fun Project.registerVerifyModuleTask (
112- compileTask : KotlinCompile ,
122+ compileTask : TaskProvider < KotlinCompile > ,
113123 sourceFile : File
114124 ): TaskProvider <out KotlinJvmCompile > {
115125 apply<KotlinApiPlugin >()
126+ @Suppress(" DEPRECATION" )
116127 val verifyModuleTaskName = " verify${compileTask.name.removePrefix(" compile" ).capitalize()} Module"
117128 // work-around for https://youtrack.jetbrains.com/issue/KT-60542
118129 val kotlinApiPlugin = plugins.getPlugin(KotlinApiPlugin ::class )
119130 val verifyModuleTask = kotlinApiPlugin.registerKotlinJvmCompileTask(
120131 verifyModuleTaskName,
121- compileTask.compilerOptions.moduleName.get()
132+ compilerOptions = compileTask.get().compilerOptions,
133+ explicitApiMode = provider { ExplicitApiMode .Disabled }
122134 )
123135 verifyModuleTask {
124136 group = VERIFICATION_GROUP
125137 description = " Verify Kotlin sources for JPMS problems"
126- libraries.from(compileTask.libraries)
127- source(compileTask.sources)
128- source(compileTask.javaSources)
138+ libraries.from(compileTask.map { it. libraries } )
139+ source(compileTask.map { it. sources } )
140+ source(compileTask.map { it. javaSources } )
129141 // part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
130- @Suppress(" INVISIBLE_MEMBER" )
131- source(compileTask.scriptSources)
142+ source(compileTask.map {
143+ @Suppress(" INVISIBLE_MEMBER" )
144+ it.scriptSources
145+ })
132146 source(sourceFile)
133147 destinationDirectory.set(temporaryDir)
134- multiPlatformEnabled.set(compileTask.multiPlatformEnabled)
148+ multiPlatformEnabled.set(compileTask.get(). multiPlatformEnabled)
135149 compilerOptions {
136150 jvmTarget.set(JvmTarget .JVM_9 )
137- // To support LV override when set in aggregate builds
138- languageVersion.set(compileTask.compilerOptions.languageVersion)
139151 freeCompilerArgs.addAll(
140152 listOf (" -Xjdk-release=9" , " -Xsuppress-version-warnings" , " -Xexpect-actual-classes" )
141153 )
142- optIn.addAll(compileTask.kotlinOptions.options.optIn)
143154 }
144155 // work-around for https://youtrack.jetbrains.com/issue/KT-60583
145156 inputs.files(
@@ -160,18 +171,21 @@ object Java9Modularity {
160171 .declaredMemberProperties
161172 .find { it.name == " ownModuleName" }
162173 ?.get(this ) as ? Property <String >
163- ownModuleNameProp?.set(compileTask.kotlinOptions. moduleName)
174+ ownModuleNameProp?.set(compileTask.flatMap { it.compilerOptions. moduleName} )
164175 }
165176
166177 val taskKotlinLanguageVersion = compilerOptions.languageVersion.orElse(KotlinVersion .DEFAULT )
167178 @OptIn(InternalKotlinGradlePluginApi ::class )
168179 if (taskKotlinLanguageVersion.get() < KotlinVersion .KOTLIN_2_0 ) {
169180 // part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
170181 @Suppress(" INVISIBLE_MEMBER" )
171- commonSourceSet.from(compileTask.commonSourceSet)
182+ commonSourceSet.from(compileTask.map {
183+ @Suppress(" INVISIBLE_MEMBER" )
184+ it.commonSourceSet
185+ })
172186 } else {
173- multiplatformStructure.refinesEdges.set(compileTask.multiplatformStructure.refinesEdges)
174- multiplatformStructure.fragments.set(compileTask.multiplatformStructure.fragments)
187+ multiplatformStructure.refinesEdges.set(compileTask.flatMap { it. multiplatformStructure.refinesEdges } )
188+ multiplatformStructure.fragments.set(compileTask.flatMap { it. multiplatformStructure.fragments } )
175189 }
176190 // part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
177191 // and work-around for https://youtrack.jetbrains.com/issue/KT-60582
@@ -181,7 +195,7 @@ object Java9Modularity {
181195 }
182196
183197 private fun Project.registerCompileModuleTask (
184- compileTask : KotlinCompile ,
198+ compileTask : TaskProvider < KotlinCompile > ,
185199 sourceFile : File ,
186200 targetDirectory : Provider <out Directory >
187201 ) = tasks.register(" ${compileTask.name} Module" , JavaCompile ::class ) {
@@ -201,10 +215,12 @@ object Java9Modularity {
201215
202216 options.compilerArgumentProviders.add(object : CommandLineArgumentProvider {
203217 @get:CompileClasspath
204- val compileClasspath = compileTask.libraries
218+ val compileClasspath = objects.fileCollection().from(
219+ compileTask.map { it.libraries }
220+ )
205221
206222 @get:CompileClasspath
207- val compiledClasses = compileTask.destinationDirectory
223+ val compiledClasses = compileTask.flatMap { it. destinationDirectory }
208224
209225 @get:Input
210226 val moduleName = sourceFile
0 commit comments