https://blog.csdn.net/qq_27520051/article/details/90384483
一、介紹
Gradle 是一種構建工具,它拋棄了基於XML的構建腳本,取而代之的是采用一種基於 Groovy(現在也支持 Kotlin)的內部領域特定語言。
二、特點
- Gradle是很成熟的技術,可以處理大規模構建
- Gradle對多語言、多平台支持性更好
- Gradle關注在構建效率上
- Gradle發布很頻繁,重要feature開發計划透明化
- Gradle社區很活躍,並且增加迅速
三、安裝
1.官網 (https://gradle.org/install/)下載二進制文件,並解壓
2.配置環境變量
Path D:\tools\gradle-5.5.1\bin
3.驗證
gradle -v
四、使用IDEA快速構建SpringBoot項目
在setting配置中設置本地倉庫地址
1.創建一個Gradle項目
2.Type選擇Gradle Project
3.選擇Web中的Spring Web Starter
4.使用本地Gradle並配置本地倉庫地址
5.項目創建完成
五、gradle配置及依賴方式說明
1.setting.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'demo' //項目名
2.build.gradle
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management' //應用的插件
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories { //遠程倉庫,根據先后順序,決定優先級
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
3.build.gradle中各種依賴說明
1.implementation
這個指令的特點就是,對於使用了該命令編譯的依賴,對該項目有依賴的項目將無法訪問到使用該命令編譯的依賴中的任何程序,也就是將該依賴隱藏在內部,而不對外部公開。2.api
完全等同於compile指令。3.compile
這種是我們最常用的方式,使用該方式依賴的庫將會參與編譯和打包。4.testCompile
testCompile 只在單元測試代碼的編譯以及最終打包測試時有效。5.debugCompile
debugCompile 只在debug模式的編譯和最終的debug打包時有效。6.releaseCompile
releaseCompile 僅僅針對Release模式的編譯和最終的Release打包。7.provided
只在編譯時有效,不會參與打包,可以在自己的moudle中使用該方式依賴。8.apk(runtimeOnly)
只在生成apk的時候參與打包,編譯時不會參與,很少用。
4.依賴版本號處理
compile ‘com.google.code.gson:gson:2.8.0’
在Gradle中可以不指定版本號,比如:
compile ‘com.google.code.gson:gson:2.+’ 引入gson 大版本為2的包
compile ‘com.google.code.gson:gson:latest.release’引入gson 最新的包
5.統一管理版本號
def dpc = rootProject.ext.testVersion
ext{
//dependencies
testVersion ='xx.xx.xx'
}
//使用
compile test dpc