确定 Spring Boot 项目中 Spring 模块和其他库的版本的最佳方法是什么?
- Spring 模块的版本
- 第三方库的版本
当您搜索“Spring 中的兼容版本”时,会出现很多问题。 但所有这些问题都与特定的库有关。
我希望了解如何确定我的项目的兼容版本的一般规则。
最佳答案
您可以查看我关于此的帖子A comprehensive list of dependencies managed by latest Spring-Boot 2.3.2.RELEASE (as a custom parent)
这只是一个引用,但您可以放弃该版本,因为它们将由 spring 本身间接管理。
好吧,这几乎是每个 Spring 开发人员都会偶然发现的事情。让我尝试解释一下我是如何解析所有托管库和第三方库的。
假设您想使用集中式配置服务器构建 spring-boot 微服务。 所以我们可以将其视为以下模块:
- 公司项目启动者:充当父级,管理依赖项
- 配置服务器
- 配置客户端
假设您选择了 spring-boot 2.3.2
版本,我使用过并且发现它更稳定。您会期望所有托管的都直接或间接使用此 spring-boot 2.3.2
版本。
我强烈建议使用https://mvnrepository.com
工件:my_company-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.my_company</groupId>
<artifactId>my_company-boot-starter-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>my_company starter-parent</name>
<dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- ... -->
<!-- ... -->
<!-- other dependencies -->
<!-- ... -->
<!-- ... -->
</dependencyManagement>
如果你必须Spring Boot Starter Parent » 2.3.2.RELEASE ,在托管依赖项下,您可以看到默认版本的所有依赖项的完整列表,这些依赖项将通过启动版本 2.3.2
隐式引用,请参阅版本
列。
更新
列指示这些托管依赖项具有最新更新,但您不需要覆盖托管依赖项的依赖项版本。如果您打算使用更新版本,则必须选择更新版本的 spring-boot-starter-parent
。所以让spring自己下载所有托管的。
对于 spring-boot-starter-parent 2.3.2 ,他们没有提到您应该使用哪个 spring-cloud-dependency 版本,这就是我们陷入困境的地方,我们需要弄清楚它。
让我们到spring-cloud-dependencies 。嘿,你可以看到很多版本,但是选择哪一个,就像验证哪个最新版本间接使用了 spring-boot 2.3.2 一样。
您需要遵循托管依赖项并遵循它,直到找到您的父版本。
例如,如果您选择:
Hoxton.SR6
Spring Cloud Dependencies(Hoxton.SR6) -> Spring Cloud Config Dependencies( 2.2.3.RELEASE) -> Spring Cloud Starter Config(2.2.3.RELEASE) -> Spring Cloud Starter(2.2.3.RELEASE) -> Spring Boot Starter(2.3.0.RELEASE)
在这里您可以看到,我们最终使用了 Spring Boot Starter(2.3.0.RELEASE)
,这不是我们所期望的。
Hoxton.SR7
Spring Cloud Dependencies(Hoxton.SR7) -> Spring Cloud Config Dependencies(2.2.4.RELEASE) -> Spring Cloud Starter Config(2.2.4.RELEASE) -> Spring Cloud Starter(2.2.4.RELEASE) -> Spring Boot Starter(2.3.2.RELEASE)
这里我们最终使用相同的启动版本2.3.2
。因此,在您的父 pom.xml
中,您可以将 Spring Cloud 版本设置为:
<properties>
<java.version>15</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lombok.version>1.18.20</lombok.version>
<spring-cloud.version>Hoxton.SR7</spring-cloud.version>
</properties>
在子 poms(jar)中,您只需提及 group
和 artifact
即可使用依赖项,跳过版本。
示例:
<parent>
<groupId>com.my_company</groupId>
<artifactId>my_company-boot-starter-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
看,我们没有在这里提供任何版本,因此 spring-cloud-config-server
版本将自动管理,并且它将是 2.2.4.RELEASE
它再次使用 2.3.2 版本的 spring-boot-starter-web
和 spring-boot-starter
。
第三方库
这在某种程度上基于语言版本。您可能想使用最新的第三方库,直到您的语言版本支持为止。
如 Lombok :1.18.20
希望这对您和其他人有所帮助,并提供一种实现版本兼容性的方法。
Tips: Never forget to check the Managed Dependency Coordinates in Aappendix of all the spring boot release page as they keep their managed dependencies & version there. Like this one Appendix F: Dependency versions
关于spring - 确定 Spring 模块和其他库的兼容版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67659727/