How to have a task tell compileJava to dependsOn(this)?


(monty.zukowski) #1

I’m trying to DRY up this code. Specifically, I’d like class AntlrTask to automatically call something like:

compileJava {
    dependsOn this
    source this.generatedJavaFiles
}

However, if I just put that block into my AntlrTask definition it complains. How can I say that compileJava dependsOn all of the AntlrTasks?

Sample code below:

dependencies {
    antlr2 ':antlr:2.7.7'
    antlr3 ':antlr-tool:3.0', ':antlr-runtime:3.0.1', ':antlr:2.7.7', ':stringtemplate:3.2'
    antxr
':antxr:0.9.0'
}
  task generateMappingParser(type: AntlrTask) {
    generatedJavaFiles = [file(destinationDir+"MappingParser.java")]
    grammarSources = [file(destinationDir+'mapping.antxr')]
    antlrTool = 'com.javadude.antxr.Tool'
    toolClasspath = configurations.antxr
}
  task generateChoreoParser(type: AntlrTask) {
    generatedJavaFiles = [file(destinationDir+"ChoreoParser.java")]
    grammarSources = [file(destinationDir+'choreo.antxr')]
    antlrTool = 'com.javadude.antxr.Tool'
    toolClasspath = configurations.antxr
}
  task generateExpressionParser(type: AntlrTask) {
    generatedJavaFiles = [file(destinationDir+"ExpressionParser.java")]
    grammarSources = [file(destinationDir+'ExpressionGrammar.g')]
    antlrTool = 'antlr.Tool'
    toolClasspath = configurations.antlr2
}
  compileJava {
    dependsOn generateMappingParser, generateChoreoParser, generateExpressionParser
    source generateMappingParser.generatedJavaFiles, generateChoreoParser.generatedJavaFiles, generateExpressionParser.generatedJavaFiles
}
  class AntlrTask extends DefaultTask {
  def destinationDir = "src/main/java/com/temboo/arcturus/choreo/"
      @OutputFiles
  def generatedJavaFiles
    @InputFiles
  def grammarSources
      def toolClasspath
  def antlrTool
      @TaskAction
  def runAntlr() {
        project.javaexec {
            main = antlrTool
            classpath = toolClasspath
            args = ["-o", "${destinationDir}", grammarSources].flatten()
        }
  }
}

(Peter Niederwieser) #2
compileJava {
    dependsOn tasks.withType(AntlrTask)
}