詳解IDEA下Gradle多模塊(項目)的構(gòu)建
我們在新起一個項目的時候,一般都會建多個子項目(IDEA里面稱之為Module模塊)。通過Gradle構(gòu)建,多個Module之間需要將公用的配置抽取到全局,子項目中只寫差異化的配置,以便于維護。
多模塊項目的Gradle目錄結(jié)構(gòu)
示例:我的示例項目demo,我需要有一個common模塊用于公用代碼,一個rest模塊用于提供rest接口,rest依賴common,如果用gradle構(gòu)建,目錄樹會是這樣:
demo ├── build.gradle -- 全局配置 ├── settings.gradle -- 全局配置 ├── common -- 子模塊1目錄 │ └── build.gradle -- 子模塊1配置 ├── rest -- 子模塊2配置 │ └── build.gradle -- 子模塊2配置 ...
IDEA下初始創(chuàng)建root目錄結(jié)構(gòu)
A. IDEA本地創(chuàng)建項目并定義項目名
如果是通過IDEA新建一個本地項目,可按照如下步驟先創(chuàng)建root項目:
1、File -> New -> Project: 選擇Gradle->Java

2、Next, 填寫GroupId和ArtifactId:
GroupId: 如com.diboot
ArtifactId:如demo
3、Next, 指定Gradle home和JVM等
4、Next, 選擇項目存放路徑。完成之后IDEA會創(chuàng)建相關(guān)文件
接下來如果你需要將本地新項目代碼上傳到代碼倉庫,可以通過VCS菜單導入:

B. 基于代碼倉庫指定的項目名創(chuàng)建root項目
而如果項目名已經(jīng)在倉庫中定義,你需要基于倉庫名初始項目的gradle配置,則項目的初始創(chuàng)建是通過VCS導入,然后用命令行初始化gradle:
- File -> New -> Project from Version Control -> ...
- 切換到Terminal命令行,輸入 gradle init,按照操作提示進行root項目的初始化。
創(chuàng)建子模塊/項目
在根目錄demo文件夾右鍵選擇 New -> Module -> Gradle -> Java, 指定子模塊ArtifactId名稱,依次添加common模塊和rest模塊后,gradle相關(guān)的目錄結(jié)構(gòu)就跟我們期望的一致了。
全局gradle配置
在demo根目錄下:
settings.gradle中的結(jié)構(gòu)定義如下
rootProject.name = 'demo' include 'common' include 'rest'
build.gradle中可以定義全局公用的構(gòu)建配置,以Spring Boot項目配置示例:
buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 所有模塊/項目的通用配置
allprojects {
group 'com.diboot'
version '1.0-SNAPSHOT'
apply plugin: 'idea'
}
// 子模塊/項目的統(tǒng)一配置
subprojects {
apply plugin: 'java'
// 指定JDK版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 指定編碼格式
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
ext {//依賴版本
springBootVersion = "2.1.2.RELEASE"
mysqlConnectorVersion = "8.0.13"
mybatisStarterVersion = "1.3.2"
fastjsonVersion = "1.2.54"
}
dependencies {
compile("javax.servlet:javax.servlet-api:4.0.1")
compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
// Mybatis
compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:$mybatisStarterVersion")
// Log4j2
compile("org.springframework.boot:spring-boot-starter-log4j2:$springBootVersion")
// JDBC Driver
compile("mysql:mysql-connector-java:$mysqlConnectorVersion")
// JSON
compile("com.alibaba:fastjson:$fastjsonVersion")
// Apache Commons
compile("org.apache.commons:commons-lang3:3.8.1")
// 單元測試
testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
testCompile("junit:junit:4.12")
}
configurations {
//移除spring boot 默認logger依賴
all*.exclude module: 'spring-boot-starter-logging'
}
}
子模塊/項目gradle配置
通用的依賴配置可以在根目錄下的build.gradle中,子模塊/項目僅配置差異化的部分即可,如子項目特定的依賴。
common下的build.gradle示例:
dependencies {
// 配置該項目特有的依賴
}
rest下的build.gradle示例(rest項目依賴common項目):
dependencies {
// 依賴common項目
compile project(":common")
// 配置該項目特有的依賴
}
代碼參考:Diboot-v2初始項目
Gradle官方相關(guān)文章:
Gradle多項目構(gòu)建介紹
Gradle多項目構(gòu)建
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 在IDEA里gradle配置和使用的方法步驟
- 解決IDEA Gradle構(gòu)建報錯''Cause: zip END header not found''
- 解決 IDEA 創(chuàng)建 Gradle 項目沒有src目錄問題
- IDEA配置java開發(fā)環(huán)境(maven、gradle、tomcat)
- IntelliJ IDEA導入Gradle項目的方法
- Gradle的安裝和IDEA集成、項目導入的詳細教程
- 詳解Gradle安裝并配置到IDEA的方法
- 使用idea和gradle編譯spring5源碼的方法步驟
- 使用IDEA和Gradle構(gòu)建Vertx項目(圖文步驟)
- idea gradle項目復(fù)制依賴小技巧(推薦)
- IDEA配置Gradle及Gradle安裝的實現(xiàn)步驟
相關(guān)文章
java 使用HttpURLConnection發(fā)送數(shù)據(jù)簡單實例
這篇文章主要介紹了java 使用HttpURLConnection發(fā)送數(shù)據(jù)簡單實例的相關(guān)資料,需要的朋友可以參考下2017-06-06
NameNode?重啟恢復(fù)數(shù)據(jù)的流程詳解
這篇文章主要為大家介紹了NameNode?重啟恢復(fù)數(shù)據(jù)的流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
java中isEmpty和isBlank的區(qū)別小結(jié)
Java中的isEmpty和isBlank都是用來判斷字符串是否為空的方法,但在不同的情況下有所區(qū)別,具有一定的參考價值,感興趣的可以了解一下2023-09-09

