在buildscript closure中访问项目的额外属性 [英] Access project extra properties in buildscript closure

查看:199
本文介绍了在buildscript closure中访问项目的额外属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要在build.gradle中的多个位置声明spring引导相关性,并且我会喜欢用一个变量来定义版本。 Gradle中最好的方法是什么? (在Maven中,我使用属性)

我的尝试是使用额外的属性,但它不能访问buildscript闭包中的属性。我搜索并阅读了许多文章访问自定义任务中的属性。我错过了什么?

  ext {
springBootVersion ='1.1.9.RELEASE'
}

buildscript {

print project.springBootVersion //在这里失败

存储库{
mavenLocal()
mavenCentral()
}
依赖关系{
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 this works
}
}
}
}


解决方案

移动 ext 块在 buildscript 块内解决了我的问题。不知道这是否正式支持,因为它可以从(非常特殊的) buildscript 块有效地配置 project.ext


I am new to gradle and have some question about project properties.

I need to declare spring boot dependences at multiple locations in my build.gradle and I'd like to use a variable to define the version. What is the best way in gradle? (in Maven, I use properties)

My attempt is use extra properties, but it cannot access the property in the buildscript closure. I googled around and read many articles accessing properties in custom tasks. What did I miss?

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
            }
        }
    }
}

解决方案

Moving the ext block inside the buildscript block solves the problem for me. Not sure if this is officially supported though, as it's effectively configuring project.ext from the (very special) buildscript block.

这篇关于在buildscript closure中访问项目的额外属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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