What is minimal sample Gradle project for ANTLR4 (with antlr plugin)?

后端 未结 7 1585
陌清茗
陌清茗 2021-02-14 03:47

I have created new Gradle project, added

apply plugin: \'antlr\'

and

dependencies {
    antlr \"org.antlr:antlr4:4.5.3\"
         


        
7条回答
  •  耶瑟儿~
    2021-02-14 04:45

    I will add onto other answers here.

    Issue 1: Generated source files are placed in build/generated-src folder.

    I found this discussion, but the solution there (setting outputDirectory property) is a bad idea. If you do gradle clean build command, this will clear out your entire source directory. The discussion there gives a good explanation as to why you should not

    the antlr generated sources are generated into a subdirectory of the "build" folder like all other artifacts, which are generated during the build. Furthermore your generated directory projectRoot/build/generated-src/antlr/main is added to the java sourceset definition to be sure its considered compileJava task. If you write the antlr generated source directly to the src/main/java folder you're polluting your source folder with output of your build process. ... Polluting your source folder during your build is an antipattern I think.

    However, if you want to do this, you can add a gradle task to copy the generated files to the build directory.

    generateGrammarSource << {
        println "Copying generated grammar lexer/parser files to main directory."
        copy {
            from "${buildDir}/generated-src/antlr/main"
            into "src/main/java"
        }
    }
    

    Issue 2: Generated source files do not have package attribute set.

    To solve this issue, add something like the following near the top of the grammar file:

    @header {
    package com.example.my.package;
    }
    

提交回复
热议问题