Gradle 没有价值的自定义任务选项
Gradle custom task option without value
我正在开发一个自定义 gradle 任务,我想要一个选项,它的作用就像一个标志,不需要值。
我只是想检查它是否已设置
基本上:我可以使用插件 gradle my-task
或 gradle my-task --flag
并且能够检查 --flag
是否存在以定义插件处理。
我在官方文档中找不到任何内容
任务的自定义命令行选项自 Gradle 4.6 via @Option
annotation on task property setters. Documentation link: Declaring and Using Command Line Options 起可用。
根据文档,boolean
属性支持无值命令行选项。
boolean
, Boolean
, Property<Boolean>
Describes an option with the value true
or false
. Passing the option on the command line treats the value as true
. For example --enabled
equates to true
. The absence of the option uses the default value of the property.
(未测试)示例:
import org.gradle.api.tasks.options.Option;
public class MyTask extends DefaultTask {
private boolean flag;
@Option(option = "flag", description = "Sets the flag")
public void setFlag(boolean flag) {
this.flag = flag;
}
@Input
public boolean isFlag() {
return flag;
}
@TaskAction
public void doWork() {
if (flag) {
getLogger().quiet("Flag is present");
}
}
}
我正在开发一个自定义 gradle 任务,我想要一个选项,它的作用就像一个标志,不需要值。
我只是想检查它是否已设置
基本上:我可以使用插件 gradle my-task
或 gradle my-task --flag
并且能够检查 --flag
是否存在以定义插件处理。
我在官方文档中找不到任何内容
任务的自定义命令行选项自 Gradle 4.6 via @Option
annotation on task property setters. Documentation link: Declaring and Using Command Line Options 起可用。
根据文档,boolean
属性支持无值命令行选项。
boolean
,Boolean
,Property<Boolean>
Describes an option with the value
true
orfalse
. Passing the option on the command line treats the value astrue
. For example--enabled
equates totrue
. The absence of the option uses the default value of the property.
(未测试)示例:
import org.gradle.api.tasks.options.Option;
public class MyTask extends DefaultTask {
private boolean flag;
@Option(option = "flag", description = "Sets the flag")
public void setFlag(boolean flag) {
this.flag = flag;
}
@Input
public boolean isFlag() {
return flag;
}
@TaskAction
public void doWork() {
if (flag) {
getLogger().quiet("Flag is present");
}
}
}