-
Notifications
You must be signed in to change notification settings - Fork 432
buildSrc/build.gradle.kts cannot resolve references defined in buildSrc/src #1320
Description
Expected Behavior
The user should be able to reference objects and functions defined in his buildSrc/src/main/kotlin directory.
Current Behavior
I have a working kotlin dsl gradle build: kotlin-dsl-gradle-experiment
I've written a function in my buildSrc/src/main/kotlin/test.kt file as follows:
package org.ninrod.backend.build
import java.io.File
fun doWeHaveToUseArtifactory(): Boolean {
val centos = File("/etc/centos-release").exists()
val workbench = File("/etc/hostname").readText().trim() == "workbench"
return centos && workbench
}
I'd love to use that function inside my buildSrc/build.gradle.kts instead of duplicating myself in every kotlin dsl block:
buildscript {
val artifactory = "http://artifactory/artifactory/gradle"
fun doWeHaveToUseArtifactory(): Boolean {
val centos = File("/etc/centos-release").exists()
val workbench = File("/etc/hostname").readText().trim() == "workbench"
return centos && workbench
}
repositories {
if (doWeHaveToUseArtifactory()) {
println("configuring artifactory for plugin repos")
maven {
url = uri(artifactory)
}
} else {
println("we are using mavencentral for plugins")
maven {
mavenCentral()
jcenter()
}
}
}
dependencies {
if (doWeHaveToUseArtifactory()) {
println("we are going to add the classpath of the org.jfrog.buildinfo plugin")
classpath("org.jfrog.buildinfo:build-info-extractor-gradle:4.9.0")
}
classpath("gradle.plugin.org.gradle.kotlin:gradle-kotlin-dsl-plugins:1.1.1")
}
}
apply(plugin = "org.gradle.kotlin.kotlin-dsl")
allprojects {
repositories {
val artifactory = "http://artifactory/artifactory/gradle"
fun doWeHaveToUseArtifactory(): Boolean {
val centos = File("/etc/centos-release").exists()
val workbench = File("/etc/hostname").readText().trim() == "workbench"
return centos && workbench
}
if (doWeHaveToUseArtifactory()) {
maven {
url = uri(artifactory)
}
} else {
mavenCentral()
jcenter()
}
}
}
Context
this affects all kotlin-dsl users that have to configure interesting/complex behaviour and have to resort to logic which should be reusable.
These actions does not work:
- importing the function
- referencing by full qualified name
Steps to Reproduce (for bugs)
simply clone the kotlin-dsl-gradle-experiment and verify the behaviour:
$ git clone https://github.com/ninrod/kotlin-dsl-gradle-experiment.git
$ cd kotlin-dsl-gradle-experiment
$ gradle build
# verify that the build works
# now try to change the buildSrc/build.gradle.kts to reuse the function define in buildSrc/src/main/kotlin/test.kt
$ gradle build
# verify that nothing works
Your Environment
Gradle 5.1.1
Build time: 2019-01-10 23:05:02 UTC
Revision: 3c9abb645fb83932c44e8610642393ad62116807
Kotlin DSL: 1.1.1
Kotlin: 1.3.11
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_192 (Oracle Corporation 25.192-b12)
OS: Linux 4.9.87-linuxkit-aufs amd64