从 Gradle 构建内部复制 Maven "dependencyManagement" 标签



我正在尝试遵循这个Spring Boot/Vaadin指南,但我使用的是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"中复制与上面<dependencyManagement/> XML元素相同的行为?

更新:当前尝试

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

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

">找不到参数的方法依赖关系管理((...">

这应该给你一个工作构建:

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 编译类路径上都可用。


编辑以回答评论中的问题:事实上,导入 BOM 会导致一组与不使用它时使用的依赖项略有不同。您可以看到依赖项差异,如下所示:

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

您可以看到细微的差异,例如org.webjars.bowergithub.webcomponents:webcomponentsjs:1.2.6与 BOM 一起使用,版本1.2.2没有它。其原因可以在定义版本1.2.6的BOM中找到,作者还提到了原因:"可传递的webjar依赖项,此处为可重复构建定义">

通常 maven dependencyManagement 标签用于导入 bom 或控制传递版本。Gradle 使用platform组件执行此操作,如下例所示。

dependencies {
  implementation platform('com.vaadin:vaadin-bom:10.0.11')
  implementation ('com.vaadin:vaadin-core')
}

这不会强迫您依赖 spring 插件来导入 bom。

相关内容

  • 没有找到相关文章

最新更新