使用Gradle构建Java项目

简介

Gradle是一种类似Maven的项目构建工具,它没有使用繁琐的XML,而是使用Groovy语言进行配置。
作为后起之秀,Gradle继承了Maven的一些思想,并且 配置简洁 ,有更强的 灵活性

Android Studio从一定程度上也加快了Gradle的流行,目前有非常多的开源项目已经迁移到了Gradle。
但现阶段Gradle还不能完全替代Maven,从目前GitHub上的趋势看来,二者可能要并存一段时间了。

从去年开始在一些中小型项目上尝试引入Gradle,仅做简单的项目构建,没有太过深入研究,整体使用下来的体验还是很愉快的。
本文主要是作一些科普,抛砖引玉,并贴出两个可以直接拿来使用的示例,帮助大家快速上手。

与Maven简单对比

  1. 简洁的配置。现在有很多人都对XML深恶痛绝,在Maven中,添加一个依赖需要编写以下5行配置:
  <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>2.5.0</version>
  </dependency>

而在Gradle中,只需要一行:

compile 'com.zaxxer:HikariCP:2.5.0'

所以Gradle配置文件的整体长度大约是Maven的1/4到1/5左右,并且更加易读。

  1. 灵活性。例如要执行一条shell命令,只需要3行。当然客观来说,灵活往往是复杂的同义词:
task dropDB(type: Exec) {
 commandLine ‘curl’,’-s’,’s’,’-x’,’DELETE’,"http://${db.server}:{db.port}/db_name"
}
  1. 约定优于配置。Gradle的Java Plugin,定义了与Maven完全一致的项目布局:
src/main/java
src/main/resources
src/test/java
src/test/resources

更多的比较,可以参考 https://gradle.org/maven-vs-gradle

安装

  • macOS下安装:
brew install gradle
  • Ubuntu下安装:
sudo apt install gradle
  • Windows下安装:
Step1: 在[https://gradle.org/releases](https://gradle.org/releases) 下载binary-only的zip包  
Step2: 解压至某一目录,如C:/bin/gradle  
Step3: 在系统属性-高级-环境变量中,新增GRADLE_HOME环境变量来指向安装路径,并在PATH环境变量的最后追加上GRADLE_HOME/bin  

学习资源

关于Gradle的学习,不再赘述,有大量的资源可供查阅

单模块项目构建示例

下面是一个单模块Spring Boot项目的示例,适合快速搭建小型项目

/build.gradle

buildscript {
    repositories {
        flatDir {
            dirs 'libs'
        }
        mavenLocal()
        maven { url "http://xxx.xxx.xxx.xxx:8081/nexus/content/groups/public" }
        //  // mavenCentral()  //jcenter()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: "spring-boot"

jar {
    baseName = 'project'
    version = '1.0-SNAPSHOT'
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter'
    compile 'org.apache.commons:commons-email:1.4'
    compile files('libs/jxl-2.6.12.jar')
    runtime 'mysql:mysql-connector-java:5.1.36'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

test {
    exclude 'com/foo/**'
}

task listJars(description: 'Display all compile jars.') << {
    configurations.compile.each { File file -> println file.name }
}

多模块项目构建示例

下面是一个基于Spring Boot的多模块项目示例,可以裁剪后直接拿来做为脚手架使用,适合中型项目

/settings.gradle

include 'common'
include 'repository'
include 'restapi'

/build.gradle

buildscript {
    repositories {
        mavenLocal()
        maven { url "http://xxx.xxx.xxx.xxx:8081/nexus/content/groups/public" }
        //mavenCentral()
        //jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

allprojects {
    apply plugin: 'idea'
    apply plugin: 'eclipse'

    group = 'com.foo.bar'
    version = '1.0-SNAPSHOT'
    //archivesBaseName = 'project'

    repositories {
        mavenLocal()
        maven { url "http://xxx.xxx.xxx.xxx:8081/nexus/content/groups/public" }
        //mavenCentral()
        //jcenter()
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin: "spring-boot"

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencies {
        compile 'org.springframework.boot:spring-boot-starter'
        compile 'org.springframework.boot:spring-boot-configuration-processor'
        compile 'org.springframework.boot:spring-boot-devtools'
        testCompile 'org.springframework.boot:spring-boot-starter-test'
    }

    test {
        exclude 'com/foo/bar/**'
    }

    task listJars(description: 'Display all compile jars.') << {
        configurations.compile.each { File file -> println file.name }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.1'
}

/common/build.gradle

bootRepackage.enabled = false
jar.baseName 'project-common'

dependencies {
    compile project(':repository')
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'commons-lang:commons-lang:2.6'
    compile 'org.apache.httpcomponents:httpclient:4.5.2'
    compile 'org.modelmapper:modelmapper:0.7.5'
}

/repository/build.gradle

bootRepackage.enabled = false
jar.baseName 'project-repository'

dependencies {
    //spring boot
    compile ('org.springframework.boot:spring-boot-starter-data-jpa')
    compile 'org.springframework.boot:spring-boot-starter-jdbc'
    //jackson
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.1'
    //hibernate validator
    compile 'javax.validation:validation-api:1.1.0.Final'
    compile 'org.hibernate:hibernate-validator:5.2.4.Final'
    compile 'org.hibernate:hibernate-validator-cdi:5.2.4.Final'
    compile 'javax.el:javax.el-api:2.2.4'
    compile 'org.glassfish.web:javax.el:2.2.4'
    //swagger
    compile 'io.springfox:springfox-swagger2:2.6.0'
    compile 'io.springfox:springfox-swagger-ui:2.6.0'
    //jdbc driver
    runtime 'mysql:mysql-connector-java:5.1.36'
    runtime 'com.h2database:h2:1.4.192'
    //test
    testCompile 'org.springframework.security:spring-security-core:4.1.1.RELEASE'
}

/restapi/build.gradle

jar.baseName 'project-rest-api'

dependencies {
    compile project(':repository')
    compile project(':common')
    //spring boot
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'org.springframework.boot:spring-boot-starter-cache'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    //oauth2
    compile 'org.springframework.security.oauth:spring-security-oauth2:2.0.11.RELEASE'
    //cache
    compile 'net.sf.ehcache:ehcache:2.10.2.2.21'
    //pool
    compile 'com.alibaba:druid:1.0.23'
    compile 'com.zaxxer:HikariCP:2.5.0'
    //swagger-staticdocs
    testCompile 'io.springfox:springfox-staticdocs:2.6.0'
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,355评论 6 343
  • 参考 中文官网构建指南:https://developer.android.google.cn/studio/bu...
    FinalSky阅读 5,263评论 0 16
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,565评论 25 707
  • 起初是在荔枝fm上无意间收听了Tom教练的《说话改变世界》这个节目,然后接触到了李笑来的《通往财富自由之路》,顺便...
    陈朔阅读 391评论 0 0