在buildscript关闭Access项目额外属性在buildscript关闭Access项目额外

2019-05-12 06:45发布

我是新来的摇篮,并有有关项目属性的一些问题。

我需要在我的build.gradle多个地点申报春天启动的依赖性,而我想用一个变量定义版本。 什么是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
            }
        }
    }
}

Answer 1:

移动ext块里面buildscript块解决了这个问题对我来说。 不知道这是否是正式支持,虽然,因为它有效地配置project.ext从(很特别) buildscript块。



Answer 2:

这将无法正常工作。

首先, buildscript块在一开始被评估,Groovy脚本的任何其他部分之前。 因此,在定义的属性ext块只是不当时存在。

其次,我不确定如果交换之间的性能buildscript和脚本的另一部分是可能的。



Answer 3:

因为buildscript块被首先计算 ,前springBootVersion已被定义。 因此,变量定义必须在去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}")
    }
}


文章来源: Access project extra properties in buildscript closure
标签: gradle