为什么有时会跳过 maven 依赖项中的版本号?
我对 maven 的功能还很陌生..我已经看到在放置依赖项的 pom.xml 中,有时只提到了 groupID 和 artifact id 并且跳过了版本.为什么是这样?例如以下依赖来自 springsource 网站 http://spring.io/guides/gs/authenticating-ldap/
I am fairly new to maven's capabilities.. I have seen that in pom.xml where dependencies are put, at times, only groupID and artifact id are mentioned and version is skipped. why is this? For example the below dependency is from springsource website http://spring.io/guides/gs/authenticating-ldap/
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-server-jndi</artifactId>
<version>1.5.5</version>
</dependency>
</dependencies>
但在 stackoverflow 的其他地方也提到版本不是可选的.如果有人能解释这一点,我会很高兴.
But elsewhere in stackoverflow it was also mentioned that version is not optional. I would be glad if someone could explain this.
推荐答案
是的,版本不是可选的.
Yes, version is not optional.
考虑一个多模块应用程序,它有 10 个模块,比如 module1、module2..module10.假设所有这 10 个项目都使用 spring-boot-starter-web
.如果这 10 个模块相互依赖,您可能希望在这 10 个模块中使用相同版本的 spring-boot-starter-web
.
Consider a multimodule application which has 10 modules, say module1, module2.. module10.Assume that all of these 10 projects use the spring-boot-starter-web
. In case these 10 modules are interdependent, you might want to use the same version of the spring-boot-starter-web
in each of these 10.
现在想象一下,如果您要在所有这 10 个 pom 文件中保持相同的版本号,然后当您想使用更新版本的 spring-boot-starter 时更新所有这些文件,那将是多么复杂-网络
.如果可以集中管理这些信息不是更好吗?
Now just imagine how complicated it would be if you were to maintain the same version number in all of these 10 pom files and then update all of them when you want to use a newer version of spring-boot-starter-web
. Wouldn't it be better if this information can be managed centrally?
Maven 有一个已知的 <dependencyManagement/>
标签来解决这个问题并集中依赖信息.
Maven has got something known a <dependencyManagement/>
tag to get around this and centralize dependency information.
对于您提供的示例,下面的一组链接将帮助您了解如何解析版本号,即使它不存在于您正在查看的 pom 中.
For the example which you have provided, below set of links will help you understand how the version number is resolved even though it's not present in the pom which you are looking at.
查看你正在查看的 pom 的父标签 (https://github.com/spring-guides/gs-authenticating-ldap/blob/master/complete/pom.xml)
Look at the parent tag of the pom you are looking at (https://github.com/spring-guides/gs-authenticating-ldap/blob/master/complete/pom.xml)
现在让我们去那个父节点看看版本是否在那个 pom 的依赖管理部分中指定(https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter-parent/pom.xml).不,它也没有在那里定义.现在让我们看看父母的父母.https://github.com/spring-projects/spring-boot/blob/master/spring-boot-dependencies/pom.xml.哦,是的,我们那里有版本号.
Now lets go to that parent and see if the versions are specified in the dependencyManagement section of that pom(https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter-parent/pom.xml). No it's not defined there as well. Now lets look at the parent of parent. https://github.com/spring-projects/spring-boot/blob/master/spring-boot-dependencies/pom.xml. Oh yea, we have got the version numbers there.
类似于dependencyManagement,插件可以在pom的pluginManagement部分进行管理.
Similar to dependencyManagement, plugins can be managed in the pluginManagement section of the pom.
希望能解释一下.
参考:dependencyManagement, 插件管理
相关文章