未加载Maven传递依赖项

问题:当我将自定义日志记录库的jar导入另一个应用程序时,我希望该自定义库包含对 slf4j 的依赖关系在应用程序中的这种依赖性。

我有一个Maven项目 my-logger ,该项目依赖于 log4j-slf4j-impl pom.xml 我的记录器

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.12.1</version>
</dependency>

在我的Spring Boot项目 my-app 中,我导入 my-logger

<dependency>
  <groupId>org.test</groupId>
  <artifactId>my-logger</artifactId>
  <version>100</version>
</dependency>

但是在IntelliJ中,我在 org.test:my-logger:100

下看不到任何依赖关系

我必须在 my-app pom.xml中添加 log4j-slf4j-impl 依赖项,否则我将获得

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"

我的记录器的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.test</groupId>
    <artifactId>my-logger</artifactId>
    <version>100</version>
    <packaging>jar</packaging>
    <name>my-logger</name>
    <description>Logging app</description>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.1</version>
        </dependency>

    </dependencies>
</project>

我的应用的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>0.0.1-snAPSHOT</version>
    <packaging>jar</packaging>
    <name>my-app</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

         <dependency>
             <groupId>org.test</groupId>
             <artifactId>my-logger</artifactId>
             <version>100</version>
         </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
wsyxdl 回答:未加载Maven传递依赖项

我猜想spring-boot-starter-parent会将log4j-slf4j-impl的范围更改为provided之类的东西。您可以通过查看mvn dependency:tree来解决这一问题,从中可以找到log4j-slf4j-impl的结果范围和版本。

,

当我进行Maven部署(请参阅https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html)时,我正在使用选项generatePom=true,并且生成的.pom文件不包含传递依赖项。 当我添加选项pomFile=pom.xml时,传递依赖项已添加到生成的.pom文件中,并且可以正常工作。

本文链接:https://www.f2er.com/3164824.html

大家都在问