New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Antlr plugin adds compile dependency on complete binary JAR #820
Comments
So you're saying that because of an old version of antlr, that became obsolete ten years ago, we should ship build tools to our customers / live environments? :) |
A small workaround is to just removing the extendsFrom antlr and manually adding the runtime:
|
When will this issue be fixed it is at least 2 years old? Supporting ANTLR 2.2 is in my opinion not needed since it is deprecated and at least 10 years old. |
Why #763 is reverted? |
Regarding point 3., Another option to consider is doing it like for the codequality plugins. @gosp because it breaks existing builds |
… runtime, but apparently, 4 years later, still does. gradle/gradle#820
- remove antlr from dependencies, see gradle/gradle#820 - remove duplicate Jetty dependency
Another workaround: let buildSrc use Example: https://github.com/JetBrains/Arend |
The workaround in #820 (comment) works for me, it is beyond my imagination why this problem is not fixed in a clean way on plugin level. Adding the compileOnly dependency needed to generate the parser / lexer classes as runtime dependency is just wrong. |
With all the breaking changes in Gradle I don't think "because it's breaking builds" can be used as an argument any longer. |
I also think that the ANTLR plugin should be fixed. However, I am stuck with the workaround #820 (comment). How do I do that with Kotlin Script? I tried
but got
Edit: Fix in #820 (comment) So I found yet another workaround:
|
In your first try you translated the workaround wrongly. You extend from all configurations except antlr, so you for example also extend compile from compile. The workaround filters the ones currently extended from. The correct Kotlin version is configurations {
compile {
setExtendsFrom(extendsFrom.filterNot { it == antlr.get() })
}
} |
There are other cases where this works as intended, e.g. the annotationProcessor configuration. It would also be totally insane that all these dependencies would be included at runtime. Why does this not work the same way for the antlr configuration out of the box? Does not make any sense imho. |
Thank you @Bytekeeper! You gave this community a pretty good workaround 👍 I preferred to replace the deprecated Following is my Groovy snippet: configurations {
compile {
// Undo extendsFrom relationship between the 'antlr' configuration and the 'api' configuration
// See https://github.com/gradle/gradle/blob/master/subprojects/antlr/src/main/java/org/gradle/api/plugins/antlr/AntlrPlugin.java#L60
extendsFrom = extendsFrom.findAll { it != configurations.antlr }
}
}
def antlrVersion = '4.9'
dependencies {
antlr group: 'org.antlr', name: 'antlr4', version: antlrVersion
implementation group: 'org.antlr', name: 'antlr4-runtime', version: antlrVersion
} I hope I could find some spare time to push for a proper fix in the Groovy source code of the ANTLR plugin instead of having to apply the above workaround in my |
2021 dependencies {
// ...
antlr("org.antlr:antlr4:4.9")
implementation("org.antlr:antlr4-runtime:4.9")
}
configurations[JavaPlugin.API_CONFIGURATION_NAME].let { apiConfiguration ->
apiConfiguration.setExtendsFrom(apiConfiguration.extendsFrom.filter { it.name != "antlr" })
} |
It is now 15 years since antlr2 was last updated, and the Gradle antlr plugin is still adding build tools not just to runtime configurations, but to the 'api' configuration – when even the actual runtime dependency surely belongs in 'implementation' by default. I realise that this may not be high enough priority to change the code, given the risk that people's builds are depending on this behavior, but please at least consider explaining it in the plugin documentation and making the above workaround more discoverable. |
The fix that was tried in #763 is significantly different from the four-part fix in "Possible Fix Options" in the ticket description. Am I right in thinking that that is because the behavior of Maybe the path forward here (without causing any unwarned breaks) is to allow the use of default dependencies to be deprecated for a particular configuration -- i.e., if someone is applying the antlr plugin in 7.x without specifying an |
The ANTLR plugin is modifying the apiConfiguration.extendsFrom(antlrConfiguration); I think the plugin would still work if that line is removed. Attempting to "un-extend" the
It appears that Gradle doesn't support "un-inheritance". Here is my current workaround: dependencies {
antlr(libs.antlr4)
api(libs.antlr4.runtime)
}
val antlrDependencies = HashSet<Dependency>()
configurations.antlr {
allDependencies.forEach {
antlrDependencies.add(it)
}
}
configurations {
api {
antlrDependencies.forEach {
exclude(it.group, it.name)
}
}
} |
Looking deeper, I think my previous "workaround" was insufficient. It also looks possible to "un-extend" configurations with |
@rex-structorum well, that is the work-around since years and working fine here since years. configurations {
api {
setExtendsFrom(extendsFrom.filterNot { it == antlr.get() })
}
} |
In your original post, you excluded it from the Can you run a dependencies report to see if these are still showing up in other configs? And/or confirm that these JARs are not showing up as transitives in a dependent project? In my recollection, through debugging I inferred that other configs were extending |
I got frustrated enough to drop the plugin and use the ANTLR tool cli in a custom gradle convention. That works for me because I'm collecting all the grammars I need into a single module-per-grammar project and publishing a bom for antlr-runtime version compat. |
Because back then it was added to the
They are not, why should they?
Of course, many configurations extend
Of course not. If you still have
Yes and no.
That does not yet resolve it, given you mean a
Iterate through the resolution result would of course trigger resolution and should be done after the fixup. |
Thanks for the detailed clarification. I'm contributing to some confusion by mixing up |
Workaround for gradle/gradle#820. This significantly reduces the size of the shadowJar (14 MB -> 1 MB).
ANTLR has two dependency binaries, one is a smaller runtime dependency and another a larger compile only dependency.
The Gradle antlr plugin documentation says to add the compile only dependency in
antlr
configuration, which is then added to compile config by the plugin. This results in the compile only dependency being listed as a dependency for any project that uses gradle antlr plugin.Expected Behavior
Project JARs built using the Gradle
antlr
plugin should only contain antlr-runtime classes (160 kB)Current Behavior
Projects built using the Gradle
antlr
plugin include an additional 1.4MB of antlr classes that cause other problems explained below.Context
Using antlr as a
compile
dependency is a problem for anyone trying to slim down shipped binaries. It is a slightly bigger problem for android projects that use antlr plugin, because android build will complain about the duplicated class files between the two dependency variants.Use Cases to Consider
We need to support ANTLR2, which doesn't have a separation between runtime and tool. We can't break working builds except when we have made really egregious mistakes.
I think the current ANTLR support has evolved to where it is now, so it's not surprising that things are a bit muddled. I think it makes a lot of sense to treat the ANTLR tool classpath completely separate from the compile classpath.
Possible Fix Options
antlr
configuration as-is, warts and all. Deprecate it.antlrTool
that is completely separate fromcompile
.antlrTool
extends fromantlr
antlrTool
to select the version of ANTLR to use andcompile
to select the version of the ANTLR runtime to use. For ANTLR2, the same dependency would be used for both.The text was updated successfully, but these errors were encountered: