我创建了一个这样的自定义 Gradle 插件,
DummyPlugin.java
public class DummyPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getTasks().create("dummy", DummyTask.class);
project.getExtensions().create("dummy", DummyExtension.class);
}
}
虚拟任务.java
public class DummyTask extends Test {
@TaskAction
public void dummy() {
Project project = getProject();
DummyExtension extension = project.getExtensions().findByType(DummyExtension.class);
System.out.println("Env of dummy is " + extension.getEnv());
}
}
DummyExtension.java
public class DummyExtension {
private String env;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
}
现在,我为不同的环境创建了三个任务,如下所示:
apply plugin: 'dummy'
task dummyCI(type: DummyTask) {
dummy {
env = 'ci'
}
}
task dummyDev(type: DummyTask) {
dummy {
env = 'dev'
}
}
task dummyTest(type: DummyTask) {
dummy {
env = 'test'
}
}
但是,在跑步时,我得到./gradlew dummyCI
的不是得到,而是得到。也一样。Env of dummy is ci
Env of dummy is test
./gradlew dummyDev
我该如何解决这个问题?谢谢。