gradle 使用心得

来源:互联网 发布:淘宝圭亚那粉趾 编辑:程序博客网 时间:2024/06/07 10:36

介绍

gradle 是一个新兴的构建工具,称之为后起之秀也不为过。作为一个构建工具,它整合了ant和maven,灵活性和扩展性都远超ant和maven。gradle 是基于groovy 脚本语言的,而groovy是可以直接编译成java class 文件,直接和java进行交互。

groovy 作为一个脚本语言支持全部的java api ,也就是意味着在gradle 中你可以使用java api 编写task 和plugin,且不用编译。

我最早接触gradle 是在android studio 中,android studio 是google 主推的android application 开发环境。现在 spring 构建工具也已经换为gradle了。可见在国外gradle还是很受欢迎的。

在下面的章节我会说明为什么我会选择使用gradle。

安装

安装包下载

gradle 的官网为https://gradle.org/。首先你需要到官网下载安装包。官网提供源码包和编译之后tar。我下载的是3.4版本,下载到本地为:


系统要求:


gradle 需要运行在jdk 1.7 以上。

安装:

解压安装包到本地文件夹,配置环境变量GRADLE_HOME,GRADLE_HOME 指向gradle 安装路径,配置方法同java,这里就不详细说明,如果不会配置可以百度一下。

测试

配置环境变量后,如果是在linux或unix下面需要使用source 命令重新加载配置文件,而windows下面只能重新启动cmd或powerShell。

然后在命令行或shell中敲入下面的指令:


使用

idea

现在版本的idea 集成开发环境已经自带插件,且默认启用了。下图是我在本地导入的gradle工程。


maven2gradle

现在大多数工程都是maven工程,而grade为了拉拢更多的开发者,提供了一种方式根据pom.xml直接生成build.gradle和settings.gradle.你只需要通过shell或cmd进入到maven工程根目录,在根目录下面执行下面的指令:



build.gradle

简单介绍

gradle 构建文件是gradle的核心,想要掌握gradle编写gradle文件是基本功。gradle 文件是使用groovy语言编写,ant和maven是使用的文件格式为xml,使用xml不好的地方是文件中有很多都是你用不到的,本来一行就可以解决,偏偏需要3到4行代码才能解决。总之gradle文件是非常简洁的,基本上没有一点冗余。

文件结构

apply plugin: 'java'
apply plugin: 'maven'

group = 'com.xxx.xxx'
version = '1.0.1-SNAPSHOT'

description = ""xxx-busServer""

sourceCompatibility = 1.8
targetCompatibility = 1.7
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

 

repositories {

maven { url "http://192.168.0.xxx:8081/nexus/content/groups/public/" }
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.3.4.RELEASE') {
exclude(module: 'spring-boot-starter-logging')
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-redis', version:'1.3.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j', version:'1.3.4.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version:'2.7.1'
compile group: 'com.alibaba', name: 'fastjson', version:'1.2.7'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version:'2.7.1'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.5.2'
compile group: 'commons-httpclient', name: 'commons-httpclient', version:'3.1'
compile group: 'commons-lang', name: 'commons-lang', version:'2.6'
compile(group: 'com.alibaba', name: 'dubbo', version:'2.8.4') {
     exclude(module: 'spring-context')
     exclude(module: 'spring-aop')
     exclude(module: 'spring-core')
     exclude(module: 'spring-beans')
     exclude(module: 'spring-expression')
}
compile group: 'com.github.sgroschupf', name: 'zkclient', version:'0.1'
compile group: 'org.javassist', name: 'javassist', version:'3.15.0-GA'
compile group: 'org.apache.zookeeper', name: 'zookeeper', version:'3.5.1-alpha'
compile group: 'io.netty', name: 'netty', version:'3.7.0.Final'
compile group: 'javax.servlet', name: 'jstl', version:'1.2'
compile group: 'javax.servlet', name: 'javax.servlet-api', version:'3.0.1'
compile group: 'com.oracle', name: 'ojdbc14', version:'10.2.0.4.0'
compile(group: 'javax.servlet.jsp', name: 'jsp-api', version:'2.2') {

}
compile(group: 'org.projectlombok', name: 'lombok', version:'1.16.8') {
}
compile(group: 'org.testng', name: 'testng', version:'6.1.1') {

}
}

上面的代码是通过gradle 直接根据pom.xml 文件生成的。可以看到构建文件分为几个部分:

    • 前面两行代码为插件引用,gradle 默认提供了一些基础插件可以帮助我们快速构建。
    • repositories {
      } 在这个closure之前的为项目声明和maven一样。在这个闭包中声明的是软件仓库的地址,可以有多个url。可以调用mavenLocal()方法读取本地maven配置文件使用本地maven仓库。
    • dependencies  代码块中声明的jar依赖,依赖声明会在下面的章节中详细说明。

这个gradle文件只包含了一个工程,所以比较简单。但是一般的我们的项目都会有几个子工程。


依赖管理

在使用gradle最让人省心的就是依赖声明。相比于maven 臃肿的xml配置文件,gradle 的依赖配置已经不能在简洁了。下面对maven和gradle做个简单对比,让读者对gradle有个基本的认识。

 gradle说明maven说明jar引用
compile group:'commons-lang',name:'commons-lang',version:'2.4'
一行就可以啦,看上去简洁明了。
<dependency> <groupId>com.xx.xxx</groupId> <artifactId>xxx-xxx-api</artifactId> <version>1.0.6-SNAPSHOT</version></dependency>
基本上需要五行才能声明依赖,排除多余jar
compile (group:'com.xxx.xxx',name:'xxx-utils',version: '1.0.0'){   exclude group:'com.fasterxml.jackson.core'  exclude group:'org.apache.poi'  exclude group:'commons-codec'  exclude group:'commons-io'  exclude group:'commons-fileupload'  exclude group:'commons-httpclient'  exclude group:'commons-logging'}
gradle支持exclude 多个group
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <exclusions> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> </exclusions></dependency>

maven需要声明你要排除的group和name,

这里就可以对比出两种方式的差距。