Home > Net >  Running loaded ant task from gradle multiple times with different properties
Running loaded ant task from gradle multiple times with different properties

Time:11-18

We have an old ant build system that is still somewhat necessary to carry over. As we migrate functionality to gradle, we're still calling some of the more useful ant targets within gradle. One such useful ant target is a report summary appender which updates an index file with any reports that have been generated.

I'm adding checkstyle to our gradle build and attempting to call this target for each report that checkstyle generates. This report target requires some properties to be set, and those properties are unique for each checkstyle task (main vs. test). However, I can't seem to figure out how to invoke that ant target from gradle multiple times, but with different properties as ant properties seem to be global for the whole build.

What I've got so far:

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

This doesn't work when I run check because ant-report-summary is only executed once (gradle figures it doesn't need to run the same thing 3 times), so only uses the properties of the last run checkstyle task:

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

I want ant-report-summary to run once for each checkstyle task, with the properties specified in the doLast. Is this possible with clever task structuring or some other way?

CodePudding user response:

Working example here

I wasn't able to get it to work with AntBuilder (many braincells died) so my approach out-sources the properties to the Ant side. Consider the following command-lines:

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

where main.properties and test.properties are as expected. Then, if we use Exec tasks, things are a bit simpler.

A base-class task for calling Ant (note: edited for refactoring):

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

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

and specific tasks with appropriate command-line (The following works for Unix, but can be easily changed to 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()
    }
}

then:

checkstyleMain.finalizedBy antReportSummaryMain
checkstyleTest.finalizedBy antReportSummaryTest

and in my working example, ./gradlew clean check will call Ant twice.

  • Related