Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ext.* variables into plugins block in build.gradle.kts

Tags:

My build file looks like this:

val nexusBaseUri: String by extra
val gradle_version: String by extra
val kotlin_version: String by extra

buildscript {
    val nexusBaseUri by extra { "https://mynexusserver/nexus" }
    val gradle_version by extra { "4.1" }
    val kotlin_version by extra { "1.1.4-3" }
    val springBoot_version by extra { "2.0.0.M3" }

    repositories {
        maven { url = uri("$nexusBaseUri/repository/public") }
        jcenter()
        maven { url = uri("http://repo.spring.io/snapshot") }
        maven { url = uri("http://repo.spring.io/milestone") }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBoot_version")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
    }
}

plugins {
    application
    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    // the following line causes a problem
    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    kotlin("jvm", kotlin_version)
}

apply {
    plugin("kotlin-spring")
    plugin("org.springframework.boot")
    plugin("io.spring.dependency-management")
}

application {
    mainClassName = "eqip.fid.FdmlInterpreterDeveloperAppKt"
}

repositories {
    maven { url = uri("$nexusBaseUri/content/groups/public") }
    jcenter()
    maven { url = uri("http://repo.spring.io/snapshot") }
    maven { url = uri("http://repo.spring.io/milestone") }
}

dependencies {
    compile(kotlin("stdlib"))
    compile("org.springframework.boot:spring-boot-starter-web")
}

tasks {
    "wrapper"(Wrapper::class) {
        gradleVersion = gradle_version
    }
}

The error I get in IntelliJ IDEA is

'val kotlin_version: String' can't be called in this context by implicit receiver. Use the explicit one if necessary

How do I fix this?

like image 299
Andy Avatar asked Sep 05 '17 11:09

Andy


People also ask

What is Ext block in Gradle?

All enhanced objects in Gradle's domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object's ext property. Alternatively, an ext block can be used to add multiple properties at once.

How do I add plugins to build Gradle?

While creating a custom plugin, you need to write an implementation of plugin. Gradle instantiates the plugin and calls the plugin instance using Plugin. apply() method. The following example contains a greeting plugin, which adds a hello task to the project.

How do I set the Gradle plugin version?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line.


1 Answers

You can define a version inside plugins and then make this version accessible outside of the block, e.g. in the dependencies section.

plugins {
    kotlin("jvm").version("1.1.61")
}

//This is necessary to make the version accessible in other places
val kotlinVersion: String? by extra {
    buildscript.configurations["classpath"]
            .resolvedConfiguration.firstLevelModuleDependencies
            .find { it.moduleName == "kotlin-gradle-plugin" }?.moduleVersion
}

dependencies {
    compile(kotlin("stdlib", kotlinVersion))
}

for version 1.2+, you will have to replace with kotlin-gradle-plugin org.jetbrains.kotlin.jvm.gradle.plugin

like image 180
s1m0nw1 Avatar answered Oct 13 '22 20:10

s1m0nw1