如何从命令行获取gradle属性? [英] How to get gradle property from command line?

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

问题描述

我正在尝试从命令行界面读取项目属性.有一个任务 gradle properties ,可以打印所有任务.因此,我可以这样写: gradle properties |grep"rootProject:根项目";|awk'{print $ NF}'|tr -d'" 并得到我想要的.返回的结果是正确的.我想使用适当的gradle命令来达到相同的结果.什么是gradle命令来获取项目名称?

I am trying read a project property from command line interface. There is a task gradle properties, which prints all of them. Therefore, I can write: gradle properties | grep "rootProject: root project" | awk '{print $NF}' | tr -d "'" and get what I want. The returned result is correct. I would like to use a proper gradle command to achieve the same result. What is a gradle command to get the project name?

这是我的 build.gradle :

plugins {
    id 'java'
}

tasks.register("rootProjectName") {
    doLast {
        println(rootProject.name)
    }
}

build {
    println 'hi'
}

推荐答案

看起来就像您在 rootProject.name 属性之后.没有内置的Gradle 任务 将为您提供该属性.您可以编写一个简单的任务,将该值打印到控制台,这将简化您的命令.

Looks like you're just after the rootProject.name property. There is no built-in Gradle task that will give you that property. You can write a simple task that prints that value to the console which will simplify your command.

tasks.register("rootProjectName") {
    doLast {
        println(rootProject.name)
    }
}

然后只需使用 -q 来调用该任务:

Then simply call that task with -q:

$ ./gradlew rootProjectName -q
demo

对于此示例,您可以看到 demo 只是打印出来.

You can see demo is simply printed out for this example.

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

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