【问题标题】:How to prevent Maven project from downloading specific dependencies?如何防止 Maven 项目下载特定的依赖项?
【发布时间】:2022-01-17 11:01:33
【问题描述】:

我想从我的项目中删除所有使用旧版 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"一样?

【问题讨论】:

标签: java eclipse maven pom.xml war


【解决方案1】:

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

来自 Gunnar Morling 的要点(警告:在此处更新 log4j 最低版本to the log4j version with no vulnerabilities reported):

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

【讨论】:

    猜你喜欢
    • 2015-03-14
    • 2020-06-16
    • 2020-07-04
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多