From dfb9147fc91d6a8b565a4f4075b6624b9870b315 Mon Sep 17 00:00:00 2001 From: Johnathan Gilday Date: Fri, 20 Dec 2019 09:50:52 -0500 Subject: [PATCH] :construction: Gradle 6 - Task Configuration Avoidance --- .../configuration-avoidance/build.gradle.kts | 37 +++++++++++++++++++ gradle-6/gradle.properties | 1 + gradle-6/settings.gradle.kts | 1 + 3 files changed, 39 insertions(+) create mode 100644 gradle-6/configuration-avoidance/build.gradle.kts diff --git a/gradle-6/configuration-avoidance/build.gradle.kts b/gradle-6/configuration-avoidance/build.gradle.kts new file mode 100644 index 0000000000..e84f08bf93 --- /dev/null +++ b/gradle-6/configuration-avoidance/build.gradle.kts @@ -0,0 +1,37 @@ +plugins { + base +} + +description = """ + Demonstrates Gradle Configuraiton Avoidance API. Creates a new configuration "extralibs" to + which we add dependencies. The custom task "copyExtraLibs" copies those dependencies to a new + build directory "extra-libs". This build uses the Task Configuraion Avoidance APIs which have + been marked stable in Gradle 6.0 +""".trimIndent() + +// extraLibs is a NamedDomainObjectProvider - the Configuration object will not be +// realized until it is needed. In the meantime, the build may reference it by name +val extralibs by configurations.registering + +dependencies { + // we can call extralibs.name without causing the extralibs to be realized + add(extralibs.name, "junit:junit:4.12") +} + +// extraLibsDir is a Provider - the Directory object will not be realized until it is +// needed +val extraLibsDir = project.layout.buildDirectory.dir("extra-libs") + +// copyExtraLibs is a TaskProvider - the task will not be realized until it is needed +val copyExtraLibs by tasks.registering(Copy::class) { + // the copy task's "from" and "into" APIs accept Provider types to support configuration + // avoidance + from(extralibs) + into(extraLibsDir) +} + +// configures the "build" task only if it needs to be +tasks.build { + // dependsOn accepts a TaskProvider to avoid realizing the copyExtraLibs needlessly + dependsOn(copyExtraLibs) +} diff --git a/gradle-6/gradle.properties b/gradle-6/gradle.properties index f97ebb7d33..7da8b3d72a 100644 --- a/gradle-6/gradle.properties +++ b/gradle-6/gradle.properties @@ -1 +1,2 @@ org.gradle.parallel=true +org.gradle.configureondemand=true diff --git a/gradle-6/settings.gradle.kts b/gradle-6/settings.gradle.kts index babe431175..88cf74c4d7 100644 --- a/gradle-6/settings.gradle.kts +++ b/gradle-6/settings.gradle.kts @@ -1,5 +1,6 @@ rootProject.name = "gradle-6" +include("configuration-avoidance") include("dependency-constraints") include("fibonacci-spi") include("fibonacci-recursive")