【发布时间】:2015-04-22 17:38:41
【问题描述】:
我有一个典型的 Antlr 4.5 项目,其中包含两个语法文件:MyLexer.g4 和 MyParser.g4。 Antlr 从中生成 6 个输出文件:MyLexer.java、MyLexer.tokens、MyParser.java、MyParser.tokens、MyParserBaseListener.java 和 MyParserListener.java。 gradle 任务都正常工作,因此输出文件都按预期生成、编译和测试。
问题在于 gradle 认为这 6 个目标文件总是过时的,因此每次运行或调试会话都必须重新生成它们,因此即使源文件都没有更改,也必须重新编译主 java 项目。
生成文件的 gradle 任务将输出规范定义为生成 6 个输出文件的文件夹。我认为我需要一种方法将其定义为 6 个特定文件而不是输出文件夹。我只是不知道这样做的语法。
这是我的 build.gradle 文件的相关部分:
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 are conveniently sorted alphabetically. I assume that will remain true.
// That ensures that files named *Lexer.g4 are listed and therefore processed before the corresponding *Parser.g4
// It matters because the Lexer must be processed first since the Parser needs the .tokens file from the Lexer.
// Also note that the output file naming convention for combined grammars is slightly different from separate Lexer and Parser grammars.
def grammars = fileTree(antlr4.antlrSource).include('**/*.g4')
def target = file("${antlr4.destinationDir}")
inputs.files grammars
// TODO: This output spec is incorrect, so this task is never considered up to date.
// TODO: Tweak the outputs collection so it is correct with combined grammars as well as separate Lexer and Parser grammars.
outputs.dir target
main = 'org.antlr.v4.Tool'
classpath = configurations.antlr4
// Antlr command line args are at https://theantlrguy.atlassian.net/wiki/display/ANTLR4/ANTLR+Tool+Command+Line+Options
args = ["-o", target,
"-lib", target,
//"-listener", //"-listener" is the default
//"-no-visitor", //"-no-visitor" is the default
"-package", antlr4.grammarpackage,
grammars.files
].flatten()
// include optional description and group (shown by ./gradlew tasks command)
description = 'Generates Java sources from ANTLR4 grammars.'
group = 'Build'
}
compileJava {
dependsOn compileAntlrGrammars
// this next line isn't technically needed unless the antlr4.destinationDir is not under buildDir, but it doesn't hurt either
source antlr4.destinationDir
}
task cleanAntlr {
delete antlr4.destinationDir
}
clean.dependsOn cleanAntlr
【问题讨论】:
标签: java gradle dependencies antlr