灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:7286回复:0

Gradle, 基于DSL的新一代Java构建工具

楼主#
更多 发布于:2012-09-08 09:47


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” xsichemaLocation=”http://maven.apache.org/POM/4.0.0 www.atcpu.com”>
<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吧,绝对有不一样的体验。 :)
作者:想飞就飞

喜欢0 评分0
游客

返回顶部