如何从命令行向gradle询问属性的值? [英] How can I ask gradle for the value of a property from the command line?

查看:90
本文介绍了如何从命令行向gradle询问属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我希望shell脚本能够获取rootProject.name的值,我该怎么做?理想情况下,我想使用一些参数来调用./gradlew,并让它将属性值(而不是其他值)打印到stdout.

For example, if I wanted a shell script to be able to get the value of rootProject.name, how could I do this? Ideally, I'd like to invoke ./gradlew with some set of arguments and have it print the property value (and nothing else) to stdout.

推荐答案

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

For clarity, here is my Gradle wrapper version:

$ ./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

这是一项现有任务,可让您了解可用的属性:

This is an existing task to give you an idea of the properties available:

$ ./gradlew properties


> Task :properties

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

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

这是我用来打印项目属性的自定义任务

Here is a custom task I've built to print out a project property

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选项来抑制一些额外的输出.

And this is how I use my custom task with a --prop parameter (indicated by @Option(option = 'prop'. I'm using the -q (quiet) Gradle option to suppress some of the extra output.

$ ./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中,您可以检查命令的返回码以了解何时解析该值.成功输出的格式取决于您,您可以轻松对其进行解析.

Because we are throwing a GradleException when we can't find the property, in Bash you can check the return code of the command to know when to parse out the value. The formatting of a successful output is up to you and you could make it easily parsed.

$ ./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中获得一个非零的返回码,并且我们知道我们不需要尝试解析出该值:

In case of a failure like the one above, we get a non-zero return code in Bash, and we know we don't need to try and parse out the value:

$ echo $?
1

不幸的是,我不知道Gradle中仅提供 的简单方法来将您关心的值提供给stdout(防止进行某些解析),但这可以帮助您实现大部分的学习具有一定的灵活性.

Unfortunately, I don't know a simple way in Gradle to only give the value you are concerned with to stdout (prevent some parsing), but this gets you most of the way there with some flexibility.

这篇关于如何从命令行向gradle询问属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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