使用不同的属性多次从gradle运行已加载的ant任务

nue99wik  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(134)

我们有一个旧的ant构建系统,它仍然有必要继续使用。当我们将功能迁移到gradle时,我们仍然在gradle中调用一些更有用的ant目标。其中一个有用的ant目标是报告摘要附加器,它可以使用生成的任何报告更新索引文件。
我正在将checkstyle添加到我们的gradle构建中,并尝试为checkstyle生成的每个报告调用此目标。但是,我似乎不知道如何从gradle多次调用该ant目标,但是具有不同的属性,因为Ant属性似乎对于整个构建是全局的。
目前为止我得到的信息是:

ant.importBuild('build.xml') { antTargetName ->
    'ant-' + antTargetName
}

checkstyleMain {
  doLast {
    ant.properties['report.prop1'] = 'foo'
    ant.properties['report.prop2'] = 'bar'
    ant.properties['report.prop3'] = 'war'
  }
}
checkstyleMain.finalizedBy 'ant-report-summary'

checkstyleTest {
  doLast {
    ant.properties['report.prop1'] = 'aaa'
    ant.properties['report.prop2'] = 'bbb'
    ant.properties['report.prop3'] = 'ccc'
  }
}
checkstyleTest.finalizedBy 'ant-report-summary'

check.dependsOn checkstyleMain, checkstyleTest

这在我运行check时不起作用,因为ant-report-summary只执行一次(gradle认为它不需要运行同一个任务3次),所以只使用上次运行的checkstyle任务的属性:

> Task :checkstyleMain
...
> Task :checkstyleTest
> Task :ant-report-summary

我希望每个checkstyle任务都运行一次ant-report-summary,并使用doLast中指定的属性。

qv7cva1a

qv7cva1a1#

工作示例here
我无法让它与AntBuilder一起工作(许多脑细胞死亡),所以我的方法将属性外包给Ant端。

ant report-summary -propertyfile main.properties
ant report-summary -propertyfile test.properties

其中,main.propertiestest.properties与预期一致。那么,如果我们使用Exec任务,事情就简单一些。
用于调用Ant的基类任务(**注:**为重构而编辑):

abstract class AntReportSummaryTask extends Exec {
    AntReportSummaryTask() {
        standardOutput = new ByteArrayOutputStream()

        ext.output = {
            return standardOutput.toString()
        }
    }
}

和特定任务(以下命令行适用于Unix,但可以轻松更改为Windows):

tasks.register('antReportSummaryMain', AntReportSummaryTask) {
    commandLine 'ant', 'report-summary', '-propertyfile', 'main.properties'
    doLast {
        println "TRACER antReportSummaryMain output:"
        println ext.output()
    }
}

tasks.register('antReportSummaryTest', AntReportSummaryTask) {
    commandLine 'ant', 'report-summary', '-propertyfile', 'test.properties'
    doLast {
        println "TRACER antReportSummaryTest output:"
        println ext.output()
    }
}

然后:

checkstyleMain.finalizedBy antReportSummaryMain
checkstyleTest.finalizedBy antReportSummaryTest

并且在我的工作示例中,./gradlew clean check将调用Ant两次。

相关问题