如何为 gradle 自定义 Java 任务输入指定默认值?
How do I specify default values for gradle custom Java task inputs?
我有一个用 Java 编写的自定义 Gradle 任务作为 Gradle 插件的一部分,如下所示:
public abstract class MyTask extends DefaultTask {
private static final String DEFAULT_SERVICE_VERSION = "NO VERSION";
private String serviceVersion;
@Option(option = "serviceVersion", description = "Configures the service version.")
public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}
@Input
public String getServiceVersion() {
return serviceVersion;
}
@TaskAction
public void myTask() {
String serviceVersionCurrent = getServiceVersion() != null ? getServiceVersion() : DEFAULT_SERVICE_VERSION;
这让我可以调用带有 serviceVersion
作为参数的任务,如下所示:
gradle myTask --serviceVersion="0.1"
我想以某种方式使 serviceVersion 可选,如果未指定值,则默认为 DEFAULT_SERVICE_VERSION
像现在这样,失败了:
> gradle myTask
> Task :myTask FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Some problems were found with the configuration of task ':myTask' (type 'MyTask').
> No value has been specified for property 'serviceVersion'.
如果指定了 none,我应该如何注释我的 属性 以使其默认为某个值?
变量初始化时赋默认值即可:
public class GreetingTask extends DefaultTask {
private String serviceVersion = "v1.0";
@Option(option = "serviceVersion", description = "Configure service version")
public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}
@Input
public String getServiceVersion() {
return this.serviceVersion;
}
@TaskAction
public void task() {
System.out.println(this.serviceVersion);
}
}
输出将是:
$ ./gradlew greeting
> Task :greeting
v1.0
BUILD SUCCESSFUL in 2s
3 actionable tasks: 3 executed
$ ./gradlew greeting --serviceVersion=v2.0
> Task :greeting
v2.0
BUILD SUCCESSFUL in 1s
3 actionable tasks: 1 executed, 2 up-to-date
我有一个用 Java 编写的自定义 Gradle 任务作为 Gradle 插件的一部分,如下所示:
public abstract class MyTask extends DefaultTask {
private static final String DEFAULT_SERVICE_VERSION = "NO VERSION";
private String serviceVersion;
@Option(option = "serviceVersion", description = "Configures the service version.")
public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}
@Input
public String getServiceVersion() {
return serviceVersion;
}
@TaskAction
public void myTask() {
String serviceVersionCurrent = getServiceVersion() != null ? getServiceVersion() : DEFAULT_SERVICE_VERSION;
这让我可以调用带有 serviceVersion
作为参数的任务,如下所示:
gradle myTask --serviceVersion="0.1"
我想以某种方式使 serviceVersion 可选,如果未指定值,则默认为 DEFAULT_SERVICE_VERSION
像现在这样,失败了:
> gradle myTask
> Task :myTask FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Some problems were found with the configuration of task ':myTask' (type 'MyTask').
> No value has been specified for property 'serviceVersion'.
如果指定了 none,我应该如何注释我的 属性 以使其默认为某个值?
变量初始化时赋默认值即可:
public class GreetingTask extends DefaultTask {
private String serviceVersion = "v1.0";
@Option(option = "serviceVersion", description = "Configure service version")
public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}
@Input
public String getServiceVersion() {
return this.serviceVersion;
}
@TaskAction
public void task() {
System.out.println(this.serviceVersion);
}
}
输出将是:
$ ./gradlew greeting
> Task :greeting
v1.0
BUILD SUCCESSFUL in 2s
3 actionable tasks: 3 executed
$ ./gradlew greeting --serviceVersion=v2.0
> Task :greeting
v2.0
BUILD SUCCESSFUL in 1s
3 actionable tasks: 1 executed, 2 up-to-date