关于 maven 的子 pom 为何无法继承父 pom 的依赖?

2019-11-22 17:55:06 +08:00
 different

父 pom 中写了 ............

	<dependency>
        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

............

子 pom ............ <parent></parent>

    <artifactId>aispringcloud</artifactId>
    
    <groupId>com.southwind</groupId>
    
    <version>1.0-SNAPSHOT</version>
    
</parent>

............

继承父的 pom

然后运行子项目的时候,必须重新在子的 pom 写,否者报错。

	<dependency>
        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

这是为什么?

我是照着视频打的代码,子工程是一个 zipkin,然后视频作者这么解析的: Spring Cloud 不同版本中具体操作有一些区别,很多组件之间是不兼容的,基于这个版本的 zipkin,需要单独引入.

难道从父 pom 继承下来的和单独引入的不是同一个吗?

求指点...

8685 次点击
所在节点    Java
30 条回复
different
2019-11-22 22:41:58 +08:00
@wolfie 不好意思,刚刚有事去忙了,现在执行了 mvn install 之后依旧不行。
删掉子 pom 的 web 依赖之后,报错。

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Exception in thread "main" java.lang.StackOverflowError

at java.base/java.util.stream.Sink$ChainedReference.cancellationRequested(Sink.java:263)

at java.base/java.util.stream.Sink$ChainedReference.cancellationRequested(Sink.java:263)


@Aruforce

我也是看着视频写的代码,实际上我并不知道何种原因导致错误,,只是感觉,既然父 pom 有了 dependencies 包含了 web 包,子 pom 就可以不需要 web 包了。 [查了资料的确如此,但是在运行的时候,却必须要在子 pom 再依赖一次 web 包]
执行了,mvn help:effective-pom 可以看到一堆依赖,但是根据报错信息,应该是和 log 包有关,但是无论我加不加 web 依赖,只能搜索到和日志相关的这个包:
<dependency>

<groupId>org.springframework.integration</groupId>

<artifactId>spring-integration-syslog</artifactId>

<version>5.1.2.RELEASE</version>

</dependency>
Xbluer
2019-11-22 22:44:39 +08:00
@Aruforce #19 排除的标签 exclusions

Spring Boot 中使用 Log4j2 的时候一般需要用到

<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>
different
2019-11-22 22:45:02 +08:00
父 pom
```
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.southwind</groupId>
<artifactId>aispringcloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eurekaserver</module>
<module>eurekaclient</module>
<module>resttemplate</module>
<module>consumer</module>
<module>zuul</module>
<module>ribbon</module>
<module>feign</module>
<module>hystrix</module>
<module>nativeconfigserver</module>
<module>nativeconfigclient</module>
<module>configserver</module>
<module>configclient</module>
<module>zipkin</module>
<module>zipkinclient</module>
</modules>
<!-- 这是 Spring Boot 的父级依赖,这样当前的项目就是 Spring Boot 项目了。-->
<!-- spring-boot-starter-parent 是一个特殊的 starter,它用来提供相关的 Maven 默认依赖。-->
<!-- 使用它之后,常用的包依赖可以省去 version 标签。(在 dependencies 里面)-->
<!-- https://blog.csdn.net/niceyoo/article/details/90731133-->
<!-- 在 dependencies 里的部分配置可以不用填写 version 信息,-->
<!-- 这些 version 信息会从 spring-boot-dependencies 里得到继承。-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<!-- https://blog.csdn.net/hanglife/article/details/90035083-->
<!-- dependencies 和 dependencyManagement 的区别概述-->

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- spring-boot-starter-web 会自动嵌入 tomcat 容器。-->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 解决 jdk9 以上没有 JAXB API 的问题 (引入下面 4 个包) -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- <version>1.18.6</version>-->
<optional>true</optional>
<!-- https://blog.csdn.net/blueheart20/article/details/81014116-->
<!-- <scope>provided</scope>-->
</dependency>
</dependencies>

<!-- 锁定版本,并不会真的引入 jar 包 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<!-- import 范围只有在 denpendencyManagement 元素下才有效果-->
<!-- https://blog.csdn.net/lqzxpp/article/details/79640638-->
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

```










子 pom
```
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>aispringcloud</artifactId>
<groupId>com.southwind</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>zipkin</artifactId>

<dependencies>
<!-- 父工程不是已经有了吗?为什么还要引入一次?-->
<dependency>
<groupId>org.springframework.boot</groupId>
<!-- spring-boot-starter-web 会自动嵌入 tomcat 容器。-->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.12.3</version>

</dependency>
<!--图形界面展示-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.12.3</version>
</dependency>


</dependencies>
</project>

```
different
2019-11-22 22:46:01 +08:00
奇怪了,这 markdown 为何没高亮,,,,姿势不对吗
different
2019-11-22 22:48:37 +08:00
@Xbluer exclusions 可以解决包冲突问题吧?但是也无法解析为何子 pom 仍然需要重新引入一次 web 包吧?
Aruforce
2019-11-23 10:55:50 +08:00
@Xbluer 我说的不是这个…是从父 pom dependencies 的依赖被强制的引入到子工程的那些 dependency 没有办法排除…
pan176
2019-11-23 20:33:24 +08:00
子 pom 会继承父 pom。
你尝试把 .idea 文件夹删掉,重新导入 pom,我就是用这种方法解决怪异现象的。
different
2019-11-23 21:03:23 +08:00
@pan176 尴尬,依旧不行
xb7420
2020-04-20 09:54:54 +08:00
我也遇到了,最终使用 idea 菜单下 File->invalidate caches / restrat ... 解决
different
2020-04-20 10:33:57 +08:00
@xb7420 后来我查了一下依赖,发现只要父 pom 和子 pom 都不单独引入 web 包就不会出现报错。总的原因导致应该是 log4j-slf4j-impl 先于 logback-classic 加载而报错。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/622213

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX