首页 > 解决方案 > 如何使用配置文件 maven 指定 Spring Boot 启动器依赖项的版本?

问题描述

我在一个 Spring boot 项目中工作,它有不同的方式连接数据库,在dev中我只用postgresql驱动程序连接它,而对于qaprod,我需要通过连接,spring-cloud-gcp-starter-sql-postgresql因为我们有一个云环境。

因此,为了管理所有这些可能性,我正在使用 maven 中的配置文件来控制我的 Spring 配置文件。

但是我在我的个人资料声明中放置了 spring cloud 的启动器依赖项,因为它们没有版本并且 maven 无法识别默认版本。

这是我的 pom 的一部分,带有版本错误: 聚甲醛:

那么,我该如何解决这个问题呢?

有没有办法知道哪个是启动器依赖项的默认版本并将该信息保存在要在配置文件标签中使用的变量中?

任何想法都会有很大帮助。

感谢您的阅读和您的时间。祝福大家

标签: javaspringspring-bootmaven

解决方案


您可以安全地dependencyManagement从您的个人资料中提取。

参考:依赖范围

import此范围仅受该部分中 pom 类型的依赖项支持 <dependencyManagement>。它表示依赖项将被指定 POM<dependencyManagement>部分中的有效依赖项列表替换。由于它们被替换,具有导入范围的依赖项实际上并不参与限制依赖项的传递性。

因此,您可以安全地从您的配置文件中提取 dependencyManagement。

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <profiles>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-gcp-starter</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
                </dependency>
            </dependencies>
        </profile>
        ...
   </profiles>

或者您可以自己提供已解决的版本;因为Greenwich.SR2那将是1.1.2.RELEASE...


推荐阅读