badass-jlink-plugin icon indicating copy to clipboard operation
badass-jlink-plugin copied to clipboard

jlink issues with Log4j 2.18.0 / 2.19.0 / 2.20.0 / 2.21.x

Open CodeDead opened this issue 3 years ago • 6 comments
trafficstars

Opening a new issue because after the fix from this one https://github.com/beryx/badass-jlink-plugin/issues/14

It seems like the problem has returned for log4j 2.19.0

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    forceMerge('log4j-api' )
    ...
}

dependencies {
    implementation 'org.apache.logging.log4j:log4j-core:2.19.0'
}

When running the jlinked binary, you will get an error very similar to:

java.util.ServiceConfigurationError: org.apache.logging.log4j.spi.Provider: module com.codedead.merged.module does not declare `uses`
...
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR StatusLogger Unable to load services for service interface org.apache.logging.log4j.core.util.ContextDataProvider

CodeDead avatar Sep 15 '22 13:09 CodeDead

For anyone facing this issue, I've found a work-around by manually adding the following 'uses' statements to your mergedModule block:

jlink {
...
    mergedModule {
        additive = true
        uses 'org.apache.logging.log4j.util.PropertySource';
        uses 'org.apache.logging.log4j.core.util.ContextDataProvider';
        uses 'org.apache.logging.log4j.core.util.WatchEventService';
        uses 'org.apache.logging.log4j.spi.Provider';
        uses 'org.apache.logging.log4j.message.ThreadDumpMessage.ThreadInfoFactory';
    }
}

CodeDead avatar Sep 18 '22 22:09 CodeDead

I filed same issue months ago here

https://github.com/beryx-gist/badass-jlink-example-log4j2-javafx/issues/5

I also manually added uses clauses but it is not working for all cases.

Hopefully it gets fixed soon.

tbnguyen1407 avatar Sep 20 '22 05:09 tbnguyen1407

I just ran into this same issue, thanks for logging it, and for posting a work-around, it saved me many hours of head-scratching!

vewert avatar Nov 21 '22 16:11 vewert

According to the log4j 2.21.0 release notes they migrated from automatic modules to named modules. This might or might not solve this problem. Note that the module names for the bridge implementations changed.

xzel23 avatar Oct 19 '23 08:10 xzel23

According to the log4j 2.21.0 release notes they migrated from automatic modules to named modules. This might or might not solve this problem. Note that the module names for the bridge implementations changed.

No luck there unfortunately. There's a new error message in 2.21.0: Module org.apache.logging.log4j.core does not read a module that exports javax.annotation.processing

Going to look for a work-around.

CodeDead avatar Oct 23 '23 20:10 CodeDead

As per https://github.com/apache/logging-log4j2/issues/1896 , the solution for log4j2 2.21.0 is to do something like this in your build.gradle file:

jlink {
    forceMerge('log4j-api')

    mergedModule {
        additive = true
        uses 'org.apache.logging.log4j.util.PropertySource'
        uses 'org.apache.logging.log4j.spi.Provider'
        uses 'org.apache.logging.log4j.message.ThreadDumpMessage.ThreadInfoFactory'
    }

You will also need to add the following to your module-info:

    requires java.compiler;
    requires java.naming;
    requires org.apache.logging.log4j;
    requires org.apache.logging.log4j.core;

CodeDead avatar Oct 24 '23 10:10 CodeDead