简体   繁体   中英

Logging dependencies still included in lib folder despite exlusion being configured in spring-boot-maven-plugin

EDIT: It turns out I am just bad at looking for stuff. The war before the changes did not have these libraries, and the original.war (before repackage) already contains them. So the issue lies somewhere else, spring-boot-maven-plugin has nothing to do with it. Still don't know where they are coming from since I've also tried simply deleting the dependency whereever I find it, but oh well, see my first sentence.

I am working on making my firms software run as a spring boot application. Since our war may be deployed in various different environments like the SAP Cloud Platform, logging libraries should not be included in the lib folder to prevent conflicts. However, some logging libraries (specifically jul-to-slf4j, log4j-api and log4j-to-self4j) are always in my lib folder no matter how specific my exclusions get. Other libraries (two of ours that are needed for tests or have to be included in the classes file) are excluded properly.

I have tried setting the tag to the specific libraries as well as to just exclude the whole group. After this, I tried to simply exclude the dependencies themselves, but they still somehow show up after mvn dependency:tree tells me they are no longer present.

This is the plugin configuration:

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

I expect the war that I build to no longer include these logging libraries in my WEB-INF/lib folder. Instead, they keep being included.

Logging frameworks such as slf4j or log4j2 have an API component and a run-time component. Your code should depend only on the API component. It is the run-time component that you should avoid specifying when you deploy to a particular environment as that environment may have its own version of that run-time component. In your example, libraries such as log4j-to-slf4j, log4j-api are API components. It is ok for them to be present in the WAR. However you should ensure that your environment is providing a compatible runtime logging component.

See the explanation for 'provided' scope here: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

Try this... Including and Excluding Files From the WAR

It is possible to include or exclude certain files from the WAR file, by using the <packagingIncludes> and <packagingExcludes> configuration parameters.

So you'll essentially just specify WEB-INF/lib/log4j- .jar, WEB-INF/lib/jul- .jar within these tags to exclude all packages starting with log4j and 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM