【问题标题】:Maven transitive dependency not loaded未加载 Maven 传递依赖项
【发布时间】:2020-03-01 01:35:18
【问题描述】:

问题:当我在另一个应用程序中导入自定义日志库的 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"

my-logger的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>

my-app的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>

【问题讨论】:

  • Maven 自动解析传递依赖,除非您更改范围或将其标记为可选。您能否也将 POM 添加到您的问题中?
  • (我已经添加了 POM)

标签: maven intellij-idea dependencies slf4j


【解决方案1】:

我猜spring-boot-starter-parentlog4j-slf4j-impl 的范围更改为provided。您可以通过查看mvn dependency:tree 来了解这一点,您将在其中找到log4j-slf4j-impl 的结果范围和版本。

【讨论】:

  • 运行mvn dependency:tree后,log4j-slf4j-impl不在树中
  • 这很奇怪。在父 POM 中查找包含 log4j-slf4j-impl 的 sn-ps。
【解决方案2】:

当我对 nexus 进行 maven 部署(参见 https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html)时,我使用了选项 generatePom=true 并且生成的 .pom 文件不包含传递依赖项。 当我添加选项 pomFile=pom.xml 时,传递依赖项被添加到生成的 .pom 文件中并且它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-17
    • 2014-08-10
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多