如何在Gradle中创建任务一次性生成多语言Antlr4语法源码
Solution: Create a Composite Task to Generate All ANTLR4 Grammar Sources
Got it, let's get this sorted! The issue with your current setup is that the generateGrammarSource task is a single instance that switches behavior based on the Language project property—you can't run it three times with different configurations in one command. Instead, we'll create three language-specific variants of the task, then a composite task that triggers all three at once.
Here's the updated build.gradle code:
apply plugin: 'antlr' // Helper method to create language-specific grammar generation tasks def createGrammarTask(String language, String outputPath, String packageName = null) { def taskName = "generate${language.capitalize()}GrammarSource" tasks.create(taskName, AntlrTask) { group = 'antlr' description = "Generates ${language} sources from Antlr4 grammars." maxHeapSize = "64m" sourceSets.main.antlr.srcDirs = ['.'] includes = ['KpiAttributes.g4'] // Configure language and arguments arguments = ["-Dlanguage=${language}"] if (packageName) { arguments += ['-package', packageName] } // Set output directory outputDirectory = file(outputPath) // Python-specific post-processing if (language == 'Python2') { doLast { File initFile = new File("${outputPath}/__init__.py") initFile.write "" } } } return tasks[taskName] } // Create individual tasks for each language def javaTask = createGrammarTask('Java', "../../../../XSpotterGUI/sviluppo/src/com/xech/xspotter4/grammars/kpiattributes", "com.xech.xspotter4.grammars.kpiattributes" ) def jsTask = createGrammarTask('JavaScript', "../../../../XSpotterGUI/sviluppo/WebContent/xspotter4/js/xech/grammars/kpiattributes" ) def pythonTask = createGrammarTask('Python2', "python/engine_lib/kpi_attributes" ) // Create the composite task to run all three tasks.create('generateAllGrammarSources') { group = 'antlr' description = 'Generates grammar sources for Java, JavaScript, and Python2 in one go.' dependsOn(javaTask, jsTask, pythonTask) }
How This Works:
- Helper Method: The
createGrammarTaskfunction cuts down on repetitive code by wrapping the common setup for each language's task. It takes the language name, output path, and optional package name, then builds a fully configuredAntlrTaskinstance. - Language-Specific Tasks: We spin up three separate tasks (
generateJavaGrammarSource,generateJavaScriptGrammarSource,generatePython2GrammarSource) each tuned to their target language's requirements—no more toggling properties via command line. - Composite Task:
generateAllGrammarSourcesacts as a trigger: when you run it, Gradle will execute all three language tasks (in parallel or sequential, depending on your setup) to generate all your grammar sources in one shot.
To Run:
Execute this single command to generate outputs for all three languages at once:
./gradlew generateAllGrammarSources
You can still run individual language tasks if needed (e.g., ./gradlew generateJavaGrammarSource)—the original functionality is preserved.
内容的提问来源于stack exchange,提问作者user6180
相关产品推荐
相关产品推荐

