Maven:如何在两个独立的 Maven 项目中保持依赖版本同步
Maven: How to keep a dependency version in sync within two separated maven projects
我有两个 Maven 项目,A
和 B
。这些是通用的独立库,它们都有一些共同的依赖关系(即 C
)。在大多数情况下,我需要将 A
和 B
都导入到我的新项目中。这是 myNewProject
:
的依赖树示例
myNewProject |
|_ A
| |
| | C -> version 1.2
|
|_ B
|
| C -> version 1.2
有什么方法可以使 C
的版本在 A
和 B
上保持一致。请记住,这是两个不同的项目。例如 B
不是 A
的子模块。
我问这个问题是因为我不想myNewProject
保留两个不同版本的依赖关系
在 myNewProject
中声明一个 <dependencyManagement>
部分,以便 children 继承它。来自documentation(里面也有例子):
The dependency management section is a mechanism for centralizing dependency information. When you have a set of projects that inherits a common parent it's possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs. The mechanism is best illustrated through some examples. Given these two POMs which extend the same parent:
在您的 parent 项目的 pom.xml
中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>C</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</dependencyManagement>
然后您无需在 children 项目(A
或 B
)中指定版本:
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>C</artifactId>
</dependency>
</dependencies>
我有两个 Maven 项目,A
和 B
。这些是通用的独立库,它们都有一些共同的依赖关系(即 C
)。在大多数情况下,我需要将 A
和 B
都导入到我的新项目中。这是 myNewProject
:
myNewProject |
|_ A
| |
| | C -> version 1.2
|
|_ B
|
| C -> version 1.2
有什么方法可以使 C
的版本在 A
和 B
上保持一致。请记住,这是两个不同的项目。例如 B
不是 A
的子模块。
我问这个问题是因为我不想myNewProject
保留两个不同版本的依赖关系
在 myNewProject
中声明一个 <dependencyManagement>
部分,以便 children 继承它。来自documentation(里面也有例子):
The dependency management section is a mechanism for centralizing dependency information. When you have a set of projects that inherits a common parent it's possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs. The mechanism is best illustrated through some examples. Given these two POMs which extend the same parent:
在您的 parent 项目的 pom.xml
中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>C</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</dependencyManagement>
然后您无需在 children 项目(A
或 B
)中指定版本:
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>C</artifactId>
</dependency>
</dependencies>