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

I created a new Gradle project, added

apply plugin: 'antlr'

and

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

build.gradle.

A file has been created src/main/antlr/test.g4with the following contents

grammar test;
r   : 'hello' ID;
ID  : [a-z]+ ;
WS  : [ \t\r\n]+ -> skip ;

But that does not work. Java source files are not generated (and there were no errors).

What did I miss?

The project is here: https://github.com/dims12/AntlrGradlePluginTest2

UPDATE

I found that my example really works, but it put the code into \build\generated-srcwhat I did not expect: shame:

+7
source share
7 answers

I will add other answers here.

Problem 1 . The generated source files are placed in a folder build/generated-src.

, ( outputDirectory) bad. gradle clean build, . ,

antlr "build", , . , projectRoot/build/-src/antlr/main java sourceet, , compileJava. , antlr, src/main/java , , .... , .

, , gradle .

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

2. .

, - :

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

, :

  • : @header{ package com.example.something.antlrparser; } @header{ package com.example.something.antlrparser; } .
  • , , src/main/antlr/com/example/something/antlrparser/grammar.g4

, gradle generateGrammarSource, .java /build/generated-src/antlr/main/com/example/something/antlrparser/*.java IntelliJ, Gradle.

build.gradle :

group 'com.example.something'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'antlr'
apply plugin: 'idea'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    antlr "org.antlr:antlr4:4.5" // use ANTLR version 4
}
+3

build.gradle

generateGrammarSource {
    outputDirectory = file("src/main/java/com/example/parser")
}

"";

@header {
    package com.example.parser;
}

Java8- antlr

:

Antlr docs.gradle.org

+2

Gradle, @Mark Vieira, . , ANTLR , ( , ).

grammar MyGrammar;

@header {
    package com.mypackage;
}

Gradle ANTLR IntelliJ, . Gradle , .

: fooobar.com/questions/137978/...

+1

Gradle STS antlr4. :

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      generateGrammarSource
[sts] -----------------------------------------------------
:generateGrammarSource UP-TO-DATE

. !

Gradle The STS plugin does not create source files for antlr4.  You must use a new plugin or command line

0

2:

gradle.build:

  generateGrammarSource {
        maxHeapSize = "64m"
        arguments += ["-visitor", 
                      "-long-messages", 
                      "-package", "your.package.name"]

}
0

All Articles