Gradle是什么?
Gradle 是以 Groovy 語(yǔ)言為基礎(chǔ),面向Java應(yīng)用為主,基于DSL語(yǔ)法的自動(dòng)化構(gòu)建工具。說(shuō)到Java的自動(dòng)化構(gòu)建工具,大家一定對(duì)Ant和Maven都不會(huì)陌生,對(duì),Gradle就是這樣一種類(lèi)似的工具,不過(guò)它比Ant和Maven強(qiáng)大的多。
Gradle能做什么?
Gradle使用易懂的DSL語(yǔ)法 ,將開(kāi)發(fā)過(guò)程中需要的編譯、構(gòu)建、測(cè)試、打包以及部署工作,變得非常簡(jiǎn)單、而且方便重復(fù)使用。而且,最重要的是,在Gradle中,你可以根據(jù)需要定義自己的model,不像maven中xml已經(jīng)限制了你所用的model。
Gradle的優(yōu)點(diǎn)?
Gradle的優(yōu)點(diǎn)非常多,這里我們先看看主要的幾個(gè):
1. 依賴(lài)管理
關(guān)于依賴(lài)管理,不得不提maven。我相信很多開(kāi)發(fā)者喜歡maven的一個(gè)主要原因在于maven的依賴(lài)管理。例如,下面是maven關(guān)于junit依賴(lài)的配置:
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
對(duì)比一下,讓我們看看Gradle怎么做:
apply plugin: “java”
group = “com.mycompany.app”
archivesBaseName = “my-app”
version = “1.0-SNAPSHOT”
repositories {
mavenCentral()
}
dependencies {
testCompile “junit:junit:3.8.1″
}
怎么樣,不僅代碼量更少,而且看起來(lái)一目了然。
2. Task
和Ant類(lèi)似,Gradle也使用task作為最小的運(yùn)行單元。
首先,先讓我們看看ant中task的定義
<?xml version=”1.0″?>
<project name=”foo”>
<target name=”hello”>
<echo>Hello World</echo>
</target>
</project>
對(duì)比一下,看看Gradle是如何做的
task hello << {
println “Hello World”
}
也許對(duì)于僅僅一行的“Hello World"而言,二者看起來(lái)差不多。不過(guò)試想一下,Ant是使用定義好的task來(lái)做要做的事情,而Gradle則是使用Groovy動(dòng)態(tài)腳本來(lái)實(shí)現(xiàn),只要你熟悉Groovy,就可以在構(gòu)建腳本中做任何想做的事情。
3. 靈活性
對(duì)于Ant或者M(jìn)aven,一般使用XML或者插件來(lái)定義構(gòu)建,由于XML本身的缺陷(復(fù)雜,不易閱讀,只能描述數(shù)據(jù)而不是流程),在復(fù)雜的項(xiàng)目中,維護(hù)XML的配置簡(jiǎn)直就是噩夢(mèng)。相反,Gradle的構(gòu)建是使用groovy腳本語(yǔ)言來(lái)定義,因此可以靈活的在構(gòu)建中使用Groovy的代碼,而不僅僅是受限與XML的單一模型。
task time << {
int hours = new Date().hours
switch (hours) {
case 0..11:
println ” Good Morning! It’s ${hours}am.”
break
case 12..17: // noon to 5pm
println ” Good Afternoon! It’s ${hours > 12 ? hours – 12 : hours}pm.”
break
default: // 6pm to 11pm
println “Good Evening! It’s ${hours – 12}pm. Shouldn’t you be going home?”
}
}
4. 擴(kuò)展性
依賴(lài)Groovy的動(dòng)態(tài)性,Gralde能夠?qū)懗龌贒SL的代碼,對(duì)于復(fù)雜的項(xiàng)目而言,很容易維護(hù)。
另外,Gradle也支持插件機(jī)制,目前已經(jīng)有很多Gradle的可用插件,像Java, War, Jetty等,使用起來(lái)非常方便。
5. 社區(qū)支持
Gradle的發(fā)展離不開(kāi)社區(qū)的支持。目前,很多著名的開(kāi)源組件,像Hibernate,Spring等都開(kāi)始使用Gradle作為自動(dòng)化構(gòu)建工具。
這篇文章闡述了Hibernate的作者為什么放棄maven,而轉(zhuǎn)投Gradle:
另外,Gradle的官方網(wǎng)站也提供了非常詳細(xì)的文檔和例子,很容易入門(mén) 。
6. 總結(jié)
最后,讓我們看看Maven和Gradle的一個(gè)比較結(jié)果:
Maven pros
* Lots of third party plugins for tasks
* Fairly robust IDE support (eclipse, netbeans, intellij)
* Dependency management
* Jetty plugin (you can run the web app off the compiled source with hot deployment)
Maven cons
* XML based configuration. which usually ends up being very verbose.
* Writing simple and custom tasks (for instance copy a set of files somewhere) is tedious.
You have to write a plugin and testing + development time for simple tasks may end up too long and time consuming.
For things to go smooth you usually will have to follow maven conventions and standard plugins.
* Everything is a plugin. Even simple tasks like compile and running tests.
So for a fresh project, downloading all the plugins takes considerable time.
Plus there might be updates to the plugin which will be downloaded occasionally when you run the build.
* Lots of maven plugins usually have problems and a general opinion is that it is hard to debug.
* Building is slow compared to ant and gradle
* Does not prepare the html unit test case report by default (but something called a surefire reports)
Gradle (pros)
* Based on groovy. Scripting tasks are quite coz it is in a programming language.
* Uses ivy dependency management (which uses maven repositories). Dependency downloads are quite fast.
* Integrates ant tasks also.
* Supports the standard directory layout.
So for compilation, testing and packaging no extra tasks need to be written as long the files are in the layout.
* Come bundles with a default set of plugins which are core to gradle. So it does not have to download/update plugins
* Builds are quite fast.
* Standard junit reports
* Supports jetty server
Gradle (cons)
* IDE support or the lack of it. Eclipse does not come with good support for gradle or groovy.
Intellij idea and netbeans supports it (idea supports gradle projects) however.
* Third party plugin support is not as good as maven
整體而言,Gradle的優(yōu)勢(shì)已經(jīng)越來(lái)越明顯。如果是新起的項(xiàng)目,不要猶豫,直接上Gradle吧,絕對(duì)有不一樣的體驗(yàn)。 :)