1. 程式人生 > 程式設計 >IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 建立多模組專案的超詳細教程

IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 建立多模組專案的超詳細教程

環境介紹 IDEA

我用的是2020.2

在這裡插入圖片描述

Gradle

安裝參考 Gradle安裝配置

我這安裝的是6.6.1

C:\Users\herion>gradle -v

------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------

Build time: 2020-08-25 16:29:12 UTC
Revision: f2d1fb54a951d8b11d25748e4711bec8d128d7e3

Kotlin: 1.3.72
Groovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM: 1.8.0_211 (Oracle Corporation 25.211-b12)
OS: Windows 10 10.0 amd64

建立 gradle-parent New Project --> Spring Initalizr 選擇jdk版本,我這裡使用1.8

在這裡插入圖片描述

Next

–> 根據需求修改 Group、Artifact、version 、Type、name、package 等,選擇所需依賴建立專案

在這裡插入圖片描述

建立成功後刪除src 目錄

在這裡插入圖片描述

建立子模組 gradle-demo

選中gradle-parent–> new -->Module

在這裡插入圖片描述

建立子模組操作與建立gradle-parent 雷同,這裡就不做複述了,建立好gradle-demo後在gradle-parent的settings.gradle 中引入模組依賴 include ‘gradle-demo'

在這裡插入圖片描述

刪除gradle-demo 中settings.gradle檔案,否則不能喝gradle-parent建立依賴關係

在這裡插入圖片描述

定義gradle 自身所需資源

buildscript {
 ext {
 springBootVersion = '2.3.4.RELEASE'
 springBootManagementVersion = '1.0.8.RELEASE'
 springCloudVersion = 'Hoxton.SR6'
 REPOSITORY_HOME = "http://maven.aliyun.com"
 }
 repositories {
 maven { url '${REPOSITORY_HOME}/nexus/content/groups/public/' }
 mavenCentral()
 maven { url 'https://repo.spring.io/snapshot' }
 maven { url 'https://repo.spring.io/milestone' }
 }
 dependencies {
 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 classpath("io.spring.gradle:dependency-management-plugin:${springBootManagementVersion}")
 }
}

修改gradle-parent專案build.gradle 配置全專案通用配置

allprojects {
 apply plugin: 'java'
 apply plugin: 'idea'
 group = 'com.herion'
 sourceCompatibility = JavaVersion.VERSION_1_8
 targetCompatibility = JavaVersion.VERSION_1_8
}

子專案通用配置

subprojects {
 apply plugin: 'org.springframework.boot'
 apply plugin: 'io.spring.dependency-management'
 [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
 //倉庫
 repositories {
 maven { url '${REPOSITORY_HOME}/nexus/content/groups/public/' }
 mavenCentral()
 maven { url 'https://repo.spring.io/snapshot' }
 maven { url 'https://repo.spring.io/milestone' }
 }

 dependencies {
 implementation 'org.springframework.boot:spring-boot-starter'
 compileOnly 'org.projectlombok:lombok'
 developmentOnly 'org.springframework.boot:spring-boot-devtools'
 annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
 annotationProcessor 'org.projectlombok:lombok'
 testImplementation('org.springframework.boot:spring-boot-starter-test') {
 exclude group: 'org.junit.vintage',module: 'junit-vintage-engine'
 }
 }

 dependencyManagement {
 imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
 imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" }
 }

 test {
 useJUnitPlatform()
 }
}

釋出外掛配置

 /**
 * 釋出外掛
 */
 publishing {
 publications {
 mavenJava(MavenPublication) {
 from components.java
 versionMapping {
  usage('java-api') {
  fromResolutionOf('runtimeClasspath')
  }
  usage('java-runtime') {
  fromResolutionResult()
  }
 }
 }
 }
 // 釋出倉庫
 repositories {
 maven {
 // TODO 換成自己的私服地址
 def releasesRepoUrl = "http://my.repo.com/nexus/repository/maven-releases"
 def snapshotsRepoUrl = "http://my.repo..com/nexus/repository/maven-snapshots"
 url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
 credentials {
  username nexusUser
  password nexusPassword
 }
 }
 }
 }

 configurations {
 [apiElements,runtimeElements].each {
 it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
 it.outgoing.artifact(bootJar)
 }
 }

驗證

Gradle 檢視所有專案

 gradle projects
> Task :projects
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'gradle-parent'
+--- Project ':gradle-common'
+--- Project ':gradle-demo'
\--- Project ':gradle-demo2'

To see a list of the tasks of a project,run gradle <project-path>:tasks
For example,try running gradle :gradle-common:tasks

Deprecated Gradle features were used in this build,making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 4s
1 actionable task: 1 executed

編譯專案

$ gradle build

Deprecated Gradle features were used in this build,making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 5s
12 actionable tasks: 12 up-to-date

執行結果

在這裡插入圖片描述

釋出jar包到nexus 命令

$ gradle publishMavenJavaPublicationToMavenRepository
> Task :gradle-common:publishMavenJavaPublicationToMavenRepository
> Task :gradle-demo:publishMavenJavaPublicationToMavenRepository
> Task :gradle-demo2:publishMavenJavaPublicationToMavenRepository
Deprecated Gradle features were used in this build,making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 24s
16 actionable tasks: 13 executed,3 up-to-date

執行結果

在這裡插入圖片描述

gradle-demo 驗證 啟動專案

在這裡插入圖片描述

瀏覽器訪問
http://localhost:8080/helllo?name=herion

在這裡插入圖片描述

demo 原始碼地址

到此這篇關於IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 建立多模組專案的文章就介紹到這了,更多相關idea+Gradle+springboot多模組項內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!