简介

说明

本文介绍gradle的用法。

官网

​Gradle Build Tool​

​Gradle 教程_w3cschool​​(不带子目录)

​Gradle User Guide 中文版​​​(带子目录)   (英文版:​​Gradle User Manual​​)

​Gradle DSL Version 6.5​​(Gradle英文文档)

Gradle简介

        Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。面向Java应用为主。当前其支持的语言限于Java、Groovy、Kotlin和Scala。

        到此不得不说另一个很火的工具maven,众所周知,maven的两大作用:①:管理jar包  ②:构建项目

        maven的缺点:XML配置文件的繁琐,特别是项目较大时,pom.xml配置眼花缭乱。

        Gradle在maven的基础上,简化了配置文件,自动搜索Gradle等,使得我们创建管理项目更加简单。

下载与安装

其他网址


1.下载

​Gradle Distributions​

查看发布版的网址:​​https://services.gradle.org/versions/current​

2.安装

直接将下载好的安装包解压到某个文件夹下即可。

新建系统变量:变量名:GRADLE_HOME 变量值:Gradle文件解压路径

系统变量 path加入:%GRADLE_HOME%\bin;

3.验证

cmd输入:gradle -v

gradle生命周期

1. 初始化: 根据 build.gradle生成project

2. 配置: 生成task的依赖顺序和执行顺序 注意划分配置代码和执行代码

3. 执行: 执行task的动作代码

语法

下边这两种写法是一样的

maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/repository/central" }
maven { 
url "http://maven.aliyun.com/nexus/content/groups/public/"
url "https://maven.aliyun.com/repository/central"
}

工程示例

root

settings.gradle

rootProject.name = 'helloworld-base'

build.gradle

buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
lombokVersion = '1.18.6'
}
repositories {
mavenCentral()
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/repository/central" }
maven { url "https://maven.aliyun.com/repository/google" }
maven { url "https://maven.aliyun.com/repository/gradle-plugin" }
maven { url "https://maven.aliyun.com/repository/jcenter" }
maven { url "https://maven.aliyun.com/repository/spring" }
maven { url "https://maven.aliyun.com/repository/spring-plugin" }
maven { url "https://maven.aliyun.com/repository/public" }
maven { url "https://maven.aliyun.com/repository/releases" }
maven { url "https://maven.aliyun.com/repository/snapshots" }
maven { url "https://maven.aliyun.com/repository/grails-core" }
maven { url "https://maven.aliyun.com/repository/mapr-public" }
maven { url "https://maven.aliyun.com/repository/apache-snapshots" }

maven { url "https://repo.spring.io/milestone" }

maven {
url 'https://repo.spring.io/libs-milestone'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.google.code.findbugs:findbugs:3.0.1")
classpath("net.sourceforge.pmd:pmd:6.17.0")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'helloworld'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
//mavenCentral()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
maven { url "https://repo.spring.io/milestone" }

maven {
url 'https://repo.spring.io/libs-milestone'
}
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/repository/central" }
maven { url "https://maven.aliyun.com/repository/google" }
maven { url "https://maven.aliyun.com/repository/gradle-plugin" }
maven { url "https://maven.aliyun.com/repository/jcenter" }
maven { url "https://maven.aliyun.com/repository/spring" }
maven { url "https://maven.aliyun.com/repository/spring-plugin" }
maven { url "https://maven.aliyun.com/repository/public" }
maven { url "https://maven.aliyun.com/repository/releases" }
maven { url "https://maven.aliyun.com/repository/snapshots" }
maven { url "https://maven.aliyun.com/repository/grails-core" }
maven { url "https://maven.aliyun.com/repository/mapr-public" }
maven { url "https://maven.aliyun.com/repository/apache-snapshots" }
}

ext['springCloudVersion'] = 'Greenwich.RC2'

dependencies {
//Spring Boot
compile('org.springframework.boot:spring-boot-starter')
//Spring MVC
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.apache.poi:poi:4.1.0')
compile('org.apache.poi:poi-ooxml:4.1.0')
compile('org.apache.poi:poi-ooxml-schemas:4.1.0')
//Spring Boot MyBatis Plus
compile('com.baomidou:mybatis-plus-boot-starter:3.0.7')
compile('com.alibaba:druid:1.1.14')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
compile('com.google.code.gson:gson:2.8.0')
compile('com.alibaba:fastjson:1.2.32')
//http
compile('commons-io:commons-io:2.6')
compile('org.apache.httpcomponents:httpcore:4.4.9')
compile('org.apache.httpcomponents:httpclient:4.5.3')
compile('org.apache.httpcomponents:httpmime:4.5.5')
compile('io.springfox:springfox-swagger2:2.9.2')
compile('io.springfox:springfox-swagger-ui:2.9.2')
compile('commons-codec:commons-codec:1.10')
//compile('org.springframework.boot:spring-boot-starter-websocket')
//MySql
compile('mysql:mysql-connector-java')
compile('io.github.openfeign:feign-okhttp')
//Okhttp
compile('com.squareup.okhttp3:okhttp:3.12.0')
compile('eu.bitwalker:UserAgentUtils:1.20')
compile('io.reactivex.rxjava2:rxjava:2.1.4')
compile('org.apache.commons:commons-lang3:3.8.1')
compile('commons-beanutils:commons-beanutils:1.9.3')
compile('org.springframework.boot:spring-boot-configuration-processor')
//用logstash收集logback
compile('net.logstash.logback:logstash-logback-encoder:5.2')
compile('org.springframework.boot:spring-boot-starter-test')
//高性能反射工具
compile('com.esotericsoftware:reflectasm:1.11.9')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-redis')
implementation('org.springframework.cloud:spring-cloud-starter-sleuth')
implementation('org.springframework.cloud:spring-cloud-starter-zipkin')
compile('dom4j:dom4j:1.6.1')
compile('jaxen:jaxen:1.1.6')
compile('io.github.openfeign.form:feign-form:3.2.2')
compile('io.github.openfeign.form:feign-form-spring:3.2.2')
compile('org.apache.commons:commons-pool2:2.7.0')
compile('org.springframework.cloud:spring-cloud-config-client')
compile('com.googlecode.aviator:aviator:4.2.7')
compile fileTree(dir: 'lib', includes: ['*.jar'])
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
compile("org.projectlombok:lombok:1.18.10")
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
bootRun {
sourceResources sourceSets.main
}


apply plugin: "pmd"

repositories {
mavenCentral()
}
pmd {
ignoreFailures = true
pmdTest.enabled = false
}
tasks.withType(Pmd) {
reports {
xml.enabled = true
html.enabled = false
}
}

子模块

settings.gradle

rootProject.name = 'helloworld-module1'
include ":helloworld-base"
project(':helloworld-base').projectDir = new File(settingsDir, '../helloworld-base')

build.gradle

buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.google.code.findbugs:findbugs:3.0.0")
classpath("net.sourceforge.pmd:pmd:6.17.0")
}
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'helloworld'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

sourceSets {
main {
java {
srcDirs = ['src/main/java','../helloworld-base/src/main/java']
}

/*resources {
srcDir 'src/resources' //资源目录
}*/
}
}

repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}

ext['springCloudVersion'] = 'Greenwich.RC2'

dependencies {

testCompile 'org.jmockit:jmockit:1.21'
implementation('com.github.jsqlparser:jsqlparser:1.2')
compile('com.jamesmurty.utils:java-xmlbuilder:1.1')
implementation project(':helloworld-base')
implementation fileTree(dir:'lib',includes:['*.jar'])
testImplementation('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

bootJar {
mainClassName = 'helloworld.module1.HelloWorldModule1Application'
}


apply plugin: "findbugs"

repositories {
mavenCentral()
}

findbugs{
ignoreFailures=true
findbugsTest.enabled=false

}
tasks.withType(FindBugs) {
reports {
xml.enabled = true
html.enabled = false
}
}


apply plugin: "pmd"

repositories {
mavenCentral()
}
pmd {
ignoreFailures = true
pmdTest.enabled=false
}
tasks.withType(Pmd){
reports{
xml.enabled=true
html.enabled=false
}
}