gpt4 book ai didi

gradle - 如何从命令行向gradle询问属性的值?

转载 作者:行者123 更新时间:2023-12-03 03:41:25 25 4
gpt4 key购买 nike

例如,如果我希望shell脚本能够获得rootProject.name的值,我该怎么做?理想情况下,我想使用一些参数来调用./gradlew,并使其将属性值(以及其他所有内容)打印到stdout。

最佳答案

为了清楚起见,这是我的Gradle包装器版本:

$ ./gradlew --version

------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------

Build time: 2019-04-26 08:14:42 UTC
Revision: 261d171646b36a6a28d5a19a69676cd098a4c19d

Kotlin: 1.3.21
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 11.0.2 (Oracle Corporation 11.0.2+9-LTS)
OS: Mac OS X 10.14.4 x86_64

这是一项现有任务,可让您大致了解可用的属性:
$ ./gradlew properties


> Task :properties

------------------------------------------------------------
Root project
------------------------------------------------------------

allprojects: [root project 'myProject', project ':otherProject', ...]
...
rootDir: /path/to/rootDir
rootProject: root project 'myProject'
...
version: 2.3.0
...

这是我为打印项目属性而构建的自定义任务
class ResolveProperties extends DefaultTask {

@Input
String prop

ResolveProperties() {
// if no --prop=<property> is provided, default to spitting out all properties
prop = "properties"
}

@Option(option = 'prop', description = 'Set the property to be evaluated for the project.')
void setProp(final String prop) {
this.prop = prop
}

@TaskAction
void resolveProp() {
List<String> propPath = this.prop.tokenize('.')
int n = propPath.size()
def currentProp = project
propPath.eachWithIndex { p, i ->
if(currentProp.hasProperty(p)) {
currentProp = currentProp.property(p)
}
else {
throw new GradleException("failed to resolve property: ${this.prop}")
}
}
println "${this.prop} -> ${currentProp}"
}
}

task resolveProperties(type: ResolveProperties)

这就是我将自定义任务与 --prop参数(由 @Option(option = 'prop'指示)一起使用的方式。我正在使用 -q(安静)Gradle选项来抑制一些额外的输出。
$ ./gradlew -q resolveProperties --prop=rootProject.name
rootProject.name -> onestop
resolveProperties took 0 seconds
$ ./gradlew -q resolveProperties --prop=rootProject.version
rootProject.version -> 2.3.0
resolveProperties took 0 seconds
$ ./gradlew -q resolveProperties --prop=rootProject.group
rootProject.group -> org.cedar.onestop
resolveProperties took 0 seconds

由于找不到属性时会抛出 GradleException,因此在Bash中,您可以检查命令的返回码以了解何时解析该值。成功输出的格式取决于您,您可以轻松对其进行解析。
$ ./gradlew -q resolveProperties --prop=does.not.exist
resolveProperties took 0 seconds

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/elliott/Documents/GitHub/onestop/build.gradle' line: 259

* What went wrong:
Execution failed for task ':resolveProperties'.
> failed to resolve property: does.not.exist

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s

如果发生类似上面的失败,我们将在Bash中得到一个非零的返回码,并且我们知道我们不需要尝试解析出该值:
$ echo $?
1

不幸的是,我不知道Gradle中是否有一种简单的方法仅将您关心的值提供给 stdout(防止进行某些解析),但是这可以使您获得大部分灵活性。

关于gradle - 如何从命令行向gradle询问属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57210735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com