如何从命令行获取gradle属性?

如何从命令行获取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.