BlueXIII's Blog

热爱技术,持续学习

0%

使用Gradle构建Java项目

简介

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

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

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

与Maven简单对比

  1. 简洁的配置。现在有很多人都对XML深恶痛绝,在Maven中,添加一个依赖需要编写以下5行配置:

    1
    2
    3
    4
    5
    <dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.5.0</version>
    </dependency>

    而在Gradle中,只需要一行:

    1
    compile 'com.zaxxer:HikariCP:2.5.0'

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

  2. 灵活性。例如要执行一条shell命令,只需要3行。当然客观来说,灵活往往是复杂的同义词:

    1
    2
    3
    task dropDB(type: Exec) {
    commandLine ‘curl’,’-s’,’s’,’-x’,’DELETE’,"http://${db.server}:{db.port}/db_name"
    }
  3. 约定优于配置。Gradle的Java Plugin,定义了与Maven完全一致的项目布局:

    1
    2
    3
    4
    src/main/java
    src/main/resources
    src/test/java
    src/test/resources

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

安装

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

    学习资源

    关于Gradle的学习,不再赘述,有大量的资源可供查阅
  • Gradle 官网
  • Gradle User Guide
  • Gradle User Guide 中文版

单模块项目构建示例

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

/build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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

1
2
3
include 'common'
include 'repository'
include 'restapi'

/build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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

1
2
3
4
5
6
7
8
9
10
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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'
}