【问题标题】:Gradle BOM projectGradle BOM 项目
【发布时间】:2021-06-17 04:15:50
【问题描述】:

我正在处理一项任务,其中在 gradle 中提到了具有所有必需依赖项的父项目,并且该项目将用作父项目,基本上是一个 BOM,其中包含所有提到的组件,并且可以在其他项目中用作依赖项

比如gradle文件中提到了spring boot parent等依赖,我可以在其他项目中引用这个项目。

项目不是多模块结构。

Maven 等价物:

<dependencyManagement>
<dependencies>
  <dependency>
 </dependency>
</dependencies>
</dependencyManagement>

类似的标签 - 插件管理、构建、配置文件等

如果您可以向我推荐这些资源,否则一些先机会有所帮助。

谢谢。

【问题讨论】:

    标签: spring-boot maven gradle


    【解决方案1】:

    可以使用platform 来实现跨项目共享公共依赖版本:

    平台是一种特殊的软件组件,可用于控制传递依赖版本。在大多数情况下,它完全由依赖约束组成,这些约束要么建议依赖版本,要么强制某些版本。因此,当您需要在项目之间共享依赖版本时,这是一个完美的工具。

    使用Java Platform plugin,您可以创建一个项目来完成此操作(Gradle 没有 1:1 等效的父级):

    // build.gradle
    // Let's call this project com.example:my-parent-bom
    
    plugins {
        id 'java-platform'
    }
    
    javaPlatform {
        allowDependencies()
    }
    
    dependencies {
        // Import other BOMs
        api platform('org.springframework.boot:spring-boot-dependencies:2.4.4')
        api platform('org.springframework.cloud:spring-cloud-dependencies:2020.0.2')
    
        // Define version for dependencies not managed by the above BOMs
        constraints {
            api 'commons-httpclient:commons-httpclient:3.1'
            runtime 'org.postgresql:postgresql:42.2.5'
        }
    }
    

    然后您可以像任何其他 Gradle 项目一样 publish 这个平台并使用它:

    // some other build.gradle
    
    plugins {
        id 'java'
    }
    
    dependencies {
        implementation platform('com.example:my-parent-bom')
    }
    

    【讨论】:

    • 试过了,但得到“找不到方法 javaPlatform() 的参数”。下面的工作 - dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}" } }
    • gradle 版本 - 6.3
    • 官方依赖管理文档:docs.gradle.org/current/userguide/…
    猜你喜欢
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多