Gradle 入门教程

一、介绍

Gradle 是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于 Groovy(现在也支持 Kotlin)的内部领域特定语言。

二、特点

  1. Gradle是很成熟的技术,可以处理大规模构建
  2. Gradle对多语言、多平台支持性更好
  3. Gradle关注在构建效率上
  4. Gradle发布很频繁,重要feature开发计划透明化
  5. Gradle社区很活跃,并且增加迅速

三、安装

1.官网 (https://gradle.org/install/)[下载](https://gradle.org/next-steps/?version=5.5.1&format=bin)二进制文件,并解压

2.配置环境变量

Path    D:\tools\gradle-5.5.1\bin

3.验证

gradle -v

四、使用IDEA快速构建SpringBoot项目

在setting配置中设置本地仓库地址

1.创建一个Gradle项目

在这里插入图片描述
2.Type选择Gradle Project
在这里插入图片描述
3.选择Web中的Spring Web Starter
在这里插入图片描述
4.使用本地Gradle并配置本地仓库地址
在这里插入图片描述
5.项目创建完成
在这里插入图片描述

五、gradle配置及依赖方式说明

1.setting.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
    }
}
rootProject.name = 'demo' //项目名

2.build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'  //应用的插件

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {  //远程仓库,根据先后顺序,决定优先级
	maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
    mavenCentral() 
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

3.build.gradle中各种依赖说明

1.implementation
这个指令的特点就是,对于使用了该命令编译的依赖,对该项目有依赖的项目将无法访问到使用该命令编译的依赖中的任何程序,也就是将该依赖隐藏在内部,而不对外部公开。

2.api
完全等同于compile指令。

3.compile
这种是我们最常用的方式,使用该方式依赖的库将会参与编译和打包。

4.testCompile
testCompile 只在单元测试代码的编译以及最终打包测试时有效。

5.debugCompile
debugCompile 只在debug模式的编译和最终的debug打包时有效。

6.releaseCompile
releaseCompile 仅仅针对Release模式的编译和最终的Release打包。

7.provided
只在编译时有效,不会参与打包,可以在自己的moudle中使用该方式依赖。

8.apk(runtimeOnly)

只在生成apk的时候参与打包,编译时不会参与,很少用。

4.依赖版本号处理

compilecom.google.code.gson:gson:2.8.0

在Gradle中可以不指定版本号,比如:

compilecom.google.code.gson:gson:2.+’ 引入gson 大版本为2的包 
compilecom.google.code.gson:gson:latest.release’引入gson 最新的包

5.统一管理版本号

def dpc = rootProject.ext.testVersion
ext{
    //dependencies
    testVersion ='xx.xx.xx'
}

//使用
compile test dpc