关于 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 继承下来的和单独引入的不是同一个吗?

求指点...

8674 次点击
所在节点    Java
30 条回复
Leonard
2019-11-22 18:01:27 +08:00
看成子 porn 和父 porn 了
mepine
2019-11-22 18:04:52 +08:00
@Leonard 同 😂
snappyone
2019-11-22 18:06:31 +08:00
用 dependencymgmt 了吗
wolfie
2019-11-22 18:12:39 +08:00
父项目 mvn install
nthin0
2019-11-22 18:18:11 +08:00
父 pom 是不是写在 dependencyManagement 里了
different
2019-11-22 18:55:31 +08:00
@nthin0
@snappyone
写在 Dependencies
different
2019-11-22 18:56:02 +08:00
@wolfie 我试试
732870147
2019-11-22 18:58:30 +08:00
下班,散光眼,Porn!?
snappyone
2019-11-22 19:10:54 +08:00
@different Dependencies 不行,参照 5 楼用 dependencyManagement
different
2019-11-22 19:30:08 +08:00
@snappyone 那父工程目录下的 dependencies 有啥用?父工程下又没有直接的代码,只有子项目
Aruforce
2019-11-22 20:02:46 +08:00
如果我理解正确的话…
父 pom 只是用来管理统一版本…dependencies 是不会向下传递的… pom 的依赖传递是向上的…
wolfie
2019-11-22 20:03:25 +08:00
找不到父项目依赖。
先找本地仓库,在找服务端仓库。
mvn install 到本地你的子项目才会找到父项目。

另外 `dependencies` 是对的,`dependencyManagement` 中的依赖子项目不会默认继承。
Aruforce
2019-11-22 20:09:55 +08:00
@wolfie dependencyManagement 是用来限定依赖版本…子工程可以重新指定版本… dependencies 应该不会向下传递…向下传递不合理…
wolfie
2019-11-22 20:13:40 +08:00
@Aruforce #13
> 这就体现了 dependencyManagement 和 dependencies 的一些区别,如果在父类中定义了 dependencies, [子类继承的时候回自动产生依赖] ,而 dependencyManagement 中的东西就像是依赖的声明,子模块想要继承还需要在自己的 pom 文件中进行添加。

随便搜的
写在父项目的好处是就是不用重复依赖。
Aruforce
2019-11-22 20:14:02 +08:00
Lz 你可以在 子工程里面执行。mvn help:effective-pom。看下 如果有 Jar 不是你想要的版本…可以 父 pom dependency management 里面 exclusion 掉 指定你要的版本…
hantsy
2019-11-22 20:14:06 +08:00
dependencies 会继承,慎用。如果子 POM 恰好不需要某依赖,但 parent dependencies 声明了,会造成污染。如果你有对代码洁癖的习惯,还是不要用它继承。

dependencyManagement 不会继承。一般用于 Parent POM,主要用来管理整个项目树型结构的依赖版本,使保持一致性,防止不同版本的依赖在打包时冲突。最好的方法是 Parent POM 用它声明,Child Pom 在 dependencies 中加入相应依赖,不需要再声明 Version。
Aruforce
2019-11-22 20:17:21 +08:00
@wolfie 我是只用父 pom 限定 jar 版本 …没在里面加过 dependencies…依赖向下传递有点违反我的直觉… 我回去了试下…能不能向下传递吧…
liangkang1436
2019-11-22 21:42:04 +08:00
https://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven

I'm fashionably late to this question, but I think it's worth a clearer response than the accepted one (which is correct, but doesn't emphasize the actual important part, which you need to deduce yourself).
In the parent POM, the main difference between the <dependencies> and <dependencyManagement> is this:
Artifacts specified in the <dependencies> section will ALWAYS be included as a dependency of the child module(s).
Artifacts specified in the <dependencyManagement> section, will only be included in the child module if they were also specified in the <dependencies> section of the child module itself. Why is it good you ask? because you specify the version and/or scope in the parent, and you can leave them out when specifying the dependencies in the child POM. This can help you use unified versions for dependencies for child modules, without specifying the version in each child module.
Aruforce
2019-11-22 22:13:10 +08:00
@Aruforce @wolfie
奇怪了 真的可以 啊....
子工程不想使用父工程某些依赖怎么排除掉 ?
按照 @liangkang1436 的说明 似乎是无法排除?让人不爽...
我还是不这么干了...
gtexpanse
2019-11-22 22:19:48 +08:00
尽量不要在 parent pom 里写<dependencies>,除非你真的确定所有 child 项目都*必须*有这个依赖

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

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

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

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

© 2021 V2EX