我正在编写一个 Java 插件,它在内部应用多个代码质量/静态分析插件来将它们的任务添加到项目中,例如,我正在添加 CheckStyle、PMD、CPD .... 等,现在我想添加 GMetrics 这是普通 gradle 中的 Ant 任务我这样做:
task gmetrics {
doLast {
ant.taskdef(name: 'gmetrics', classname: 'org.gmetrics.ant.GMetricsTask', classpath: configurations.gmetrics.asPath)
//ensure reporting directory created
ant.gmetrics() {
report(type: 'org.gmetrics.report.XmlReportWriter') {
option(name: 'outputFile', value: "${reportsDir}/gmetrics.xml")
}
fileset(dir: 'src') {
include(name: '**/*.java')
exclude(name: '**/*Config*')
}
}
}
}
如何在 Java Task 类中编写它,我在我的插件中执行以下操作:
project.getTasks().create("gmetrics", GmetricsTask.class);
然后这个 GmetricsTask.class 看起来像这样:
public class GmetricsTask extends BaseTask {
@TaskAction
public void gmetrics() throws TaskExecutionException {
getProject().getAnt(); // <-- can I do anything with this builder OR:
getProject().ant(new Closure<???>() {
@Override
public void run() {
// this closure doesn't compile: Cannot resolve Closure
}
});
}
}