properties中的点属性

properties中的点属性

本文介绍了访问存储在settings.gradle中的gradle.properties中的点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gradle 6.0.1项目. build.gradle(摘录)如下:

I have a Gradle 6.0.1 project. The build.gradle (excerpt) looks like:

plugins {
  id "application"
  id "com.github.edeandrea.xjc-generation"
  id "eclipse"
  id "idea"
  id "java"
  id "org.springframework.boot"
}

...

dependencies {
  implementation platform("org.springframework.boot:spring-boot-dependencies:${property("spring-boot.version")}")

  // ...more stuff here
}

// ...more stuff here

我正在管理settings.gradle中的所有插件版本:

I'm managing all the plugin versions in settings.gradle:

pluginManagement {
  plugins {
    id "application"
    id "com.github.edeandrea.xjc-generation" version "1.0"
    id "eclipse"
    id "idea"
    id "java"
    id "org.springframework.boot" version "${spring-boot.version}"
  }
}

rootProject.name = "spring-core"

...而且我通常将工件版本放在gradle.properties中:

...and I usually put the artifact versions in gradle.properties:

#
# Dependency Versions
oracle.version = 18.3.0.0
spring-boot.version = 2.2.1.RELEASE

#
# Gradle Settings
org.gradle.configureondemand = false
org.gradle.daemon = false

#
# System Settings
systemProp.file.encoding = UTF-8
systemProp.sun.jnu.encoding = UTF-8

现在的问题是,我无法像在build.gradle中一样从settings.gradle中读取settings.gradle中的点属性-我已经尝试使用${property("spring-boot.version")}.

Now the problem is I can't read dot-properties in settings.gradle (from gradle.properties) in the same way I do it inside build.gradle — I already tried using ${property("spring-boot.version")}.

有什么方法可以实现?我可以轻松地将密钥更改为springBootVersion之类的方法,并且可以正常工作,但是我想知道是否有一种方法可以使我目前拥有它:spring-boot.version.

Is there any way to achieve that? I can easily change the key to something like springBootVersion and it works, but I wonder if there is a way to have in the way I currently have it: spring-boot.version.

推荐答案

使用getProperty("spring-boot.version")

带有其他变体的简单gradle项目

simple gradle project with additional variants


task test{
    doLast {
        //success
        println project.property('aaa.bbb.ccc')
        println project.'aaa.bbb.ccc'
        println getProperty('aaa.bbb.ccc')

        //failure: Could not get unknown property 'aaa.bbb.ccc' for task ':test' of type org.gradle.api.DefaultTask
        println property('aaa.bbb.ccc')
    }
}

gradle.properties

gradle.properties

aaa.bbb.ccc=12345


property('aaa.bbb.ccc')失败,因为它尝试获取当前对象(任务)的属性,但为项目定义了aaa.bbb.ccc


property('aaa.bbb.ccc') fails because it tries to get property on current object (task) but aaa.bbb.ccc defined for project

project.property('aaa.bbb.ccc')成功是因为它应该成功

however project.property('aaa.bbb.ccc') succeeds because it should

project.'aaa.bbb.ccc'与groovy中的project.getProperty('aaa.bbb.ccc')相同

project.'aaa.bbb.ccc' is the same as project.getProperty('aaa.bbb.ccc') in groovy

project.getProperty('aaa.bbb.ccc')之所以起作用是因为groovy基本对象 GroovyObject (IHMO)

project.getProperty('aaa.bbb.ccc') works because of groovy basic object GroovyObject (IHMO)

getProperty(name)实际上位于 org.gradle.groovy.scripts.BasicScript 并没有真正的文档...

and getProperty(name) without prefix actualy located in org.gradle.groovy.scripts.BasicScript and not really documented...

这篇关于访问存储在settings.gradle中的gradle.properties中的点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:51