【问题标题】:What is Maven <parent> in Gradle?Gradle 中的 Maven <parent> 是什么?
【发布时间】:2018-05-26 11:05:18
【问题描述】:

在一个 Maven 项目中,我有标签:

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>

Gradle 的语法是什么?

这是我目前的 build.gradle。我认为这需要添加到构建脚本中,也许会替换我目前拥有的。如果可能,请解释为什么我需要添加它。我正在尝试将 pom.xml 转换为 gradle。

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
    }
}

plugins {
    id 'org.springframework.boot' version '1.5.9.RELEASE'
    id 'java'
}


repositories {
    mavenCentral()
}

/*Runtime dependencies*/
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile("io.springfox:springfox-swagger2:2.6.1")
    compile("io.springfox:springfox-swagger-ui:2.6.1")
    compile('org.springframework.boot:spring-boot-starter')
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-rest")
}

【问题讨论】:

  • 你想从父母那里继承什么?特性?依赖管理?通用配置?可以使用 Gradle 在一个地方声明所有这些,而无需继承。
  • 我不需要任何具体的东西。我更担心我会错过starter-parent 提供的某些依赖项。我的担心是由于没有满足依赖关系,但事实证明这不是问题(已修复)。然而,约翰的回答消除了我的一个很大的疑问。

标签: maven spring-boot gradle


【解决方案1】:

在 Gradle 中,多模块项目中只有父->子关系。您没有 Maven 中的 child->parent 定义。

因此,您通常有一个父文件夹,其中有一个 settings.gradle,其中包含对其子级的引用。

像这样(父 settings.gradle):

include 'sub-module-1'
include 'sub-module-2

然后你有两个子文件夹 sub-module-1sub-module-2 包含他们自己的 build.gradle 文件。

但是,回到你的情况,当你只使用 spring-boot 插件时,你不需要任何这些 org.springframework.boot 插件将配置所有必要的依赖项所以你只需要添加你想要的可选依赖项。

【讨论】:

    【解决方案2】:

    Maven中遵循Gradle父语法的等价物

    apply plugin : "io.spring.dependency-management"
    
    
    dependencyManagement {
        imports {
            mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
        }
    }
    

    【讨论】:

    • 这只会导入托管依赖项,对吧?没有属性。
    • 这在尝试为 gradle 项目设置特定版本的 Spring Boot 父级时帮助了我很多。谢谢
    猜你喜欢
    • 2018-09-14
    • 2018-09-17
    • 2020-04-02
    • 2015-03-13
    • 1970-01-01
    • 2011-05-22
    • 2014-11-17
    • 2014-08-05
    • 2022-07-30
    相关资源
    最近更新 更多