【问题标题】:Replicating Maven "dependencyManagement" tag from inside Gradle build从 Gradle 构建中复制 Maven “dependencyManagement”标签
【发布时间】:2019-10-31 11:06:33
【问题描述】:

我正在尝试关注this Spring Boot/Vaadin guide 然而我使用的是 Gradle,不是 Maven。

在该指南的最顶部,他们说要使用以下 Maven XML:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>10.0.11</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

但是我没有看到通过 Gradle 提供的 dependencyManagement 任务。所以我问:如何在“Gradle Land”中复制与上述&lt;dependencyManagement/&gt; XML 元素相同的行为?

更新:当前尝试

dependencyManagement {
     imports {
          mavenBom 'com.vaadin:vaadin-bom:10.0.11'
     }
}

唯一的问题是,当我将它添加到我的 build.gradle 然后运行 ​​./gradlew clean 时,我收到以下 Gradle 错误:

找不到参数的依赖管理()方法...

【问题讨论】:

标签: maven spring-boot gradle


【解决方案1】:

这应该会给你一个有效的构建:

plugins {
    // the Gradle plugin which provides the “dependencyManagement” block
    id 'io.spring.dependency-management' version '1.0.6.RELEASE'
    // add Java build functionality to be able to follow the Vaadin guide
    id 'java'
}

dependencyManagement {
    imports {
        // the Maven BOM which contains a coherent set of module versions
        // for Vaadin dependencies
        mavenBom 'com.vaadin:vaadin-bom:10.0.11'
    }
}

repositories {
    // find dependency modules on Maven Central
    mavenCentral()
}

dependencies {
    // the dependency module you need according to the Vaadin with
    // Spring Boot guide; the version of the module is taken from the
    // imported BOM; transitive dependencies are automatically taken
    // care of by Gradle (just as with Maven)
    compile 'com.vaadin:vaadin-spring-boot-starter'
}

运行 ./gradlew dependencies --configuration compileClasspath 以查看所有依赖项现在都在您的 Java 编译类路径中可用。


编辑以回答 cmets 中的问题:确实,BOM 的导入导致一组与没有它时使用的依赖项略有不同。您可以像这样看到依赖项差异:

  1. ./gradlew dependencies --configuration compileClasspath &gt; with-BOM.txt
  2. 删除dependencyManagement 块并将版本添加到单个依赖项:compile 'com.vaadin:vaadin-spring-boot-starter:10.0.11'
  3. ./gradlew dependencies --configuration compileClasspath &gt; without-BOM.txt
  4. diff -u with-BOM.txt without-BOM.txt

您可以看到细微的差别,例如 org.webjars.bowergithub.webcomponents:webcomponentsjs:1.2.6 与 BOM 一起使用和版本 1.2.2 没有它。原因可以在the BOM 中找到,其中定义了版本1.2.6,作者还提到了原因:“Transitive webjar dependencies, defined here for repeatable builds”

【讨论】:

  • 很棒的答案 -- 谢谢@Chriki (+1) -- 给你一个快速的问题:确认一下,它 听起来com.vaadin:vaadin-spring-boot-starter 想要引入某些依赖项,但是通过指定mavenBom 'com.vaadin:vaadin-bom:10.0.11',我们可以覆盖默认情况下启动的内容……我理解正确吗?再次感谢!
  • 是的,没错。我已经用更多信息更新了我的答案。
猜你喜欢
  • 2017-04-10
  • 1970-01-01
  • 2020-11-13
  • 2021-08-19
  • 1970-01-01
  • 2014-05-16
  • 2016-05-18
  • 2018-12-15
  • 1970-01-01
相关资源
最近更新 更多