cannot get log4j-api-java9

2.3k Views Asked by At

I'm building my project with Java11 and maven my pom look like this and i'm trying to get the right log4j-api for my project

log4j-api is since 2.9 a multi-release jar with support of java9+ and older java versions

In my project i still not able to log4j-api-java9 https://github.com/apache/logging-log4j2/tree/master/log4j-api-java9

my pom looks like this

    ...
    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <vertx.version>3.5.3</vertx.version>
            <log4j.version>2.11.1</log4j.version>
            <async-http-client.version>2.5.2</async-http-client.version>
            <maven.compiler.release>11</maven.compiler.release>
    </properties>
<dependencies>
    <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.version}</version>
    </dependency>
  ...
  ...
<dependencies>

    <build>
       ...
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
                <executions>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>test-compile</phase>
                        <configuration>
                            <testExcludes>
                                <exclude>**/*IT.java</exclude>
                            </testExcludes>
                        </configuration>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
 <plugins>
<build>

using this is also hopeless

<dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api-java9</artifactId>
                <version>${log4j.version}</version>
    </dependency>
2

There are 2 best solutions below

0
On

There doesn't seem to be separate org.apache.logging.log4j:log4j-api-java9 artifact. Instead, Log4j publishes org.apache.logging.log4j:log4j-api as a multi-release JAR (see JEP 238), containing both the old Log4. classes and the Java 9+ Log4j classes. See LOG4J2-2537: Log4j 2 Performance issue with Java 11 for more back-story.

Ideally you want to use an environment that recognizes multi-release JARs. I'm not sure which environment this question originally referenced, but from comments it may have been old plugins that have since been upgraded.

However if you're on a platform such as AWS Lambda which doesn't support multi-release JARs, I've found a workaround by adding the following to your Maven Shade Plugin <configuration> section to copy over the Java 9+ version of the relevant Log4J classes, which apparently don't even look for sun.reflect.Reflection.getCallerClass, thus avoiding the warning related to that class being missing:

<relocations>
  <relocation>
    <pattern>META-INF/versions/9/org/apache/logging/log4j/</pattern>
    <shadedPattern>org/apache/logging/log4j/</shadedPattern>
  </relocation>
</relocations>

See my other answer for more discussion.

However it is much preferred to update your environment, if possible, to support multi-release JARs.

0
On

For me I had to update my maven-plugin-enforcer version from 3.0.0 to 3.0.0-M3, as stated here: https://github.com/jhipster/generator-jhipster/issues/16318#issuecomment-989489090