如何防止Maven项目下载特定依赖项?

use*_*460 3 java eclipse war pom.xml maven

我想使用较旧的 log4J 版本(特别1.27.1是我的项目中的版本)删除所有直接/传递依赖项。

我已确保我的项目pom.xmllib文件夹不包含log4J 1.27.1.

但是,每当项目构建到.war文件中时,我的项目中仍有一些其他库仍在使用log4j 1.27.1,导致log4j-1.27.1.jar被下载到构建中进行部署。

我们有什么方法可以通过阻止 Maven 项目下载特定依赖项来强制它吗?例如,通过在pom.xml文件中添加一些配置?


编辑 1:了解一种方法是在文件中进行排除pom.xml。但这需要我明确提及所有工件。无论如何,有没有像我所说的那样去做"hey, I don't want to see log4j-1.27.1.jar downloaded in my Maven project at all, be it any artifact is depending on it"

Geo*_*met 5

使用enforcer插件禁止所有不安全的log4j版本。任何使用都会使您的构建失败并告诉您它在哪里使用,从而允许您使用安全版本覆盖 log4j 依赖项或完全排除它。

来自 Gunnar Morling 的要点(警告:将此处的 log4j 最低版本更新为没有报告漏洞的 log4j 版本):

<!-- plug-in configuration to put into your parent POM for avoiding any usages of
     outdated log4j2 versions, some of which are subject to the RCE CVE-2021-44228
     ("Log4Shell"), CVE-2021-45046, and CVE-2021-45105. Make sure to check for the
     latest version of log4j2 at
     https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
...
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <id>ban-bad-log4j-versions</id>
      <phase>validate</phase>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <bannedDependencies>
            <excludes>
              <exclude>org.apache.logging.log4j:log4j-core:(,2.17.0)</exclude>
            </excludes>
          </bannedDependencies>
        </rules>
        <fail>true</fail>
      </configuration>
    </execution>
  </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)