Squeazer
Squeazer

Reputation: 1254

Gradle Kotlin DSL multi project build with Java Modules

I'm creating a new project (using IntelliJ IDEA) that will be using:

I'm having problems setting up Gradle to properly build my project. Most examples I've found are for Groovy and not Kotlin DSL, and most only cover some of the features I want, but not all.

Right now I have two modules, core and lib, where the core module requires the lib module. My gradle build scripts are:

build.gradle.kts

plugins {
    base
    kotlin("jvm") version "1.3.41" apply false
}

subprojects {
    afterEvaluate {
        tasks.withType<JavaCompile> {
            inputs.property("moduleName", extra["moduleName"])
            options.compilerArgs.addAll(arrayOf("--module-path", classpath.asPath))
            classpath = files()
        }
    }

    repositories {
        mavenCentral()
        jcenter()
    }
}

core/build.gradle.kts

extra.set("moduleName", "myproject.core")

plugins {
    kotlin("jvm")
}

dependencies {
    compile(kotlin("stdlib"))
    compile(project(":networking"))
}

lib/build.gradle.kts

extra.set("moduleName", "myproject.lib")

plugins {
    kotlin("jvm")
}

dependencies {
    compile(kotlin("stdlib"))
}

Doing this, configuration fails with:

A problem occurred configuring project ':core'.

Cannot get property 'moduleName' on extra properties extension as it does not exist

If I remove the inputs.property() line the configuration succeeds, but the core compilation fails (lib compiles successfully) with :

Task :core:compileKotlin

e: Module myproject.lib cannot be found in the module graph

I assume the issue is is my root build.gradle.kts, but I cannot figure out how to make it work. Googling around, Kotlin DSL for Gradle is somewhat new and not as widely used, and documentation is pretty scarce.

Any advice would be appreciated!

Upvotes: 3

Views: 4120

Answers (1)

Squeazer
Squeazer

Reputation: 1254

Naturally after posting the question I found the solution. There exists a Gradle plugin that does exactly what's needed in this situation, with a KotlinDSL example: https://github.com/java9-modularity/gradle-modules-plugin/tree/master/test-project-kotlin

Using the plugin, all I needed to do is change the root build.gradle.kts file:

plugins {
    base
    kotlin("jvm") version "1.3.41" apply false
    id("org.javamodularity.moduleplugin") version "1.5.0" apply false
}

subprojects {
    apply(plugin = "org.javamodularity.moduleplugin")

    repositories {
        mavenCentral()
        jcenter()
    }
}

Note: Make sure that your module-info.java file is in the java src folder, and not in the kotlin src folder, otherwise the plugin will not detect the module.

Upvotes: 2

Related Questions