【问题标题】:How can I use a dependencyManagement version with a classifier with Spring's Gradle dependency management plugin?如何使用带有 Spring 的 Gradle 依赖管理插件的分类器的 dependencyManagement 版本?
【发布时间】:2021-04-21 02:36:08
【问题描述】:

根据dependency-management-plugin#67,不能在dependencySet 块内指定分类器。此限制在相关问题 (Is it possible to set a dependencySet entry's classifier using Spring's Gradle dependency management plugin) 中进行了讨论。

dependencyManagement {
    dependencies {
        dependencySet(group:'com.querydsl', version: '4.2.2') {
            entry 'querydsl-apt' // This needs to use the "general" classifier
            entry 'querydsl-mongodb'
        }
    }
}

dependencies {
  annotationProcessor 'com.querydsl:querydsl-apt:4.2.2:general' // Version needed to use a classifier
  implementation 'com.querydsl:querydsl-mongodb'
}

解决此限制的一种方法是使用 ext 块,并定义版本变量:

ext {
  querydslVersion = 4.2.2
}

dependencyManagement {
    dependencies {
        dependencySet(group:'com.querydsl', version: querydslVersion) {
            entry 'querydsl-mongodb'
        }
    }
}

dependencies {
  annotationProcessor "com.querydsl:querydsl-apt:$querydslVersion:general" // Version needed to use a classifier
  implementation 'com.querydsl:querydsl-mongodb'
}

这种方法的一个缺点是它需要设置一个原本不会设置的属性(并且可能需要在多模块插件中的模块之间提供,从而使构建不那么明显)。如果依赖版本通过 BOM 传递(例如 imports { mavenBom 'org.springframework.boot:spring-boot-dependencies:2.3.1.RELEASE'},因为这需要手动保持版本与 BOM 的值同步,它也不会特别好用。

尽管有这个限制,还有没有办法使用依赖管理部分中定义的版本?

【问题讨论】:

    标签: spring gradle dependency-management gradle-dependencies


    【解决方案1】:

    Spring 依赖管理插件通过managedVersions map 提供对托管版本的编程访问。这可以用来获取工件的依赖版本,可以用来指定要使用的版本:

    dependencies {
      annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.managedVersions['com.querydsl:querydsl-apt']}:general"
      implementation 'com.querydsl:querydsl-mongodb'
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 2019-01-30
      • 2021-04-11
      • 2017-05-13
      • 2015-10-13
      • 2019-12-29
      • 1970-01-01
      • 2021-05-18
      • 2022-12-23
      相关资源
      最近更新 更多