官网提供了两种下载方式:Binary-only(源码)、Complete(源码 + 文档),有阅读文档的需求下载Complete,无需要下载Binary-only即可。
下载地址: https://gradle.org/releases/
解压到想安装的目录下
(1)配置环境变量 GRADLE_HOME
变量名:GRADLE_HOME
变量值:解压到的目录
(2)如上,配置环境变量 GRADLE_USER_HOME
变量名:GRADLE_USER_HOME
变量值:可以是自定义gradle仓库目录,也可以和Maven的仓库同一个目录
(3)配置 Path,在环境变量Path中追加以下配置:
变量值:%GRADLE_HOME%\bin;
打开cmd,输入 gradle -v 命令,有看到打印版本信息即安装成功
(1)构建项目
(2)构建成功
(1)File > New > Project > Gradle 勾选java
(2)填写好以下信息
(3)构建成功
├── gradle //为包装文件生成的文件夹 │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew //Gradle 包装器启动脚本 ├── gradlew.bat //Gradle 包装器启动脚本 ├── settings.gradle //用于定义构建名称和子项目的设置文件 └── app ├── build.gradle //app项目的构建脚本 └── src ├── main │ └── java //默认 Java 源文件夹 │ └── demo │ └── App.java └── test └── java //默认 Java 测试源文件夹 └── demo └── AppTest.java
项目所属组。配置的是项目所属组名称。
group 'com.alone.demo'
项目版本,配置项目打包后的版本。
version '0.0.1-SNAPSHOT'
用于编译源文件的java版本,默认为当前JVM版本。
//配置java版本为1.8 sourceCompatibility = 1.8
repositories中可以配置各种仓库。寻找依赖的过程是依照你配置仓库项的顺序执行的。一般会将本地仓库配置在前,其次是私服,而后是中央仓库,这也是为了能最快的找到需要的依赖包。
repositories { mavenLocal() maven { url 'http://192.168.1.1:80/content/groups/public' } maven { url 'http://192.168.1.1:80/content/groups/public-snapshots' } mavenCentral() }
项目依赖项处理程序。用来配置引入的依赖项。
常用指令
关键字 | 说明 |
---|---|
compile | 具有依赖传递 |
implementation | 仅实现依赖(不具有传递性,即使用该关键字的包对于其它module是不可见的) |
compileOnly | 仅编译时依赖,运行时不使用 |
annotationProcessor | 编译期间使用的注释处理器 |
runtimeOnly | 仅运行时依赖项 |
testImplementation | 仅实现测试的依赖项 |
testCompileOnly | 附加依赖项仅用于编译测试,不在运行时使用 |
testRuntimeOnly | 运行时仅依赖于运行测试 |
archives | 该项目生产的工件(例如罐子)Gradle 使用它来确定构建时要执行的“默认”任务 |
引入依赖方式:
dependencies { //单个依赖 implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.73' //单个依赖简写 implementation 'com.alibaba:fastjson:1.2.73' //多个依赖 implementation 'com.alibaba:fastjson:1.2.73', 'log4j:log4j:1.2.17' //闭包依赖,为了添加额外配置 implementation("org.mybatis:mybatis:3.5.6"){ exclude group :"org.springframework",module:"spring" exclude group :"org.javassist",module:"javassist" exclude group :"org.jboss.netty",module:"netty" } //双引号方式使用变量,在ext中声明或 def 关键字声明变量 lombok_version=具体版本 annotationProcessor "org.projectlombok:lombok:$lombok_version" compileOnly "org.projectlombok:lombok:${lombok_version}" }
项目配置。可以配置全局排除依赖。
configurations { all{ exclude group :"org.springframework.boot",module:"spring-boot-starter-logging" } //编译期排除 //compile.exclude module: 'spring-boot-starter-logging' //在构建过程中排除 //all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } //简写 configurations.all*.exclude module: "spring-boot-starter-logging"
buildscript中声明的是gradle脚本自身需要使用的资源。可以声明包括变量、任务、依赖项、第三方插件、maven仓库地址等。gradle在执行脚本时会优先执行该代码块中的内容,而后执行其它脚本。
buildscript { //变量 ext { spring_boot_version = '2.3.4.RELEASE' spring_dependency_management_version = '1.0.10.RELEASE' } //maven仓库 repositories { mavenLocal() maven { url 'http://192.168.1.1:80/content/groups/public' } maven { url 'http://192.168.1.1:80/content/groups/public-snapshots' } mavenCentral() } //依赖项 dependencies { classpath "io.spring.gradle:dependency-management-plugin:${spring_dependency_management_version}" classpath "org.springframework.boot:spring-boot-gradle-plugin:${spring_boot_version}" } //任务 task myTask(){ //处理任务 } }
plugins中配置的是gradle脚本使用到的插件。如果使用的是核心插件如 ‘java’无需指定版本,使用的是“社区插件”则必须指定version。
plugins 与 apply plugin 效果是一样。apply plugin是使用较早,plugins是高版本后添加的特性,区别在于 plugins 中指定的插件必须是插件中心 https://plugins.gradle.org/存在的,此外apply plugin 可以用在 allprojects 和 subprojects 中。
1.高版本引入插件方式(gradle 2.1及以上):plugins
plugins { id 'java' id 'application' id 'org.springframework.boot' version '2.3.4.RELEASE' }
2.遗留版本引入插件(结合buildscript):apply plugin
buildscript { ext { spring_boot_version = '2.3.4.RELEASE' } repositories { mavenLocal() maven { url 'http://192.168.1.1:80/content/groups/public' } maven { url 'http://192.168.1.1:80/content/groups/public-snapshots' } mavenCentral() } //先引入插件 dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version") } } apply plugin: 'java'//核心插件不需要在buildscript中提前引入,且不需要指定版本 apply plugin: 'application'//核心插件不需要在buildscript中提前引入,且不需要指定版本 apply plugin: 'org.springframework.boot'//中心插件,需要与buildscript结合提前引入,引入之后在这里使用无需加版本号
在gradle官方文档中明确指出,如果想使用依赖关系管理,那就需要使用的gradle版本大于4.4
方式1:使用插件并指定版本
plugins { id 'java' //指定版本 id 'org.springframework.boot' version "2.3.4.RELEASE" //配置依赖管理插件,自定义托管版本 id 'io.spring.dependency-management' version "1.0.10.RELEASE" } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' testImplementation "org.springframework.boot:spring-boot-starter-test" }
方式2:使用插件,自动追加版本
plugins { id 'java' //指定版本 id 'org.springframework.boot' version "2.3.4.RELEASE" } //应用依赖管理插件,自动给插件追加版本号 apply plugin: "io.spring.dependency-management" dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' testImplementation "org.springframework.boot:spring-boot-starter-test" }
使用 BOM 方式管理 spring-cloud依赖版本
plugins { id 'java' id 'org.springframework.boot' version "2.3.4.RELEASE" id 'io.spring.dependency-management' version "1.0.10.RELEASE" } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation "org.springframework.cloud:spring-cloud-starter-config" implementation "org.springframework.cloud:spring-cloud-starter-openfeign" } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${spring_cloud_version}" } }
//在 build.gradle 增加以下配置 tasks.withType(JavaCompile) { options.encoding = 'UTF-8' }
//在 build.gradle 增加以下配置 gradle.taskGraph.whenReady { tasks.each { task -> if (task.name.contains("test")) { task.enabled = false } } }
1.build.gradle配置
// 1.私服账号密码 buildscript { ext { nexusUsername = 'admin' nexusPassword = '12345' } } // 2.配置分组,版本号 group 'com.alone.demo' version '0.0.1-SNAPSHOT' // 3.配置maven插件 plugins { id 'maven' } //发布私服,包名为文件 settings.gradle 中 rootProject.name 配置的名称,版本为上文配置的 verison 版本号 uploadArchives { repositories { mavenDeployer { //这里配置自己的私服地址 repository(url: "http://192.168.1.1:80/content/repositories/releases/") { authentication(userName: nexusUsername, password: nexusPassword) } //这里配置自己的私服地址 snapshotRepository(url: "http://192.168.1.1:80/content/repositories/snapshots/") { authentication(userName: nexusUsername, password: nexusPassword) } } } }
2.执行发布
方式1:使用命令在根目录下执行 > gradle uploadArchives
方式2:使用编译器右边的插件列表执行发布
1.build.gradle配置
// 1.配置分组,版本号 group 'com.alone.demo' version '0.0.1-SNAPSHOT' // 3.配置maven插件 plugins { id 'maven-publish' } //发布本地,包名为文件 settings.gradle 中 rootProject.name 配置的名称,版本为上文配置的 verison 版本号 publishing { publications { maven(MavenPublication) { from(components.java) } } }
2.使用idea右边插件列表执行发布
# 查看多有依赖 gradle dependencies # 查看指定配置依赖库 #如:查看编译时依赖 gradle dependencies -configuration compile #如:查看运行时依赖 gradle dependencies -configuration runtime gradle dependencies -configuration <configuration>
使用编译器插件列表查看所有依赖
自动打印出依赖关系
在项目中,经常会遇到依赖冲突,主要可以通过报错中分析具体冲突的依赖包,或通过上一步中查看依赖关系来分析存在的依赖包冲突从而进行包的版本统一,或者排除冲突包来解决问题。
1.通过配置全局排除冲突包(全局封杀),如:
//方式一 configurations { all{ exclude group :"org.springframework.boot",module:"spring-boot-starter-logging" } } //方式二 简写 configurations.all*.exclude module: "spring-boot-starter-logging"
2.诊断出具体依赖项冲突可以排除具体包,如:
dependencies { implementation("org.mybatis:mybatis:3.5.6"){ exclude group :"org.springframework",module:"spring" exclude group :"org.jboss.netty",module:"netty" } }
3.强制指定版本策略,即在发生冲突时使用指定的版本,如:
//冲突失败策略设置 configurations.all { resolutionStrategy { failOnVersionConflict() } } //强制指定版本策略设置 dependencies { //方式一 闭包 implementation ("io.netty:netty-all:4.0.44.Final"){ force true } //方式二 简写 implementation group: 'io.netty', name: 'netty-all', version: '4.0.44.Final', force: true }
4.在平时使用动态依赖来减小发生版本冲突的几率,如:
dependencies { //方式一:指定某个版本以上,减小发生版本冲突的几率。如以下依赖选择 3 以上任意一个版本 implementation 'cn.afterturn:easypoi-base:3.+' // 方式二:选择最新的版本 implementation 'cn.afterturn:easypoi-base:latest.integration' }