注:IntelliJ IDEA版本為2019.3.1。另一篇文章可供參考: [Java] IntelliJ IDEA 2019.3.1創建完整的Spring Boot項目
1. 新建項目
2. 新建項目為Gradle
3. 刪掉根項目的src目錄,開始准備創建模塊
4. 開始創建一個通用模塊common模塊
選擇file->new->module(注意此時鼠標的位置應該在根項目名上)
下一步
下一步
下一步
5. 至此,common模塊已經建好
把common模塊下紅圈圈起來的三個無用文件刪除
6. 繼續重復上面新建module的操作來創建一個logrevert模塊
7. 選擇依賴
選擇web依賴,在這里勾選 spring web。Spring會把web相關的jar在項目初始化時加入依賴導入。下面有很多其他的依賴可自行嘗試,然后一路下一步,模塊創建完成后把剛才common模塊刪除的那三個無用文件也刪了。
至此,項目結構完成,下面是細節調整
8. 根目錄下的settings.gradle
把所有的子模塊都include進來,這里include的順序要注意順序,被依賴的放在前面,因為gradle打包時是按照順序依賴的。不然可能會出現有些jar包沒打進去
include('common', 'logrevert')
9. 配置子項目通用配置
根目錄下的build.gradle
plugins { id 'org.springframework.boot' version '2.2.4.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' id 'war' } group = 'com.ppwang' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() maven { url'https://repo.spring.io/milestone' } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } test { useJUnitPlatform() } subprojects{ apply plugin:'java' apply plugin:'idea' apply plugin:'eclipse' apply plugin:'jacoco' apply plugin:'org.springframework.boot' apply plugin:'io.spring.dependency-management' group ='com.ppwang.testtool' sourceCompatibility =1.8 targetCompatibility =1.8 // java編譯的時候缺省狀態下會因為中文字符而失敗 [compileJava,compileTestJava,javadoc]*.options*.encoding ='UTF-8' bootJar { enabled =false // 默認不需要打可執行jar包 } repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() maven { url'https://repo.spring.io/milestone' } } dependencies { testCompile('org.springframework.boot:spring-boot-starter-test') } }
10. 分別修改common模塊和logrevert模塊的build.gradle。logrevert依賴於common
common
plugins { id 'org.springframework.boot' id 'io.spring.dependency-management' id 'java' } group = 'com.ppwang.testtool' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() maven { url'https://repo.spring.io/milestone' } } dependencies { 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() }
logrevert
plugins { id 'org.springframework.boot' id 'io.spring.dependency-management' id 'java' } group = 'com.ppwang.testtool' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() maven { url'https://repo.spring.io/milestone' } } dependencies { implementation project(':common') 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() }
11. 至此,所有的配置就已經完成
打開gradle管理,刪除多余的模塊編譯,只保留跟項目(即最全的項目),刪除common和logrevert。然后點擊刷新按鈕刷新gradle。多模塊項目就全部完成了
Have fun with Java!