gradle / kotlin-dsl-samples

Samples builds using the Gradle Kotlin DSL
https://gradle.org/kotlin/
Other
3.71k stars 434 forks source link

buildScript block does not respect top level imports in build.gradle.kts #1319

Closed ninrod closed 5 years ago

ninrod commented 5 years ago

Expected Behavior

the buildScript block should resolve a reference to an imported function in gradle 5.1.1

Current Behavior

the buildScript block is not resolving a reference to an imported function in gradle 5.1.1

Context

this is somewhat related to #939, but actually #939 misses the mark. Functions can indeed be used, but only if you specify their full qualified name. The problem is that the buildScript block does not respect the top level imports defined in the build.gradle.kts file.

note that in other blocks, like in the allprojects block, importing works fine.

Your Environment

...e-experiment ➜ gradle --version

-----------------------------------------------------------
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

...e-experiment ➜ 

Steps to Reproduce (for bugs)

I have setup a git project so that you can verify this behaviour: here. Reproducing the problem is very easy:

$ git clone https://github.com/ninrod/kotlin-dsl-gradle-experiment.git
$ cd kotlin-dsl-gradle-experiment
$ gradle build
# verify that the build works

The build works, but that's only because I'm using the full qualified name inside the buildScript block.

If we change org.ninrod.backend.build.doWeHaveToUseArtifactory() to simply doWeHaveToUseArtifactory() in the two locations in buildScript block, the build fails: note that there is an import (import org.ninrod.backend.build.*) at the top of the gradle file.

# change build.gradle.kts to not use the full qualified name
$ gradle build
# watch the build fail because doWeHaveToUseArtifactory is an unresolved reference

> Configure project :
e: /var/lib/docker/volumes/workbench_ext4/_data/kotlin-dsl-gradle-experiment/build.gradle.kts:9:13: Unresolved reference: doWeHaveToUseArtifactory                    

FAILURE: Build failed with an exception.

Here is the build.gradle.kts file, for easier referencing:

import org.gradle.jvm.tasks.Jar
import org.ninrod.backend.build.*

buildscript {
    val artifactory = "http://artifactory/artifactory/gradle"
    repositories {
        // BUG HERE: I have to use the full qualified name of the doWeHaveToUseArtifactory
        // because the buildScript block does not respect the top level defined imports!
        if (org.ninrod.backend.build.doWeHaveToUseArtifactory()) {
            println("configuring artifactory for plugin repos")
            maven {
                url = uri(artifactory)
            }
        } else {
            println("we are using mavencentral for plugins")
            maven {
                mavenCentral()
                jcenter()
            }
        }
    }
    dependencies {
        // BUG HERE: I have to use the full qualified name of the doWeHaveToUseArtifactory
        // because the buildScript block does not respect the top level defined imports!
        if (org.ninrod.backend.build.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("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11")
    }
}

apply(plugin = "kotlin")
apply(plugin = "com.jfrog.artifactory")
plugins {
    application
}

application {
    mainClassName = "org.ninrod.backend.EntrypointKt"
    version = "0.0.1"
}

configure<JavaPluginConvention> {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

repositories {
        val artifactory = "http://artifactory/artifactory/gradle"
        if (doWeHaveToUseArtifactory()) {
            maven {
                url = uri(artifactory)
            }
        } else {
            mavenCentral()
            jcenter()
        }
}

dependencies {
    // kotlin
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11")
    compile("org.jetbrains.kotlin:kotlin-reflect:1.3.11")

    // db
    compile("org.jetbrains.exposed:exposed:0.11.2")
    compile("org.jetbrains.exposed:spring-transaction:0.11.2")
    compile("org.postgresql:postgresql:42.2.5")

    // tests
    testCompile("org.junit.jupiter:junit-jupiter-api:5.3.2")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.2")
}

tasks {
    "jar"(Jar::class) {
        baseName = project.name
        manifest {
            attributes["Main-Class"] = application.mainClassName
        }
        from( configurations.runtime.get().map { if (it.isDirectory) it else zipTree(it) })
    }

    val dump by creating {
        println("CONFIGURATION PHASE!!!")
        println(hello())
        println("temos que usar artifactory? " + doWeHaveToUseArtifactory())
        doLast {
            println("EXECUTION PHASE!!!")
        }
    }
}
JLLeitschuh commented 5 years ago

Duplicate of #939

eskatos commented 5 years ago

Closing as duplicate of #939