@ -14,6 +14,8 @@ import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.registering
import org.gradle.kotlin.dsl.withType
import org.gradle.process.JavaForkOptions
import java.io.File
import java.nio.file.Files
import java.util.regex.Pattern
class ExecParameters (
@ -35,6 +37,9 @@ open class ModuleInfoExtension {
lateinit var moduleName : String
val execParameters = ExecParameters ( )
internal val stubbedModules = mutableListOf < String > ( )
fun stubModule ( moduleName : String ) = stubbedModules . add ( moduleName )
fun modularExec ( action : ExecParameters . ( ) -> Unit ) = execParameters . action ( )
}
@ -47,7 +52,8 @@ class ModuleInfoCompilePlugin : Plugin<Project> {
override fun apply ( target : Project ) = target . run {
val infoExtension = target . extensions . create ( " moduleInfo " , ModuleInfoExtension :: class . java )
if ( ! JavaVersion . current ( ) . isJava9Compatible
|| project . findProperty ( " skipModuleInfo " ) in listOf ( " " , " true " ) ) return @run
|| project . findProperty ( " skipModuleInfo " ) in listOf ( " " , " true " )
) return @run
val moduleInfoFile = file ( " src/main/module/module-info.java " )
if ( moduleInfoFile . exists ( ) ) {
@ -73,16 +79,43 @@ class ModuleInfoCompilePlugin : Plugin<Project> {
}
private fun Project . setupModuleInfoCompilation ( infoExtension : ModuleInfoExtension ) {
val stubOutputDir = buildDir . resolve ( " generated/moduleInfoStubs " )
fun String . stubModuleInfoPath ( ) = stubOutputDir . resolve ( " $this /module-info.java " )
val createModuleStubs by tasks . registering ( JavaCompile :: class ) {
stubOutputDir . deleteRecursively ( )
Files . createDirectories ( stubOutputDir . toPath ( ) )
infoExtension . stubbedModules . forEach {
source ( it . stubModuleInfoPath ( ) . also { file ->
file . parentFile . mkdirs ( )
file . writeText ( " module $it {} " )
} )
}
classpath = files ( )
options . compilerArgs . addAll ( listOf (
" --release " , infoExtension . version . majorVersion ,
" --module-source-path " , stubOutputDir . absolutePath
) )
destinationDirectory . set ( buildDir . resolve ( " classes/moduleStubs " ) )
}
val compileJava = tasks . named < JavaCompile > ( " compileJava " )
val compileModuleInfoJava by tasks . registering ( JavaCompile :: class ) {
dependsOn ( createModuleStubs )
val javaCompile = compileJava . get ( )
classpath = files ( )
source ( " src/main/module/module-info.java " )
source ( javaCompile . source )
destinationDirectory . set ( buildDir . resolve ( " classes/module " ) )
check ( infoExtension . version . isJava9Compatible )
options . compilerArgs . addAll ( listOf ( " --module-path " , javaCompile . classpath . asPath ) )
val separator = " ${File.pathSeparatorChar} "
val modulePath = javaCompile . classpath . asPath + separator + infoExtension . stubbedModules
. joinToString ( separator ) { " ${stubOutputDir.absolutePath} / $it / $it .jar " }
options . compilerArgs . addAll ( listOf ( " --module-path " , modulePath ) )
if ( infoExtension . extraArgs . isNotEmpty ( ) ) {
options . compilerArgs . addAll ( infoExtension . extraArgs )
sourceCompatibility = infoExtension . version . majorVersion