Skip to content

Log4j pollutes downstream compile classpaths, for example making compilation slower #4034

Open

Description

@Vampire

Description

Log4j since version 2.25 publishes Gradle Module Metadata.

In the Gradle Module Metadata it declares dependencies on 5 libraries that only contain annotations and that are only used to annotate code, not anywhere in the API.

These dependencies should not be exposed in the apiElements variant as that just pollutes the downstream compile classpaths.

Annotations for which no classfile is present are ignored by the JVM.

If someone wants to use those annotations, that someone is responsible to add the annotation to the classpath.

If this was added to work-around the problem that you get a warning (and with -Werror an error) from the Java compiler about missing class files for enum constants used in annotations, this is a bug in the Java compiler (https://bugs.openjdk.org/browse/JDK-8305250) and should imho not be worked-around on the library-level.

As you can for example read at https://mail.openjdk.org/pipermail/jdk-dev/2024-October/009459.html and https://bugs.openjdk.org/browse/JDK-6322301, annotations for which no classfile is present should be ignored by the JVM.

This should be fixed by fixing the bug in javac.
As work-around on the client side, one could stop using -Werror, disable that specific linting option, or add the dependencies in question to the compileOnly configuration.

It is imho not the responsibility of a library to work-around this bug in Javac that is used by downstream projects with special configuration, thereby polluting the compile classpath of all downstream projects even if they don't use that option that makes the warning an error and thus making compilation slower for all downstream projects unless they then manually exclude those dependencies.

Configuration

Version: 2.25+

Activity

  1. Vampire commented on Jan 23, 2026

    @Vampire
    ContributorAuthor

    And even worse, you have this for each and every module, no matter whether it even uses any of those annotations or not.
    log4j-api for example does not have any of those annotations added.
    Yet it needlessly adds those dependencies to downstream compile classpaths without any reason.

    So even if you do not agree that log4j should not work-around this Java compiler bug,
    please at least reconfigure it, so that only those artifacts declare the dependencies that actually have annotations of the respective dependency,
    or even only if they use enum constants from those libraries, as the annotations themselves are handled properly by the Java compiler, even with -Werror.

  2. vy commented on Feb 16, 2026

    @vy
    Member

    @Vampire, I share your frustration. I'm tired of pushing fixes for annotation dependencies to every single release in the last 2 years or so. I hope somebody will eventually publish a Maven plugin that erases the given set of annotations at the post-packaging phase. Any takers? 😉

    So even if you do not agree that log4j should not work-around this Java compiler bug, please at least reconfigure it, so that only those artifacts declare the dependencies that actually have annotations of the respective dependency, or even only if they use enum constants from those libraries, as the annotations themselves are handled properly by the Java compiler, even with -Werror.

    I'd support such a direction. Would you mind contributing this change in a PR, please?

  3. vy commented on Feb 16, 2026

    @vy
    Member

    As one would expect, Log4j is not the only project getting heat on this shortcoming. See junit-team/junit-framework#1065 —tipped by @sormuras.

  4. Vampire commented on Feb 16, 2026

    @Vampire
    ContributorAuthor

    I'm not sure what you mean by "pushing fixes for annotation dependencies to every single release in the last 2 years or so".
    Maven Central artifacts are not mutable afair, are they?
    Also, in releases before 2.25 this was fine as no GMM was published and the POM does not have this.
    It was added in afa773a by a PR from @jjohannes to have these dependencies added by publishing the GMM with them.
    But doing so is imho horribly wrong and just working around a problem in consumers that they have self-made by using -Werror for the Java compiler and thus hitting the JVM bug that is the actual problem here.

    If one does not use -Werror one does not have a problem.
    If one does use -Werror and thus hits the JVM bug mentioned above, then imho where you do the -Werror you should also do the work-around by adding the annotation libraries to the compile classpath and stress the JVM development to fix that bug.

    In my opinion it is the wrong way for a library to add the annotation libraries to consumer compile classpaths to work-around a JVM bug that only some users are hitting due to their compiler settings and at the same time polluting the compile classpath of all consumers needlessly that do not have such a configuration.

    And even if you want to do this imho wrong work-around on log4j side, it should at least be restricted to the actual artifacts that use those annotations and not to all artifacts like it is right now.
    It makes absolutely no sense to have those dependencies for log4j-api for example as that artifact does not use any of those annotation dependencies.

    I'd support such a direction. Would you mind contributing this change in a PR, please?

    Unfortunately, I don't touch Maven with a five-foot pole if I can avoid it, and here this is a Maven build with some plugin that builds the GMM file for publication, and I wouldn't even know how to test this locally, as an install does not even put the GMM to the Maven Local repository (which is probably also a bug but in that plugin I guess).

    Maybe @jjohannes can clean this up to at least only contain this disruptive dependencies on the relevant artifacts that should imho be completely removed, as he also made the PR that added them to each and every artifact if you really prefer to keep this work-around that pollutes the compile classpath of most of your consumers needlessly.

  5. vy commented on Feb 20, 2026

    @vy
    Member

    @Vampire, openjdk/jdk#29818 addressing JDK-8305250 is ready for review. Would you mind verifying that, using an OpenJDK built with this PR, reverting #3450 doesn't cause any problems, and log4j-samples build still passes, please?

  6. Vampire commented on Feb 24, 2026

    @Vampire
    ContributorAuthor

    Even without any change log4j-samples does not build with the JDK build of that PR

    [INFO] --- compiler:3.14.0:testCompile (default-testCompile) @ log4j-nashorn-test ---
    [INFO] Recompiling the module because of changed dependency.
    [INFO] Compiling 2 source files with javac [debug parameters release 17 module-path] to target\test-classes
    An exception has occurred in the compiler (27-internal). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
    java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCCompilationUnit does not have member field 'com.sun.tools.javac.tree.EndPosTable endPositions'
            at com.google.errorprone.JavacErrorDescriptionListener.lambda$provider$3(JavacErrorDescriptionListener.java:169)
            at com.google.errorprone.ErrorProneAnalyzer.finished(ErrorProneAnalyzer.java:210)
            at jdk.compiler/com.sun.tools.javac.api.MultiTaskListener.finished(MultiTaskListener.java:133)
            at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1426)
            at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1373)
            at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:959)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:106)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:154)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:102)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:96)
            at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
            at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:214)
            at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1226)
            at org.apache.maven.plugin.compiler.TestCompilerMojo.execute(TestCompilerMojo.java:208)
            at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:126)
            at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:328)
            at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:316)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:174)
            at org.apache.maven.lifecycle.internal.MojoExecutor.access$000(MojoExecutor.java:75)
            at org.apache.maven.lifecycle.internal.MojoExecutor$1.run(MojoExecutor.java:162)
            at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute(DefaultMojosExecutionStrategy.java:39)
            at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:159)
            at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:105)
            at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:73)
            at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:53)
            at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:118)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:261)
            at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:173)
            at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:101)
            at org.apache.maven.cli.MavenCli.execute(MavenCli.java:906)
            at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:283)
            at org.apache.maven.cli.MavenCli.main(MavenCli.java:206)
            at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
            at java.base/java.lang.reflect.Method.invoke(Method.java:565)
            at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:255)
            at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:201)
            at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:361)
            at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:314)
    [INFO] -------------------------------------------------------------
    [WARNING] COMPILATION WARNING :
    [INFO] -------------------------------------------------------------
    [WARNING] No processor claimed any of these annotations: org.junit.jupiter.api/org.junit.jupiter.api.Timeout,org.junit.jupiter.api/org.junit.jupiter.api.Test
    [WARNING] The following options were not recognized by any processor: '[log4j.graalvm.artifactId, log4j.graalvm.groupId]'
    [INFO] 2 warnings
    [INFO] -------------------------------------------------------------
    

    But I tested with the file from the MCVE of the PR: https://github.com/jjohannes/gradle-demos/blob/main/log4j-metadata/src/main/java/org/example/App.java with 2.24.3 and there is no problem:

    $ jdk-17.0.15/bin/javac -Xlint:all -Werror -classpath log4j-api-2.24.3.jar App.java && echo SUCCESS || echo FAILURE
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/message/ParameterizedMessage.class): warning: Cannot find annotation method 'replacement()' in type 'InlineMe': class file for com.google.errorprone.annotations.InlineMe not found
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/message/ParameterizedMessage.class): warning: Cannot find annotation method 'imports()' in type 'InlineMe'
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/status/StatusData.class): warning: Cannot find annotation method 'value()' in type 'SuppressFBWarnings': class file for edu.umd.cs.findbugs.annotations.SuppressFBWarnings not found
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/status/StatusData.class): warning: Cannot find annotation method 'justification()' in type 'SuppressFBWarnings'
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/SystemPropertiesPropertySource.class): warning: Cannot find annotation method 'value()' in type 'ServiceProvider': class file for aQute.bnd.annotation.spi.ServiceProvider not found
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/SystemPropertiesPropertySource.class): warning: Cannot find annotation method 'resolution()' in type 'ServiceProvider'
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/Activator.class): warning: Cannot find annotation method 'value()' in type 'Headers': class file for org.osgi.annotation.bundle.Headers not found
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/Activator.class): warning: Cannot find annotation method 'name()' in type 'Header': class file for org.osgi.annotation.bundle.Header not found
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/Activator.class): warning: Cannot find annotation method 'value()' in type 'Header'
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/Activator.class): warning: Cannot find annotation method 'name()' in type 'Header'
    log4j-api-2.24.3.jar(/org/apache/logging/log4j/util/Activator.class): warning: Cannot find annotation method 'value()' in type 'Header'
    error: warnings found and -Werror specified
    1 error
    11 warnings
    FAILURE
    $ jdk-27_pr29818/bin/javac -Xlint:all -Werror -classpath log4j-api-2.24.3.jar App.java && echo SUCCESS || echo FAILURE
    SUCCESS
    $
    

    So it seems they indeed finally clean up and fix that mess. :-)

  7. self-assigned this
    on Mar 16, 2026
  8. ppkarwasz commented on Mar 18, 2026

    @ppkarwasz
    Member

    Hi @Vampire,

    We didn't add the annotation libraries to the compileOnlyApi configuration, due to JDK-8305250, but due to a different compiler behavior: the linter warns users about missing annotation classes, even if those classes are not even loaded by the runtime (have a retention of CLASS). See #3110 (comment) for details.

    I filed this behavior as JDK-8342833, but the OpenJDK team classified it as intended behavior.

    Due to our large variety of users, we often need to make compromises on the behavior of our releases. Having a couple more libraries at compile time beats breaking builds for users the need to use -Werror by policy.

  9. Vampire commented on Mar 18, 2026

    @Vampire
    ContributorAuthor

    Having a couple more libraries at compile time beats breaking builds for users the need to use -Werror by policy.

    @ppkarwasz I understand, but I - personally - still do not agree.
    I seldomly have seen -Werror in use with Java, so I'd say it is the minority of the users.
    So you make build for the majority of your users slower just to work-around a Java Compiler bug for some of your users that are just hit by the bug due to their individual configuration and imho should apply the work-around of adding those libraries to their compile classpaths.

    But anyway.

    but due to a different compiler behavior
    [...]
    I filed this behavior as JDK-8342833, but the OpenJDK team classified it as intended behavior.

    That is the same issue, or at least the same root cause and also fixed by the same PR.

    If in your JDK issue (I didn't read all comments) they say it is "not an issue" and "intended behavior", this is simply a mis-evaluation of the one writing so.

    $ cat App.java
    public class App {
        org.apache.logging.log4j.Level l;
    
        public static void main(String[] args) {
            System.out.println("Hello from: " + App.class.getModule().getName());
        }
    }
    
    $ eclipse_adoptium-25-amd64-windows.2/bin/javac -J-Duser.language=en -Xlint:all -Werror -classpath log4j-api-2.24.1.jar App.java && echo SUCCESS || echo FAILURE
    log4j-api-2.24.1.jar(/org/apache/logging/log4j/Level.class): warning: [classfile] Cannot find annotation method 'value()' in type 'BaselineIgnore': class file for aQute.bnd.annotation.baseline.BaselineIgnore not found
    error: warnings found and -Werror specified
    1 error
    1 warning
    FAILURE
    
    $ /d/Programme/Java/jdk-27_pr29818/bin/javac -J-Duser.language=en -Xlint:all -Werror -classpath log4j-api-2.24.1.jar App.java && echo SUCCESS || echo FAILURE
    SUCCESS

    So also that issue you linked to is just the same problem.
    Misbehavior of javac that is fixed with that PR.

    Besides that in the comment you linked to you wrote that you will not add those dependencies, but you did now. :-)

    And even if that were not the case, those dependencies should only be added to the artifacts actually affected, not each and every artifact coming from log4j, so that you at least not pollute the downstream compile classpath completely without reason. :-)


    There could be one issue still, if you compile against annotation v2 that has the value in the annotation and a downstream consumer adds annotation v1 to the compile classpath that does not have the value yet, then you get the same warning.

    For that it would probably then be preferable to add a constraint for that annotation library to the log4j-bom,
    so that consumers applying the BOM will get at least annotation v2 without you having a dependency on it, just in case they bring v1 to the compile classpath another way.

    But even then and even with you declaring those dependencies, it could also be the other way around.
    You compile against annotaiton v1 which has the value in the annotation.
    And then a downstream consumer adds annotation v2 to the compile classpath where the value was removed.
    Even with your dependency then v2 would win (at least in Gradle) due to conflict resolution and thus again you get the "cannot find value warning".

    So with that PR fix, it is just the same version conflict / breaking changes problem we always have when composing different dependencies and their transitives.

  10. ppkarwasz commented on Mar 19, 2026

    @ppkarwasz
    Member

    Having a couple more libraries at compile time beats breaking builds for users the need to use -Werror by policy.

    @ppkarwasz I understand, but I - personally - still do not agree. I seldomly have seen -Werror in use with Java, so I'd say it is the minority of the users. So you make build for the majority of your users slower just to work-around a Java Compiler bug for some of your users that are just hit by the bug due to their individual configuration and imho should apply the work-around of adding those libraries to their compile classpaths.

    Can you quantify the performance difference in the compilation? Is it imputable to the additional download time of the dependencies or do you see a significant performance difference during the compilation?

    I agree with you that only a minority of users compiles with -Werror, but that minority has reached out multiple times asking to fix it on our end. There are company policies that developers can not easily overcome (even if they don't make sense), for example Spring Boot couldn't upgrade to version 2.25.1 due to a log event of severity ERROR being generated during the build (#3779).

    In comparison, a compilation slowdown is a barely noticeable problem.

    That is the same issue, or at least the same root cause and also fixed by the same PR.

    You are right, I didn't read the PR in detail.

    Besides that in the comment you linked to you wrote that you will not add those dependencies, but you did now. :-)

    We didn't add them to the runtime classpath, which is, in my opinion, the most relevant.

  11. added
    bug
    build
    dependencies
    priority: P2
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugbuilddependenciespriority: P2

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions