java9-modularity / gradle-modules-plugin

This Gradle plugin helps working with the Java Platform Module System
https://javamodularity.com/
MIT License
233 stars 36 forks source link

Unresolved reference: modularity #216

Open anantharaman93 opened 2 years ago

anantharaman93 commented 2 years ago

When I try to use modularity.mixedJavaRelease under subprojects in a kotlin based gradle build file, I am getting Unresolved reference: modularity error.

TWiStErRob commented 1 year ago

Kotlin DSL is really tricky with subprojects especially that this plugin lazily registers the extension based on the existence of a file in a subproject. Root project doesn't have a module-info.java usually and therefore it never gets a modularity extension, so Gradle doesn't know that it needs to generate an accessor for it. The plugin is not recommended to be applied to the root project anyway (... apply false). If you can't migrate to precompiled convention plugins, you can try writing your own Kotlin DSL accessor for modularity:

val org.gradle.api.Project.`modularity`: org.javamodularity.moduleplugin.extensions.ModularityExtension get() =
    (this as org.gradle.api.plugins.ExtensionAware).extensions.getByName("modularity") as org.javamodularity.moduleplugin.extensions.ModularityExtension

fun org.gradle.api.Project.`modularity`(configure: Action<org.javamodularity.moduleplugin.extensions.ModularityExtension>): Unit =
    (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("modularity", configure)

Drop these at the end of your build.gradle.kts and you should be good to go.

big-andy-coates commented 7 months ago

@anantharaman93 did this resolve your issue?