[GRADLE-3323] Antlr plugin should preserve package structure Created: 21/Jul/15  Updated: 18/Aug/15  Resolved: 18/Aug/15

Status: Resolved
Project: Gradle
Affects Version/s: 2.2
Fix Version/s: 2.7-rc-1

Type: Bug
Reporter: René Gröschke (Inactive) Assignee: René Gröschke (Inactive)
Resolution: Fixed Votes: 1


Henry S:
The simplest antlr build seems to always place generated code at the base of build/generated-src/antlr/main/. This causes java errors if a package was defined in the .g files, like this:


{ package com.example.foo.antlr; }

I realize it might be difficult for the Antlr plugin to be aware of that package information, but an alternate solution would be to mirror the source folder structure like resources do:

Thus this .g file:

Should have generated code placed under a similar package structure:

(I should mention that simply specifying the outputDirectory does not work in the case of multiple .g files with different packages)

Steve Ebersole:

I second this.

And what's worse is that this seems to be a regression. I believe that this used to work in versions of Gradle prior to 2.3/2.4 when the Antlr plugin was significantly changed.

For additional information... putting the grammar file in the proper "package directory structure".. the plugin does find the grammar files, but it flattens the output back out into a root directory rather than honoring the "package directory structure".

Interestingly (not sure how) compiling the generated sources works out ok. Using Henry's example..

Foo.g declares package com.example.foo.antlr;
No matter where that file exists under src/main/antlr it is found, but the generated sources end up in ${buildDir}/generated-src/antlr/main rather than in ${buildDir}/generated-src/antlr/main/com/example/foo/antlr
On compile, somehow, the right thing does happen. Not only are the classes compiled, but somehow they get put into ${buildDir}/classes/main/com/example/foo/antlr
The Hibernate build[1] illustrates this if you need a way to reproduce. Hibernate is using Antlr 2, while it would appear that Henry is using Antlr 3 or 4. So that may be a difference too.

Ultimately the issue is in IDEs. If you import the Hibernate project into your IDE it (properly) shows ${buildDir}/generated-src/antlr/main as a source directory, however it shows it being incorrect since the sources are not in their proper directories according to their package statements.

I find it fascinating that the compiled classes end up in the proper directory. I am almost positive javac is not doing that.

[1] http://github.com/hibernate/hibernate-orm

Comment by René Gröschke (Inactive) [ 21/Jul/15 ]

This is a regression at it worked with prior gradle versions for antlr2. I have a simple fix for this available

Generated at Wed Jun 30 12:45:47 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.