java - How do I declare gradle Antlr task output specs to avoid unnecessary rebuilds -


i have typical antlr 4.5 project 2 grammar files: mylexer.g4 , myparser.g4. them, antlr generates 6 output files: mylexer.java, mylexer.tokens, myparser.java, myparser.tokens, myparserbaselistener.java , myparserlistener.java. gradle tasks working correctly output files generated, compiled , tested expected.

the problem gradle sees 6 target files being out of date, every run or debug session has regenerate them , therefore has recompile main java project if none of source files have changed.

the gradle task generates file has output spec defined folder 6 output files generated. think need way define being 6 specific files rather output folder. don't know syntax that.

here's pertinent part of build.gradle file:

ext.antlr4 = [     antlrsource:    "src/main/antlr",     destinationdir: "src/main/java/com/myantlrquestion/core/antlr/generated",     grammarpackage:               "com.myantlrquestion.core.antlr.generated" ]  task makeantlroutputdir << {     file(antlr4.destinationdir).mkdirs() }  task compileantlrgrammars(type: javaexec, dependson: makeantlroutputdir) {     // grammars conveniently sorted alphabetically. assume remain true.     // ensures files named *lexer.g4 listed , therefore processed before corresponding *parser.g4     // matters because lexer must processed first since parser needs .tokens file lexer.     // note output file naming convention combined grammars different separate lexer , parser grammars.     def grammars = filetree(antlr4.antlrsource).include('**/*.g4')     def target = file("${antlr4.destinationdir}")     inputs.files grammars     // todo: output spec incorrect, task never considered date.     // todo: tweak outputs collection correct combined grammars separate lexer , parser grammars.     outputs.dir target      main = 'org.antlr.v4.tool'     classpath = configurations.antlr4     // antlr command line args @ https://theantlrguy.atlassian.net/wiki/display/antlr4/antlr+tool+command+line+options     args = ["-o", target,             "-lib", target,             //"-listener",      //"-listener" default             //"-no-visitor",    //"-no-visitor" default             "-package", antlr4.grammarpackage,             grammars.files      ].flatten()      // include optional description , group (shown ./gradlew tasks command)     description = 'generates java sources antlr4 grammars.'     group       = 'build' }  compilejava {     dependson compileantlrgrammars     // next line isn't technically needed unless antlr4.destinationdir not under builddir, doesn't hurt either     source antlr4.destinationdir }  task cleanantlr {     delete antlr4.destinationdir } clean.dependson cleanantlr 

i discovered problem not target files out of date, rather, due bug in cleanantlr task, being deleted every time gradle task run. problem of code in cleanantlr being run during gradle's initialization , configuration phase, if cleanantlr task wasn't being executed.

originally, task defined as:

task cleanantlr {     delete antlr4.destinationdir } clean.dependson cleanantlr 

the solution define this: (note "<<" after task name.)

task cleanantlr << {     delete antlr4.destinationdir } clean.dependson cleanantlr 

... or, additional clarity, use more verbose, functionally equivalent task definition:

task cleanantlr {     dolast() {         // sure wrap execution phase code inside dolast().          // otherwise run during initialization or configuration phase, when unrelated task is run.         // run when netbeas ide first loaded project.         //println 'deleting antlr directory: ' + antlr4.destinationdir         delete antlr4.destinationdir     } } clean.dependson cleanantlr 

with bug fixed, original outputs specification compileantlrgrammars task works correctly. there no need specify each individual output file. explained quite in section 15.9.2 of https://gradle.org/docs/current/userguide/more_about_tasks.html.

def grammars = filetree(antlr4.antlrsource).include('**/*.g4') def target = file("${antlr4.destinationdir}") inputs.files grammars outputs.dir target 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -