Gradle是什么?
Gradle 是以 Groovy 語言為基礎,面向Java應用為主,基于DSL語法的自動化構建工具。說到Java的自動化構建工具,大家一定對Ant和Maven都不會陌生,對,Gradle就是這樣一種類似的工具,不過它比Ant和Maven強大的多。
Gradle能做什么?
Gradle使用易懂的DSL語法 ,將開發過程中需要的編譯、構建、測試、打包以及部署工作,變得非常簡單、而且方便重復使用。而且,最重要的是,在Gradle中,你可以根據需要定義自己的model,不像maven中xml已經限制了你所用的model。
Gradle的優點?
Gradle的優點非常多,這里我們先看看主要的幾個:
1. 依賴管理
關于依賴管理,不得不提maven。我相信很多開發者喜歡maven的一個主要原因在于maven的依賴管理。例如,下面是maven關于junit依賴的配置:
<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>
對比一下,讓我們看看Gradle怎么做:
apply plugin: “java”
group = “com.mycompany.app”
archivesBaseName = “my-app”
version = “1.0-SNAPSHOT”
repositories {
mavenCentral()
}
dependencies {
testCompile “junit:junit:3.8.1″
}
怎么樣,不僅代碼量更少,而且看起來一目了然。
2. Task
和Ant類似,Gradle也使用task作為最小的運行單元。
首先,先讓我們看看ant中task的定義
<?xml version=”1.0″?>
<project name=”foo”>
<target name=”hello”>
<echo>Hello World</echo>
</target>
</project>
對比一下,看看Gradle是如何做的
task hello << {
println “Hello World”
}
也許對于僅僅一行的“Hello World"而言,二者看起來差不多。不過試想一下,Ant是使用定義好的task來做要做的事情,而Gradle則是使用Groovy動態腳本來實現,只要你熟悉Groovy,就可以在構建腳本中做任何想做的事情。
3. 靈活性
對于Ant或者Maven,一般使用XML或者插件來定義構建,由于XML本身的缺陷(復雜,不易閱讀,只能描述數據而不是流程),在復雜的項目中,維護XML的配置簡直就是噩夢。相反,Gradle的構建是使用groovy腳本語言來定義,因此可以靈活的在構建中使用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. 擴展性
依賴Groovy的動態性,Gralde能夠寫出基于DSL的代碼,對于復雜的項目而言,很容易維護。
另外,Gradle也支持插件機制,目前已經有很多Gradle的可用插件,像Java, War, Jetty等,使用起來非常方便。
5. 社區支持
Gradle的發展離不開社區的支持。目前,很多著名的開源組件,像Hibernate,Spring等都開始使用Gradle作為自動化構建工具。
這篇文章闡述了Hibernate的作者為什么放棄maven,而轉投Gradle:
另外,Gradle的官方網站也提供了非常詳細的文檔和例子,很容易入門 。
6. 總結
最后,讓我們看看Maven和Gradle的一個比較結果:
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的優勢已經越來越明顯。如果是新起的項目,不要猶豫,直接上Gradle吧,絕對有不一樣的體驗。 :)