之前一直使用Maven
进行多模块项目开发,有如下好处:
- 代码拆分,层次、结构清晰,利于维护
- Module可复用
最近使用Gradle
开发,也想使用多模块开发,写下这篇文章记录下。
本机环境
1、Java: version “1.8.0_181”
2、Mac:10.13.6,High Sierra
3、Gradle:version “4.10.2”
4、Idea 2018.2
创建根项目
cd ~/Documents/workspace
mkdir gradle-springboot2-projects
gradle init
之后生成如下项目结构:
gradle-springboot2-projects
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
配置根目录的gradle
1、在根目录新建gradle.properties
文件
内容如下:
springBootVersion=2.1.0.RELEASE
springCloudVersion=Finchley.RELEASE
2、配置build.gradle
文件
//定义扩展属性(给脚本用的脚本)
buildscript {
//定义扩展属性(可选)
ext {
}
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url 'https://repo.spring.io/libs-milestone' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply from: 'gradle/wrapper.gradle'
// 所有模块都采用统一的版本号以及groupName
allprojects {
//修改项目属性(可选)
group 'me.flygopher.springboot2'
version '1.0.0'
}
// 配置所有子模块
subprojects {
//应用插件
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
// JVM 版本号要求
sourceCompatibility = 1.8
targetCompatibility = 1.8
// java编译的时候缺省状态下会因为中文字符而失败
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
//指定目录结构
sourceSets {
main {
java{
srcDir 'src/main/java'
}
resources {
srcDir 'src/main/java'
srcDir 'src/main/resources'
}
}
}
// 定义仓库
repositories {
maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven{url 'https://mvnrepository.com/'}
mavenLocal()
mavenCentral()
}
dependencies {
// 引入根目录和各子项目下的libs中的jar包
ext.jarTree = fileTree(dir: 'libs', include: '**/*.jar')
ext.rootProjectLibs = new File(rootProject.rootDir, 'libs').getAbsolutePath()
ext.jarTree += fileTree(dir: rootProjectLibs, include: '**/*.jar')
compile jarTree
// 测试依赖
testCompile(
'junit:junit:4.12',
'org.springframework:spring-test',
'org.assertj:assertj-core:3.11.1',
'org.mockito:mockito-core:2.23.0',
)
}
}
3、最后配置settings.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.10.2/userguide/multi_project_builds.html
*/
rootProject.name = 'gradle-springboot2-projects'
到这里你的根项目配置好了。
子模块项目的新建
在IDEA
中使用右键根项目,New -> Module -> 选择Gradle,填写ArtifactId和Module Name即可。
之后会自动在子模块项目下生成build.gradle
, 以及在根项目的settings.gradle
中添加如下内容:
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.10.2/userguide/multi_project_builds.html
*/
rootProject.name = 'gradle-springboot2-projects'
include 'chapter01'
findProject(':chapter01')?.name = 'spring-boot-admin'
include 'chapter02'
findProject(':chapter02')?.name = 'spring-boot-admin-client'
其中findProject(':chapter01')?.name = 'spring-boot-admin'
是因为我的ArtifactId和Module Name不一致导致的。
参考
Building Spring Boot 2 Applications with Gradle
「真诚赞赏,手留余香」
请我喝杯咖啡?
使用微信扫描二维码完成支付
