安装 java 11 后,java 9 的 Maven 构建失败

maven build fails for java 9 when java 11 is installed

我正在尝试从源代码构建 log4j 包:

$ 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]

这有点奇怪,因为我的 java 版本是 11,我缺少什么?

$ 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)

直到最近,log4j-core 的构建过程还依赖于配套 log4j-core-java9 模块的构建过程。

这种多模块构建方法是在 this issue and the changes performed are described in this commit 中引入的。

在库的未来版本中,这些依赖项将按照 this issue 中的描述删除,正是为了避免潜在的依赖项问题。该问题提供了有关它是否正在发生的更多信息:

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.

如果您需要从源代码构建旧版本的代码,根据 aforementioned commit 中引入的更改,您首先需要组装 log4j-core-java9 然后在构建中使用依赖项log4j-core 本身的过程。

简而言之,请尝试以下操作。

首先,log4j2 在其构建过程中使用 toolchains,因此对于 JDK 11,您可能需要在 ~/.m2/toolchains.xml 文件中提供一个条目,如下所示:

<?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>

您可以在存储库中找到不同的工具链配置示例。

现在,运行 从您解压库的目录中执行以下命令。

安装log4j2-api-java9:

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

安装log4j2-api:

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

安装log4j-core-java9:

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

最后,包log4j-core:

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

请注意,此构建过程可能会生成大量工件,这些工件将安装在您的 Maven 本地存储库中,并且可能与从 Maven 远程存储库下载的库版本发生冲突。考虑为此过程使用临时 docker 容器,例如:

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

和运行里面有这些不同的命令。