如何在使用Maven更新子模块依赖后,更新父模块的依赖。

huangapple go评论78阅读模式
英文:

How to update parent dependency after i update children denpendency using maven

问题

我使用IDEA,我的Spring Boot应用程序包含多个模块,如下所示:

parent
 -web
 -service
 -dao

service模块的pom.xml中有一个名为common:1.2.0-SNAPSHOT的jar依赖项。我将其版本更新为1.3.0-SNAPSHOT,然后执行了Maven重新导入并运行了mvn clean install -U命令,但是这两个版本仍然存在于依赖列表中。在依赖树中,也存在两个jar依赖项。此外,当我运行应用程序时,它抛出了一个NoClassDefFoundError错误,而我已经在1.3.0-SNAPSHOT中添加了该类。

这是我的Maven依赖树:

service:jar
+- dao:jar
\- common:jar:1.3.0-SNAPSHOT

web:jar
+- service:jar
  +- dao:jar
  \- common:jar:1.2.0-SNAPSHOT
\- others:jar

service模块的pom.xml中的依赖项配置如下:

<dependency>
   <groupId>com</groupId>
   <artifactId>common</artifactId>
   <version>1.3.0-SNAPSHOT</version>
</dependency>

我该如何更新这个依赖项?

英文:

I use IDEA,My springboot app contains multiple modules like that:

parent
 -web
 -service
 -dao

There is a jar dependency common:1.2.0-SNAPSHOT in service module defined in pom.xml, I updated its version to 1.3.0-SNAPSHOT, after that i do maven reimport and run mvn clean install -U but these two versions still in dependencies list, and in dependency tree, two jar in the dependencies, also when i run the app, it throw NoClassDefFoundError error which i added in 1.3.0-SNAPSHOT

This is my maven dependenct tree:

service:jar
+- dao:jar
\- common:jar:1.3.0-SNAPSHOT

web:jar
+- service:jar
  +- dao:jar
  \- common:jar:1.2.0-SNAPSHOT
\- others:jar

service pom dependency:

&lt;dependency&gt;
   &lt;groupId&gt;com&lt;/groupId&gt;
   &lt;artifactId&gt;common&lt;/artifactId&gt;
   &lt;version&gt;1.3.0-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;

How can i update the dependency?

答案1

得分: 0

如@Haroon @chrylis所说,我检查了父级pom.xml,并在此pom中找到配置了&lt;parent&gt;

    &lt;parent&gt;
        &lt;groupId&gt;insight.plat&lt;/groupId&gt;
        &lt;artifactId&gt;insight-bom-base&lt;/artifactId&gt;
        &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;

在pom insight-bom-base中,配置了common 1.3.0-SNAPSHOT,所以Web模块依赖于此版本而不是1.2.0-SNAPSHOT版本。

&lt;dependencyManagement&gt;
&lt;dependency&gt;
        &lt;groupId&gt;com&lt;/groupId&gt;
        &lt;artifactId&gt;common&lt;/artifactId&gt;
        &lt;version&gt;1.2.0-SNAPSHOT&lt;/version&gt;
&lt;/dependencyManagement&gt;

正如Maven文档中的传递性依赖所示,

依赖管理 - 这允许项目作者在遇到传递性依赖或在未指定版本的依赖项中直接指定要使用的工件版本。
在前面一节的示例中,即使A不直接使用D,也将D直接添加到了A中。
相反,A可以将D作为依赖项包含在其dependencyManagement部分中,并在引用D时直接控制使用哪个版本,如果需要的话。

父级dependencyManagement的优先级高于传递性依赖,因此在服务模块中,它使用common 1.2.0-SNAPSHOT

英文:

As @Haroon @chrylis said, i checked the parent pom.xml and found &lt;parent&gt; configured in this pom.

    &lt;parent&gt;
        &lt;groupId&gt;insight.plat&lt;/groupId&gt;
        &lt;artifactId&gt;insight-bom-base&lt;/artifactId&gt;
        &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;

in pom insight-bom-base, common 1.3.0-SNAPSHOT is configured, so web module depend this version not version 1.2.0-SNAPSHOT.

&lt;dependencyManagement&gt;
&lt;dependency&gt;
        &lt;groupId&gt;com&lt;/groupId&gt;
        &lt;artifactId&gt;common&lt;/artifactId&gt;
        &lt;version&gt;1.2.0-SNAPSHOT&lt;/version&gt;
&lt;/dependencyManagement&gt;

As maven doc Transitive Dependencies shows,

Dependency management - this allows project authors to directly specify the 
versions of artifacts to be used when they are encountered in transitive 
dependencies or in dependencies where no version has been specified. 
In the example in the preceding section a dependency was directly added to A even though 
it is not directly used by A. 
Instead, A can include D as a dependency in its dependencyManagement section and directly 
control which version of D is used when, or if, it is ever referenced.

The parent dependencyManagement has higher priority than Transitive Dependencies, so in service module, it use common 1.2.0-SNAPSHOT

huangapple
  • 本文由 发表于 2020年10月10日 11:39:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64289724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定