用Idea 2019.3+和Gradle5.2.1+ 构建 SpringBoot多项目(三)

如今开始项目大多数默认使用SpringBoot,下面配置SpringBoot多项目,java

以前都是用Maven管理项目依赖,使用https://start.spring.io/ 初始化一个Gradle类型项目做为参考,配置以下:git

主要参考文件build.gradlegithub

plugins {
	id 'org.springframework.boot' version '2.2.5.RELEASE'
	id 'io.spring.dependency-management' version '1.0.9.RELEASE'
	id 'java'
}

group = 'org.hazulnut'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-actuator'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

test {
	useJUnitPlatform()
}

先配置公共repositoriesweb

allprojects {
    apply plugin: 'java'

    group 'org.hazulnut'
    version '1.0-SNAPSHOT'
    setProperty('sourceCompatibility', 1.8)
    setProperty('targetCompatibility', 1.8)

    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/jcenter'}
        maven { url 'https://maven.aliyun.com/repository/spring/' }
        maven { url 'https://maven.aliyun.com/repository/spring-plugin'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
        mavenLocal()
        mavenCentral()
        jcenter()
    }
}

选一个model项目配置依赖:spring

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

build正常,logback项目下依赖能够正常引入:springboot

项目都是依赖于SpringBoot,把公共配置放到root项目中配置,子项目去掉公共配置:app

root项目的build.Gradle文件maven

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

allprojects {
    apply plugin: 'java'

    group 'org.hazulnut'
    version '1.0-SNAPSHOT'
    setProperty('sourceCompatibility', 1.8)
    setProperty('targetCompatibility', 1.8)

    repositories {
        maven { url 'https://maven.aliyun.com/repository/public/' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/jcenter'}
        maven { url 'https://maven.aliyun.com/repository/spring/' }
        maven { url 'https://maven.aliyun.com/repository/spring-plugin'}
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
        mavenLocal()
        mavenCentral()
        jcenter()
    }
}

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

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
}

子项目build.Gradle文件spring-boot

从上图红框标注能够看到全部子项目都正常引入公共依赖。gradle

注意:root项目的build.Gradle中的plugins配置块不能缺乏,不然报下图所示错误。

至此 Gradle5.2.1+ 构建 SpringBoot多项目完成。

 Github项目 https://github.com/HazelNutsWorkGroup/nuts.springboot.single ,

 Gitee项目   https://gitee.com/sleeber/nuts.springboot.single

 欢迎你们交流