---
# System prepended metadata

title: Gradle multiple subprojects & modules
tags: [gradle, Springboot]

---

---
title: 'Gradle multiple subprojects & modules'
tags: gradle,Springboot
description: Gradle multiple subprojects & modules
---

# Gradle multiple subprojects & modules(用Springboot為專案)

---

[TOC]

---

## 使用的技術&工具

- Springboot 2.7.X
- Gradle 7.X


---

## 請先下載的Springboot

先到[https://start.spring.io/](https://) ，下載需要的專案
紅色部分是這一次範例的設定(Packaging到專案裡面會調整)
![](https://i.imgur.com/OzgkvDQ.png)


---

## Root Project
Root專案目錄

```
├── TestModule
│   ├── gradle
│       └── ...
│   ├── build.gradle
│   ├── gradlew
│   ├── gradlew.bat
│   ├── HELP.md
│   ├── settings.gradle
``` 

TestModule的settings.gradle內容為
``` gradle
rootProject.name = 'TestModule'
```

TestModule的build.gradle內容為
``` gradle
buildscript {
    ext {
        springBootVersion = '2.7.7'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java'
}

subprojects {

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

    group = 'com.nec'
    version = '0.0.1'
    sourceCompatibility = '17'

    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
        developmentOnly
        runtimeClasspath {
            extendsFrom developmentOnly
        }
        all {
            //exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-batch'
        implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-jdbc'
        implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-websocket'
        compileOnly 'org.projectlombok:lombok'
        runtimeOnly 'com.mysql:mysql-connector-j'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.springframework.batch:spring-batch-test'

    }

}

```

---
## Sub Project

Sub專案目錄，專案名稱為application
``` 
├── TestModule
│   ├── application
│       └── src
│           └── ...
│       └── build.gradle
│       └── settings.gradle
│   ├── gradle
│       └── ...
│   ├── build.gradle
│   ├── gradlew
│   ├── gradlew.bat
│   ├── HELP.md
│   ├── settings.gradle
``` 

application的settings.gradle內容為
``` gradle
rootProject.name = 'application'
``` 

application的build.gradle內容為
``` gradle
plugins {
	id 'java'
	id 'war'
}

dependencies {

}
``` 

---
## Module

Module專案目錄，專案名稱為library
``` 
├── TestModule
│   ├── application
│       └── src
│           └── ...
│       └── build.gradle
│       └── settings.gradle
│   ├── gradle
│       └── ...
│   ├── library
│       └── src
│           └── ...
│       └── build.gradle
│       └── settings.gradle
│   ├── build.gradle
│   ├── gradlew
│   ├── gradlew.bat
│   ├── HELP.md
│   ├── settings.gradle
``` 

library的settings.gradle內容為
``` gradle
rootProject.name = 'library'
``` 

library的build.gradle內容為
``` 
jar {
    enabled = true
}

bootJar {
    enabled = false
}
``` 

---

## 最後專案整合

TestModule的settings.gradle改為
``` gralde
rootProject.name = 'TestModule'
include 'library', 'application'
``` 

application的build.gradle改為
``` gralde
plugins {
	id 'java'
	id 'war'
}

dependencies {
	implementation project(':library')
}
``` 


---