如何将ext。*变量转换为build.gradle.kts中的插件块 [英] How to get ext.* variables into plugins block in build.gradle.kts

查看:873
本文介绍了如何将ext。*变量转换为build.gradle.kts中的插件块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的构建文件如下所示:

  val nexusBaseUri:String by extra $ b $ 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)}
}
依赖项{
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)
}
}

插件{
应用程序
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//以下行导致问题
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
kotlin(jvm,kotlin_version)
$ b插件(kotlin-spring)
插件(org.springframework.boot)
插件(io.spring。依赖关系管理)
}

应用程序{
mainClassName =eqip.fid.FdmlInterpreterDeveloperAppKt
}

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

依赖项{
compile(kotlin( stdlib))
compile(org.springframework.boot:spring-boot-starter-web)
}

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

我在IntelliJ IDEA中遇到的错误是: b
$ b


'val kotlin_version:String'can not be在这种情况下由隐式接收器调用。

如何解决这个问题?

解决方案

您可以在插件中定义一个版本,然后使该版本可以在博客以外访问,例如在依赖关系部分。

 插件{
kotlin jvm)。version(1.1.61)
}

//这是使版本在其他地方可用的必要条件
val kotlinVersion:String?由额外{
buildscript.configurations [classpath]
.resolvedConfiguration.firstLevelModuleDependencies
.find {it.moduleName ==kotlin-gradle-plugin} ?. moduleVersion
}

依赖关系{
compile(kotlin(stdlib,kotlinVersion))
}

对于版本1.2+,您必须替换为 kotlin-gradle-plugin org.jetbrains.kotlin .jvm.gradle.plugin


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?

解决方案

You can define a version inside plugins and then make this version accessible outside of the blog, 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

这篇关于如何将ext。*变量转换为build.gradle.kts中的插件块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆