Gradle, 基於DSL的新一代Java構建工具,gradle新一代

來源:互聯網
上載者:User

Gradle, 基於DSL的新一代Java構建工具,gradle新一代

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吧,絕對有不一樣的體驗。 :)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.