Gradle and Antlr 3.X

It is my eternal goal to write simple builds for projects, but they always end up in a heap of shell script, groovy, ant, java and other bits and pieces.

I thought I’d look at gradle to clean up a few of the edges and used it for a new project I was doing (to replace a series of shell scripts that read eclipse .classpath files with an Antlr based parser).

Unfortunately, there is no default support for Antlr3 in gradle yet. There’s a pull request to add Antlr3 support in the works, but it’s not made it into the main code line, and has been around for a few months gathering dust. In the mean time, I’ve adapted a script I found on Tapestry Central.

It’s my first gradle script and it hasn’t been an entirely pleasant experience so far. It might pay for itself in terms on server builds though. Time will tell.

apply plugin: 'java'

project.ext.grammarpackage = "eclipse"

repositories {

configurations {

dependencies {
compile 'org.antlr:antlr-runtime:3.2'
antlr3 'org.antlr:antlr:3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'

task antlrOutputDir << {

task generateGrammarSource(dependsOn: antlrOutputDir, type: JavaExec) {
description = 'Generates Java sources from Antlr3 grammars.'

destinationDir = "$buildDir/generated-sources/antlr"
def antlrSource = 'src/main/antlr'

inputs.dir file(antlrSource)
outputs.dir file(destinationDir)

def grammars = fileTree(antlrSource).include('**/*.g')

main = 'org.antlr.Tool'
classpath = configurations.antlr3
args = ["-o", "${destinationDir}/${project.ext.grammarpackage}", grammars.files].flatten()

compileJava {
dependsOn generateGrammarSource
source generateGrammarSource.destinationDir


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s