访问已遮蔽的Gradle属性

huangapple go评论70阅读模式
英文:

how to access a shadowed gradle property

问题

如何访问在当前上下文中被其他属性遮盖的Gradle属性变量?

示例:

gradle.properties

version = 1.0
someOtherVar = test

build.gradle

apply plugin 'java'
jar {
    archiveVersion = "${version}-${someOtherVar}"
}

在这种情况下,${version} 引用的是 jar.version 而不是具有相同名称的属性(属性 version 被 jar version 遮盖)。如何在 jar 上下文中获取 version 属性的值?

英文:

How can I access a gradle property variable that is shadowed by some other property in the current context?

Example:

gradle.properties

version = 1.0
someOtherVar = test

build.gradle

apply plugin 'java'
jar {
    archiveVersion = "${version}-${someOtherVar}"
}

In that context ${version} refers to the jar.version instead of the property having the same name (property version is shadowed by jar version). How can I get the value of the version property in the jar context?

答案1

得分: 0

项目的 version 字段可以通过 FQN project.version(对于当前项目)或 rootProject.version(对于根项目)访问。

英文:

The project's version fields can be accessed via the FQN project.version (for the current project) or rootProject.version (for the root project).

答案2

得分: 0

在这种特定情况下,您可以使用Project.version属性,但在更一般的情况下,请使用Project.property(propertyName)方法:

apply plugin 'java'
jar {
    archiveVersion = "${property('version')}-${someOtherVar}"
}
英文:

While in this specific case, you could use the Project.version property, in a more general case, use the Project.property(propertyName) method:

apply plugin 'java'
jar {
    archiveVersion = "${property('version')}-${someOtherVar}"
}

huangapple
  • 本文由 发表于 2020年1月3日 23:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定