【问题标题】:Logging dependencies still included in lib folder despite exlusion being configured in spring-boot-maven-plugin尽管在 spring-boot-maven-plugin 中配置了排除,但日志依赖项仍包含在 lib 文件夹中
【发布时间】:2019-09-20 09:44:34
【问题描述】:

编辑:原来我只是不擅长找东西。更改之前的战争没有这些库,并且原始 .war(重新打包之前)已经包含它们。所以问题出在其他地方,spring-boot-maven-plugin 与它无关。仍然不知道它们来自哪里,因为我也尝试过简单地删除我找到的依赖项,但是哦,好吧,看我的第一句话。

我正在努力让我的公司软件作为 Spring Boot 应用程序运行。由于我们的战争可能部署在各种不同的环境中,例如 SAP Cloud Platform,因此日志库不应包含在 lib 文件夹中以防止冲突。但是,无论我的排除有多么具体,一些日志库(特别是 jul-to-slf4j、log4j-api 和 log4j-to-self4j)总是在我的 lib 文件夹中。其他库(测试所需或必须包含在类文件中的两个库)被正确排除。

我已经尝试将标签设置为特定的库以及排除整个组。在此之后,我尝试简单地排除依赖项本身,但是在 mvn dependency:tree 告诉我它们不再存在之后它们仍然以某种方式出现。

这是插件配置:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>
                    repackage
                </goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>de.firm.integration.BaseSpringConfiguration</mainClass>
        <excludes>
            <exclude>
                <groupId>de.firm.integration</groupId>
                <artifactId>eis-generator-odata-api</artifactId>
            </exclude>
            <exclude>
                <groupId>de.firm.integration</groupId>
                <artifactId>eis-admin-ui</artifactId>
            </exclude>
            <exclude>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
            </exclude>
            <exclude>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </exclude>
            <exclude>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-jul</artifactId>
            </exclude>
            <exclude>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
            </exclude>
            <exclude>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-to-slf4j</artifactId>
            </exclude>
        </excludes>
    </configuration>
</plugin>

我希望我构建的战争不再将这些日志库包含在我的 WEB-INF/lib 文件夹中。相反,它们一直被包括在内。

【问题讨论】:

    标签: java spring spring-boot log4j slf4j


    【解决方案1】:

    试试这个...Including and Excluding Files From the WAR

    通过使用 配置参数,可以在 WAR 文件中包含或排除某些文件。

    所以您基本上只需指定 WEB-INF/lib/log4j-.jar、WEB-INF/lib/jul-.jar 在这些标签中排除所有以 log4j 和 jul 开头的包。

    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <packagingExcludes>
           WEB-INF/lib/log4j-*.jar, WEB-INF/lib/jul-*.jar
          </packagingExcludes>
        </configuration>
      </plugin>
    </plugins>
    

    【讨论】:

    • 不幸的是,这不起作用,因为在“重新打包”目标期间,spring 插件在 maven war 插件之后运行。因此,当 maven war 插件运行时,依赖项将不存在。
    • 好的,最初插件执行顺序是通过使用构建生命周期/阶段来控制的。问题是当您在同一阶段有两个或多个插件时。从 Maven 3.0.3 开始,绑定到同一阶段的插件应该按照它们列出的顺序执行。
    • 嗨,好像不是。我们最终完全删除了依赖项。我们考虑过使用 PackagingExcludes,但它似乎是一种解决方法,它依赖于可能会再次改变的东西。
    【解决方案2】:

    slf4j 或 log4j2 等日志框架具有 API 组件和运行时组件。您的代码应仅依赖于 API 组件。当您部署到特定环境时,您应该避免指定运行时组件,因为该环境可能有自己的运行时组件版本。在您的示例中,诸如 log4j-to-slf4j 、 log4j-api 之类的库是 API 组件。他们可以出现在 WAR 中。但是,您应该确保您的环境提供了兼容的运行时日志记录组件。

    在此处查看“提供”范围的说明:https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2013-11-04
      • 2012-04-22
      • 2018-07-16
      相关资源
      最近更新 更多