我正在编写一个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
}
});
}
}