IDEA下Gradle多模塊(項目)的構建

咱們在新起一個項目的時候,通常都會建多個子項目(IDEA裏面稱之爲Module模塊)。經過Gradle構建,多個Module之間須要將公用的配置抽取到全局,子項目中只寫差別化的配置,以便於維護。

多模塊項目的Gradle目錄結構

示例:個人示例項目demo,我須要有一個common模塊用於公用代碼,一個rest模塊用於提供rest接口,rest依賴common,若是用gradle構建,目錄樹會是這樣:html

demo
├── build.gradle      -- 全局配置
├── settings.gradle   -- 全局配置
├── common            -- 子模塊1目錄
│   └── build.gradle  -- 子模塊1配置
├── rest              -- 子模塊2目錄
│   └── build.gradle  -- 子模塊2配置
...

IDEA下初始建立root目錄結構

A. IDEA本地建立項目並定義項目名

若是是經過IDEA新建一個本地項目,可按照以下步驟先建立root項目:java

  1. File -> New -> Project: 選擇Gradle->Java
    圖片描述
  2. Next, 填寫GroupId和ArtifactId:mysql

    GroupId: 如com.diboot
    ArtifactId:如demo
  3. Next, 指定Gradle home和JVM等
  4. Next, 選擇項目存放路徑。完成以後IDEA會建立相關文件

接下來若是你須要將本地新項目代碼上傳到代碼倉庫,能夠經過VCS菜單導入:
圖片描述git

B. 基於代碼倉庫指定的項目名建立root項目

而若是項目名已經在倉庫中定義,你須要基於倉庫名初始項目的gradle配置,則項目的初始建立是經過VCS導入,而後用命令行初始化gradle:github

  1. File -> New -> Project from Version Control -> ...
  2. 切換到Terminal命令行,輸入 gradle init,按照操做提示進行root項目的初始化。

建立子模塊/項目

在根目錄demo文件夾右鍵選擇 New -> Module -> Gradle -> Java, 指定子模塊ArtifactId名稱,依次添加common模塊和rest模塊後,gradle相關的目錄結構就跟咱們指望的一致了。web

全局gradle配置

在demo根目錄下:
settings.gradle中的結構定義以下spring

rootProject.name = 'demo'
include 'common'
include 'rest'

build.gradle中能夠定義全局公用的構建配置,以Spring Boot項目配置示例:sql

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'
}
// 子模塊/項目的統一配置
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示例:apache

dependencies {
    // 配置該項目特有的依賴
}

rest下的build.gradle示例(rest項目依賴common項目):json

dependencies {
    // 依賴common項目
    compile project(":common")
    // 配置該項目特有的依賴
}

代碼參考:
Diboot-v2初始項目

Gradle官方相關文章:
Gradle多項目構建介紹
Gradle多項目構建


Diboot - 簡單高效的輕代碼開發框架 (求star)

相關文章
相關標籤/搜索