Home > Software engineering >  maven build fails for java 9 when java 11 is installed
maven build fails for java 9 when java 11 is installed

Time:12-24

I'm trying to build the log4j package from source:

$ wget https://github.com/apache/logging-log4j2/archive/refs/tags/rel/2.14.1.tar.gz
$ tar -xf 2.14.1.tar.gz
$ cd logging-log4j2-rel-2.14.1/log4j-core
$ /apache-maven-3.8.4/bin/mvn compile 1>./temp.txt
$ grep "\[ERROR\] Failed.*java9.*" ./temp.txt | sed "s/: org/:\norg/g"
[ERROR] Failed to execute goal on project log4j-core: Could not resolve dependencies for project org.apache.logging.log4j:log4j-core:jar:2.14.1:
org.apache.logging.log4j:log4j-core-java9:zip:2.14.1 was not found in https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

This is a bit weird because my java version is 11, what am I missing?

$ java -version
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13 8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13 8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

CodePudding user response:

Until recently, the build process of log4j-core depends on the build process of a companion log4j-core-java9 module.

This multi-module build approach was introduced in this issue and the changes performed are described in this commit.

In a future version of the library these dependencies will be removed as described in this issue precisely to avoid potential dependency problems. The issue provides more information about was it going on:

Log4j API and Log4j Core have to build the Java 9 classes in one Maven module and then copy them into the "normal" module. The Java 9 projects are not deployed during a release. The pom.xml files were specifying both as provided dependencies. When the enforcer plugin sees this it is trying to resolve them - sometimes - and fails. It seems these don't need to be declared as dependencies because they are configured into the dependency plugin. So the dependencies should be removed from the pom files.

If you need to build the code from an older version from source, according to the changes introduced in the aforementioned commit, you first need to assembly log4j-core-java9 and then use the dependency in the build process of log4j-core itself.

In a nutshell, try the following.

First, log4j2 uses toolchains in its build process, so for JDK 11 you may need to provide an entry in ~/.m2/toolchains.xml file like the following:

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
  <!-- other definitions -->
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>11</version>
    </provides>
    <configuration>
      <jdkHome>path/to/the/jdk (/opt/java/openjdk, for instance)</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

You can find different toolchain configuration examples in the repository library.

Now, run the following commands from the directory in which you unpacked the library.

Install log4j2-api-java9:

cd log4j2-api-java9
../mvnw clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true

Install log4j2-api:

cd ../log4j2-api
../mvnw clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true

Install log4j-core-java9:

cd ../log4j-core-java9/
../mvnw clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true

Finally, package log4j-core:

cd ../log4j-core
../mvnw clean package -Dmaven.test.skip=true -Dmaven.javadoc.skip=true

Please, be aware that this build process could generate a lot of artifacts that will be installed in your Maven local repository, and that may conflict with versions of the library downloaded from the Maven remote repository. Consider using for this process a temporary docker container, for example:

docker run -it --entrypoint /bin/bash adoptopenjdk/openjdk11:latest

and run these different commands within it.

  • Related