Java中的Gradle與Groovy的區(qū)別及存在的關系
前言:
在Java項目中,有兩個主要的構建系統(tǒng):Gradle和Maven。構建系統(tǒng)主要管理潛在的復雜依賴關系并正確編譯項目。還可以將已編譯的項目以及所有資源和源文件打包到.war或.jar文件中。對于簡單的構建,Maven和Gradle之間的選擇幾乎是個人喜好之一,或者也許是公司CTO或技術經(jīng)理的偏好。他們倆都是非常好的構建工具。但是,對于更復雜的項目,Gradle比Maven更勝一籌。
一、Gradle構建的利與弊
個人喜歡Gradle;我討厭XML,復雜的Java/Groovy
項目,如果沒有Gradle,幾乎是寸步難行的。除了沒有復雜的XML以外,Gradle還使用Groovy或Kotlin編寫的構建腳本提供了靈活性和更快的構建速度。借助Kotlin或Groovy的全部功能以及Gradle API庫,您可以創(chuàng)建功能強大且復雜的構建腳本。這肯定是提升效率的工具。
對于DSL(特定于域的語言)需要一些時間來適應,并且Gradle以難以學習而著稱。但是,我認為這主要是因為人們已經(jīng)習慣了Maven
。使用Gradle,您實質上可以學習一種構建語言,而不只是簡單地學習XML。與僅在Maven中添加依賴項相比,充分利用Gradle無疑具有更陡峭的學習曲線。但是向Gradle文件添加依賴項實際上并不比在Maven中困難。擴展和自定義Gradle構建比編寫Maven插件和自定義構建步驟要簡單得多。
Gradle還極大地縮短了構建時間,尤其是在大型項目中,因為Gradle僅處理已更改的任務和文件就可以很好地完成工作。此外,它提供了構建緩存和構建守護進程,使重復構建的性能更高。而且,像Maven一樣,它使用并行線程進行依賴關系解析和項目構建。同樣,對于小型,簡單的構建,這種性能提升可能并不明顯。但是對于較大的項目,這種性能提升是巨大的。
因此,總結一下。Gradle是:
- 大型項目更快
- 無限制可定制==更陡峭的學習曲線
- 使用Groovy或Kotlin代替XML
而Maven是:
- 普遍采用
- 對于較小項目更簡單
- 帶有XML和尖括號
二、Groovy的優(yōu)點
簡要介紹一下Groovy。Groovy是一種JVM語言,它可以編譯為與Java相同的字節(jié)碼,并且可以與Java類無縫地互操作。Groovy是Java的向后兼容超集,這意味著Groovy可以透明地與Java庫和代碼交互。但是,它還增加了許多新功能:可選的鍵入,函數(shù)式編程,運行時靈活性以及許多元編程內容。它還極大地清理了Java中許多冗長的代碼格式。Groovy尚未成為主流的開發(fā)語言,但是它已經(jīng)在測試(由于其簡化的語法和元編程功能)和構建系統(tǒng)中占據(jù)了一席之地。
三、依存關系
我們需要為本教程安裝一些內容:
Java:您可能已經(jīng)安裝了Java。本教程至少需要Java 1.8。如果不是,請轉到官網(wǎng)下載并安裝它。
Gradle:但是,由于本教程是有關Gradle
的教程,因此在本教程中,您可以繼續(xù)進行安裝。
四、認識build.gradle
build.gradle
文件是Gradle項目的核心,是構建配置必不可少的一項。就比如pom.xml對于Maven來說,這是等效的(沒有所有令人討厭的尖括號)
讓我們來看一段:
// 配置運行構建腳本的要求 buildscript {? ? ? // 設置自定義屬性 ? ? ext { ? ? ? ? ?springBootVersion = '2.1.6.RELEASE'? ? ? } ? ? ? // 解決buildscript塊中的依賴項時,檢查Maven Central中的依賴項 ? ? repositories { ? ? ? ? ?mavenCentral() ? ? ? } ? ? ? // 我們需要spring boot插件來運行構建腳本 ? ? dependencies { ? ? ? ? ?classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") ? ? ? } ? } ? ? ? // 添加構建插件 apply plugin: 'java'? apply plugin: 'org.springframework.boot'? apply plugin: 'io.spring.dependency-management'? ? ? // 設置全局變量 group = 'com.okta.springboottokenauth'? version = '0.0.1-SNAPSHOT'? sourceCompatibility = 1.8? ? ? // 用于搜索以解決項目依賴關系的倉庫地址 repositories { ? ? ? mavenCentral() ? } ? ? // 項目依賴 dependencies { ? ? ? implementation( 'com.okta.spring:okta-spring-boot-starter:1.2.1' ) ? ? ? implementation('org.springframework.boot:spring-boot-starter-security') ? ? ? implementation('org.springframework.boot:spring-boot-starter-web') ? ? ? testImplementation('org.springframework.boot:spring-boot-starter-test') ? ? ? testImplementation('org.springframework.security:spring-security-test') ? }
理解Gradle
構建文件的關鍵是要意識到它是一個腳本,內置在Groovy DSL中。粗略地講,它是一個配置腳本,它調用定義了配置選項的一系列閉包(考慮函數(shù))。它看起來像JSON或propertiy
文件,盡管從技術上來說這是錯誤的。
但是,真正的有趣的來自build.gradle Groovy
腳本。因為它可以執(zhí)行任意代碼并訪問任何Java庫,特定于構建的Gradle DSL和Gradle API。
五、Gradlebuildscript
讓我們從上至下查看腳本:
buildscript
閉包配置構建腳本本身(與應用程序相對)所需的屬性,依賴項和源倉庫。- 接下來,
apply plugin
以非常好友的方式應用了插件。這些擴展了Gradle-Groovy DSL框架的基本功能:將該java插件與Spring Boot和Spring依賴項管理一起應用。Java插件提供配置Gradle的期望標準的Java項目的目錄結構:src/main/java
,src/main/resources,src/test/java等,這些可以被配置為改變默認的目錄或添加新的目錄。 - 接下來,將一些標準屬性應用于構建。
repositories
塊定義了構建腳本將在哪里尋找依賴關系。Maven Central
是最常見的(mavenCentral()),但也可以配置其他倉庫,包括自定義倉庫和本地倉庫??梢允褂脕韺⒈镜?code>Maven緩存配置為倉庫mavenLocal()
。如果團隊希望協(xié)調項目之間的構建,但又不想將項目構建文件實際捆綁在一起,這將很有幫助。- 最后,定義項目依賴項。
其中每個模塊定義閉包的順序無關緊要,因為大多數(shù)build.gradle文件僅定義依賴項,設置項目屬性并使用預定義的任務,因此文件中元素的順序無關緊要。例如,沒有理由repositories
塊必須走在該dependencies
塊之前。您可以將build.gradle
文件視為Gradle在執(zhí)行調用它的shell命令分配的任何任務之前讀取的配置文件。
但是,當您開始使用Gradle
的功能來定義自定義任務并執(zhí)行任意代碼時,它將變得更加復雜。Gradle將以build.gradle自上而下的方式讀取文件,并執(zhí)行在其中找到的所有代碼塊;根據(jù)此代碼的作用,它可以在腳本中創(chuàng)建強制排序。此外,當您定義自定義任務和屬性(在Gradle API中找不到)時,排序很重要,因為這些符號不會被預先定義,因此必須在構建腳本中定義它們才能使用它們。
六、什么是閉包
回到Groovy剛問世時,函數(shù)式編程是相當小眾的領域,將諸如閉包之類的東西帶入JVM感覺很瘋狂。如今,它變得更加普遍:Javascript
中的每個函數(shù)都是閉包。一般來說,閉包是具有范圍的一流函數(shù)。
這意味著兩件事:
- 閉包是可以在運行時作為變量傳遞的函數(shù)
- 閉包保留對定義它們的變量范圍的訪問
Java
版本的閉包稱為lambda
。這些是在1.8版中引入Java的,順便說一句,這并不是在Groovy獲得最初的流行和函數(shù)式編程開始發(fā)展的同時發(fā)生的。
為了演示lambda
,請看一下名為的JUnit測試LambdaTest.java。
src/test/java/com/okta/springboottokenauth/LambdaTest.java
interface SimpleLambda { ? ? ? public int sum(int x, int y); ? } ? ? ? public class LambdaTest { ? ? ? ? ? // 創(chuàng)建一個lambda函數(shù)? ? ? public SimpleLambda getTheLambda(int offset) { ? ? ? ? ? int scopedVar = offset; ? ? ? ? ? return (int x, int y) -> x + y + scopedVar; ? ? ? } ? ? ? ? ? @Test? ? ? public void testClosure() { ? ? ? ? ? // 測試lambda方法,當offset=1 ? ? ? ? SimpleLambda lambda1 = getTheLambda(1); ? ? ? ? ? assertEquals(lambda1.sum(2,2), 5); ? ? ? ? ? ? ? // ?測試lambda方法,當offset=2 ? ? ? ? SimpleLambda lambda2 = getTheLambda(2); ? ? ? ? ? assertEquals(lambda2.sum(2,2), 6); ? ? ? } }
這個示例很有代表性,演示了lambda
的兩個基本屬性。在閉包或lambda函數(shù)中,實現(xiàn)是在getTheLambda(int offset)
方法中定義的。創(chuàng)建lambda
時,將offset變量封裝在閉包范圍中并返回。該lambda被分配給變量??梢灾貜驼{用它,并且它將引用相同的作用域。此外,可以使用封裝在單獨作用域中并分配給其他變量的新變量來創(chuàng)建新的lambda。
來自強大的面向對象的背景,封閉最初感覺就像蟲洞在嚴格的對象范圍連續(xù)體上打穿透孔一樣,奇怪地將對象的各個部分在空間和時間上連接在一起。
七、Gradle只是閉包
采取build.gradle文件的依賴項部分:
dependencies { ? ? ? implementation( 'com.okta.spring:okta-spring-boot-starter:1.2.1' ) ? ? ? implementation('org.springframework.boot:spring-boot-starter-security') ? ? ? ... }
沒有Groovy DSL速記,實際上是:
project.dependencies({ ? ? implementation( 'com.okta.spring:okta-spring-boot-starter:1.2.1' ) ? ? ? implementation('org.springframework.boot:spring-boot-starter-security') ? ? ? ...? })
括號中的所有內容實際上都是傳遞給該project.dependencies()
方法的閉包。該project對象是Project該類的實例,該類是構建的主要API父類。
這些函數(shù)將一系列依賴項作為字符串傳遞。那么,為什么不使用更傳統(tǒng)的靜態(tài)數(shù)據(jù)結構(如JSON,屬性或XML)呢?原因是這些重載函數(shù)也可以使用閉包代碼塊,因此可以進行深度自定義。
八、探索Gradle依賴項配置
依賴關系塊內部是一系列配置和名稱:
dependencies { ? ? configurationName dependencyNotation }
我們的build.gradle
文件使用兩種配置:implementation
和testImplementation
。
implementation()
定義編譯時所需的依賴項。此配置方法稱為compile
。testImplementation()
并定義了僅用于測試(舊testCompile)所需的依賴項。
您可能會看到的另一個依賴項配置是runtimeOnly
和testRuntimeOnly
。這聲明了運行時提供的不需要對其進行編譯的依賴項。
定義依賴關系的方法比對本文的范圍有用的方法更多。幾乎可以說任何東西都可以是依賴項:本地文件,jar的目錄,另一個Gradle項目等等,并且可以將依賴項配置為執(zhí)行某些操作,例如排除某些子依賴項。
值得注意的是:Gradle和Maven以完全相同的方式解決依賴關系。例如,假設我們想從Spring Boot Starter中排除Log4j依賴關系,我們可以這樣做:
dependencies { ? ? ? implementation( 'com.okta.spring:okta-spring-boot-starter:1.2.1' ) { ? ? ? ? exclude group: 'org.apache.logging.log4j', module: 'log4j-api' ? ? } }
或者說我們想將目錄中的所有文件都包含libs為依賴項:
dependencies { ? ? ? implementation fileTree('libs') }
九、打包Gradle版本
關于Gradle的一件很棒的事情是Gradle包裝器。Gradle命令行為gradle。但是,您會注意到在網(wǎng)上的許多地方,您都會看到./gradlew或gradlew.bat。這些是調用包裝程序的命令。
包裝器允許項目捆綁在項目本身內部構建項目所需的Gradle版本。這樣可以確保對Gradle的更改不會中斷構建。它還可以確保即使沒有安裝Gradle的人也可以運行構建。
它將以下文件添加到您的項目:
├── gradle │ ? └── wrapper │ ? ? ? ├── gradle-wrapper.jar │ ? ? ? └── gradle-wrapper.properties ├── gradlew └── gradlew.bat
gradlew和gradlew.bat是用于Linux/OSX和Window(分別)執(zhí)行腳本。他們運行build.gradle使用捆綁的搖籃文件.jar的gradle/wrapper子目錄。
十、任務
任務是Gradle的核心。Java插件增加了十幾個任務,包括:clean
,compile
,test
,jar
,和uploadArchives
。Spring Boot
插件添加了bootRun任務,該任務運行Spring Boot應用程序。
通常,任務是這樣運行的:gradle taskName otherTaskName
,或使用包裝器:./gradlew taskName otherTaskName
。
如果打開終端并cd進入示例項目的基本目錄,則可以使用gradle tasks列出build.gradle文件定義的所有任務。tasks當然,它本身是由基本Gradle API定義的任務。
> Task :tasks ------------------------------------------------------------ Tasks runnable from root project ------------------------------------------------------------ Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects that depend on it. buildNeeded - Assembles and tests this project and all projects it depends on. classes - Assembles main classes. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles test classes. Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Distribution tasks ------------------ assembleDist - Assembles the main distributions assembleMonitorDist - Assembles the monitor distributions distTar - Bundles the project as a distribution. distZip - Bundles the project as a distribution. installDist - Installs the project as a distribution as-is. installMonitorDist - Installs the project as a distribution as-is. monitorDistTar - Bundles the project as a distribution. monitorDistZip - Bundles the project as a distribution. Documentation tasks ------------------- groovydoc - Generates Groovydoc API documentation for the main source code. javadoc - Generates Javadoc API documentation for the main source code. Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'fun'. components - Displays the components produced by root project 'fun'. [incubating] dependencies - Displays all dependencies declared in root project 'fun'. dependencyInsight - Displays the insight into a specific dependency in root project 'fun'. dependentComponents - Displays the dependent components of components in root project 'fun'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'fun'. [incubating] projects - Displays the sub-projects of root project 'fun'. properties - Displays the properties of root project 'fun'. tasks - Displays the tasks runnable from root project 'fun'. IDE tasks --------- cleanIdea - Cleans IDEA project files (IML, IPR) idea - Generates IDEA project files (IML, IPR, IWS) openIdea - Opens the IDEA project Verification tasks ------------------ check - Runs all checks. test - Runs the unit tests. Rules ----- Pattern: clean<TaskName>: Cleans the output files of a task. Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration. Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration. To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task>
我想指出dependencies
任務。它將列出一棵樹,其中包含項目所需的所有依賴關系(包括子依賴關系)。嘗試gradle dependencies
在項目根目錄中運行。您可以使用該dependencyInsight任務來深入了解特定的子依賴項。
另一個有助于解決問題的properties
任務是該任務,該任務列出了在根項目對象實例上定義的所有屬性。
當然,在開發(fā)Spring Boot項目時,可以使用命令:./gradlew bootJar,該任務將項目及其依賴項打包在一個jar文件中。
到此,基礎篇完事兒,提高篇中將會實踐一下自定義任務和Groovy閉包在Gradle配置文件build.gradle文件中如何使用。
到此這篇關于Java中的Gradle與Groovy的區(qū)別及存在的關系的文章就介紹到這了,更多相關Java中的Gradle與Groovy內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
RocketMQ NameServer保障數(shù)據(jù)一致性實現(xiàn)方法講解
這篇文章主要介紹了RocketMQ NameServer保障數(shù)據(jù)一致性實現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12spring學習之@SessionAttributes實例解析
這篇文章主要介紹了spring學習之@SessionAttributes實例解析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02Intellij IDEA命令行執(zhí)行java無法加載主類解決方案
這篇文章主要介紹了Intellij IDEA命令行執(zhí)行java無法加載主類解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09