从子项目中提取插件重复项到根gradle配置

时间:2019-04-03 09:47:07

标签: spring-boot gradle build.gradle gradle-plugin spring-boot-gradle-plugin

我有这个项目结构:

-root-project
 -settings.gradle
 -build.gradle
 -subProjectA(spring-boot)
  -build.gradle
 -subprojectB(spring-boot)
  -build.gradle
 -subprojectC(spring-boot)
  -build.gradle
 -commonProject(java library)
  -build.gradle

我的根设置很简单。gradle:

rootProject.name = 'root-project'
include 'subProjectA'
include 'subprojectB'
include 'subprojectC'
include 'commonProject'

这是我的根项目build.gradle:

group = 'my.domain'
version = '0.0.1-SNAPSHOT'

ext {
    springBootVersion = '2.1.3.RELEASE'
    cxfVersion = '3.2.7'
    uuidGeneratorVersion = '3.1.5'
    commonLang3Version = '3.7'
    encacheVersion = '2.6.11'
    logstashVersion = '5.2'
}

在每个子项目中,我都有带有这些插件的build.gradle文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'eclipse'

我在每个spring-boot中都有重复的插件和subModule依赖项,我想将其移至common(root)文件。但是我不知道该怎么办。

1 个答案:

答案 0 :(得分:0)

在根build.gradle文件中,您可以使用subprojects块来应用通用插件或其他配置,例如通用任务,sourceCompatibility和targetCompatibility,清单信息,测试配置等。如下所示。 / p>

subprojects {
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'idea'
    apply plugin: 'java'
    apply plugin: 'eclipse'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    jar {
        manifest {
            attributes (
                'Built-By'       : System.properties['user.name'],
                'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
                'Created-By'     : "Gradle ${gradle.gradleVersion}",
                'Build-Jdk'      : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
                'Build-OS'       : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
            )
        }
    }

   // Other Configs
}