Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gradle 在buildscript闭包中访问项目额外属性_Gradle - Fatal编程技术网

Gradle 在buildscript闭包中访问项目额外属性

Gradle 在buildscript闭包中访问项目额外属性,gradle,Gradle,我是gradle的新手,对项目属性有一些疑问 我需要在build.gradle中的多个位置声明spring启动依赖项,并且我想使用一个变量来定义版本。在格拉德尔最好的方式是什么?(在Maven中,我使用属性) 我尝试使用额外属性,但无法访问buildscript闭包中的属性。我在谷歌上搜索并阅读了许多文章,这些文章都是在自定义任务中访问属性的。我错过了什么 ext { springBootVersion = '1.1.9.RELEASE' } buildscript { pr

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

我需要在build.gradle中的多个位置声明spring启动依赖项,并且我想使用一个变量来定义版本。在格拉德尔最好的方式是什么?(在Maven中,我使用属性)

我尝试使用额外属性,但无法访问buildscript闭包中的属性。我在谷歌上搜索并阅读了许多文章,这些文章都是在自定义任务中访问属性的。我错过了什么

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
            }
        }
    }
}
这是行不通的

首先,在groovy脚本的任何其他部分之前,从一开始就对
buildscript
块进行评估。因此,在
ext
块中定义的属性当时并不存在


其次,我不确定是否可以在
buildscript
和脚本的其他部分之间交换属性。

ext
块移动到
buildscript
块中,我就解决了这个问题。但不确定这是否得到官方支持,因为它从(非常特殊的)
buildscript
块有效地配置了
project.ext

因为
buildscript
块是在定义
springBootVersion
之前首先计算的。因此,变量定义必须在任何其他定义之前进入
buildscript
块:


感谢您提供有关buildscript块的计算时间的信息。是否有其他替代方案来实现我的目标?目前不知道如何处理。几乎可以肯定通过外部文件,但在我看来没有意义。谢谢,彼得。这对我有效,还没有发现任何副作用。
buildscript {

    ext {
        springBootVersion = '1.1.9.RELEASE'
    }

    print project.springBootVersion //Will succeed

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