gradle - 在buildscript闭包中访问项目的额外属性

标签 gradle

我是新手,对项目属性有一些疑问。

我需要在build.gradle的多个位置声明spring boot的依赖关系,我想使用一个变量来定义版本。最好的方式是gradle? (在Maven中,我使用属性)

我的尝试是使用额外的属性,但无法访问buildscript闭包中的属性。我在Google周围搜索并阅读了许多有关访问自定义任务中属性的文章。我错过了什么?

ext {
    springBootVersion = '1.1.9.RELEASE'
}

buildscript {

    print project.springBootVersion //fails here

    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.springBootVersion}")
    }
}

install {
    repositories.mavenInstaller {
        pom.project {
            parent {
                groupId 'org.springframework.boot'
                artifactId 'spring-boot-starter-parent'
                version "${project.springBootVersion}" //this one works
            }
        }
    }
}

最佳答案

ext块移到buildscript块内可以为我解决问题。但不确定是否已正式支持此功能,因为它可以从(非常特殊的)project.ext块中有效配置buildscript

关于gradle - 在buildscript闭包中访问项目的额外属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40294552/

相关文章:

android - 不推荐使用的Gradle功能使其与Gradle 5.0不兼容。 Android Studio 3.2

java - 我的 Android 应用程序遇到问题

java - 并行运行testNG套件

android - 将依赖于本地 Android 库项目的 Android 库发布到 Maven

android - 未在 android studio 中创建的新项目

spring - 无法解析 intelliJ 中 EnableEurekaServer 的符号。是的,我有依赖项/无效+重新启动了我的 IDE)

android - 如何将我在 GitHub 上的库添加到 Android Gradle 文件中?

maven - 没有可用版本时,Gradle/Maven下载快照

java - 为每个构建类型解析应用程序标签

java - 无法获取 org.gradle.api.Project 类型的根项目 'artifactory_contextUrl' 的未知属性 '[redacted]'