Gradle 是一个基于 Apache Ant 和 Apache Maven 概念的项目自动化建构工具。Gradle 构建脚本使用的是 Groovy 或 Kotlin 的特定领域语言来编写的,而不是传统的 XML。
创建 Gradle 根项目
首先创建一个基础的 Gradle 工程,这个创建步骤和创建 Maven 工程类似,在 IDEA 中,点击 File -> new Project
![创建 Gradle 工程]()
点击 Next ,依次填写 GroupId、AtifactId、Version,这三个值的含义与 Maven 里的含义一样
![填写信息]()
点击 Next,最后点击 Finish
![完成创建]()
这时,在项目里,除了 src 等之类目录, 还应该会有 build.gradle 和 settings.gradle 文件,注意这两个文件,下面会用到!
修改 build.gradle
build.gradle 文件可以理解为 Maven 里的 pom.xml 文件,修改刚刚创建的那个项目里的 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
| subprojects { group "cn.liaocp.tenement" version "1.0"
apply plugin: "java" apply plugin: "idea"
sourceCompatibility = 1.8 targetCompatibility = 1.8
repositories { mavenLocal()
maven { url "https://maven.aliyun.com/nexus/content/groups/public" } }
ext { springbootVersion = "2.4.0" lombokVersion = "1.18.8" mysqlDriverVersion = "8.0.9-rc" }
dependencies { implementation "org.springframework.boot:spring-boot-starter-cache:$springbootVersion" implementation "org.springframework.boot:spring-boot-starter-data-jpa:$springbootVersion" implementation "org.springframework.boot:spring-boot-starter-security:$springbootVersion" implementation "org.springframework.boot:spring-boot-starter-validation:$springbootVersion" implementation "org.springframework.boot:spring-boot-starter-web:$springbootVersion" implementation "org.projectlombok:lombok:$lombokVersion" implementation "org.springframework.boot:spring-boot-devtools:$springbootVersion" annotationProcessor "org.projectlombok:lombok:$lombokVersion" runtimeOnly "mysql:mysql-connector-java:$mysqlDriverVersion" testImplementation "org.springframework.boot:spring-boot-starter-test:$springbootVersion" }
}
|
到这里,当前的项目可以看做此项目为 root 项目,子模块的所有依赖,都通过这里继承,项目里的版本号在这里控制
创建 core 模块
在 IDEA 的 Project 窗口,右键,选择 new -> Module,然后就会弹出一个类似上面创建 Gradle 项目的窗口
![新建模块]()
不同的是当我们点击 Next 之后
![选择模块工程]()
窗口里多了一列 Add as module to xxx,以及 GroupId 和 Version 这时是不能修改的,只能填写 AtifactId,我们填写 core
![模块信息]()
接下来 next -> Finish 就创建好了
修改 build.gradle
由于在 tenement 里已经定义了很多东西,所以在 core 里去没必要重复定义了,可以删除里面的所有内容
到这里,我们以及有了一个父工程和它的子模块了,他们两个的关系是在父工程里的 settings.gradle 文件里定义的,打开名字为 tenement 里面的 settings.gradle
1 2 3 4
| rootProject.name = 'tenement'
include 'core'
|
创建 admin 模块
重复上面的创建模块的步骤,创建一个名称为 admin 模块,在这个模块里,我们需要创建一个 SpringBoot 的启动类,将给它作为项目的启动模块,并使用配置 Gradle 打包脚本
![包结构]()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package cn.liaocp.tenement;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class AdminApplication extends SpringApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class);
}
}
|
修改 build.gradle
由于 admin 模块,负责启动和打包,所以里面会有一些其他的配置,例如构建脚本,启动类等,同时依赖 core 模块
将下面内容替换 admin 模块里的 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
| apply plugin:'application' apply plugin: 'org.springframework.boot'
mainClassName = "cn.liaocp.tenement.AdminApplication"
dependencies { compile project(":core") }
buildscript { repositories { mavenLocal() maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } }
ext { springbootVersion = "2.4.0" }
dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:$springbootVersion") } }
|
启动和打包
命令行启动
使用 cmd 进入到 admin 模块的目录下,由于我们引入了 org.springframework.boot 插件,所以我们可以通过下面命令启动:
也可以通过启动:
命令行打包
打包之后的路径:admin/build/libs/admin.jar
idea 启动和打包
打包之后的路径:admin/build/libs/admin.jar
找到点击箭头指向 Gradle ,按层次依次展开,就可以看到红框里的内容,双击即可,其实也就是 IDEA 帮我们运行了命令
![idea 启动和打包]()
这个时候的 admin.jar 包里面包含了项目启动需要的所有 jar 包,包括我们自己编写的 core 模块也打成了 jar 包,放在了里面