Search code examples
gradlegradle-kotlin-dsl

Gradle Kotlin DSL: how to configure subprojects using typesafe api


Gradle 6.1.1

I have been trying to convert my projects' Gradle files using Kotlin DSL in a type-safe manner but so far failed. All my projects are multi-project builds in Java. The idea is to factorize/deduplicate the common configurations of the submodules (whether in the 'subprojects' block of the root module or in a separate 'kts' file I don't care). The official documentation states that it is not possible to have type-safe with 'subprojects' and apply(from = 'shared.gradle.kts') (https://docs.gradle.org/current/userguide/kotlin_dsl.html).

It works as below but it is rather anoying:


plugins {
    idea
    eclipse
}

subprojects {
    apply(plugin = "java")

    dependencies {
       "implementation"("com.google.guava:guava:28.1-jre")
       //...
    }
}

Is there a way to factorise the module configurations for all the submodules in a type-safe manner ? If not... does gradle plan to allow it ?


Solution

  • Gradle 6.1.1 Type-safe model accessors reads:

    Only the main project build scripts and precompiled project script plugins have type-safe model accessors. Initialization scripts, settings scripts, script plugins do not. These limitations will be removed in a future Gradle release.

    Whatever "future release" might mean ...while Cross-configuring projects reads:

    Cross project configuration is a mechanism by which you can configure a project from another project’s build script. A common example is when you configure subprojects in the root project build script. Taking this approach means that you won’t be able to use type-safe accessors for model elements contributed by the plugins. You will instead have to rely on string literals and the standard Gradle APIs.