Reputation: 1109
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 ?
Upvotes: 4
Views: 1334
Reputation: 380
Works for me
plugins {
java // just simply add java plugin to the root plugins section
idea
eclipse
}
subprojects {
apply(plugin = "java")
dependencies {
implementation("com.google.guava:guava:28.1-jre")
}
}
In this case java
plugin will be applied to the root project that turns to creation of build
folder in the root. To avoid build
folder creation you need to disable build
and jar
tasks for the root project:
tasks {
jar {
isEnabled = false
}
build {
isEnabled = false
}
}
See also gradle: disable creation of the build folder at root in multi-projects application
Upvotes: 1
Reputation: 76569
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.
Upvotes: 3