【转】Gradle的使用教程

Gradle的使用教程

一、相关介绍

        Gradle是一个好用的构建工具 ,使用它的原因是:

  • 配置相关依赖代码量少,不会像maven一样xml过多 
  • 打包编译测试发布都有,而且使用起来方便 
  • 利用自定义的任务可以完成自己想要的功能

二、安装

 下载地址 https://services.gradle.org/distributions/  ,下载你所需要对应的版本,我这里下载的是gradle-6.6.1-bin.zip。

下载后解压到你想要的目录即可,然后设置环境变量:

GRADLE_HOME

 %GRADLE_HOME%\bin;

 在cmd模式下查看,出现以下信息证明安装成功:

gradle -v

然后我们可以在在环境变量里配置gradle默认的仓库地址(和maven不太一样):

GRADLE_USER_HOME

build.gradle 文件
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.7.9'
        classpath 'io.spring.dependency-management:io.spring.dependency-management.gradle.plugin:1.1.0'
    }
}

plugins {
    id 'java'
}

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

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

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

 使用自定义属性,需要使用双引号括起来EL表达式,${}, $不加{}都可以,例如:

buildscript {
    ext {
        springBootVersion = '2.7.9'
        springDependencyManagementVersion = '1.1.0'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        classpath "io.spring.dependency-management:io.spring.dependency-management.gradle.plugin:$springDependencyManagementVersion"
    }
}

plugins {
    id 'java'
}

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

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

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

 

IDEA如何正确配置Gradle? GRADLE_USER_HOME 和 Gradle user home的区别

 

配置 Gradle (IDEA) 环境的坑

Plugin [id: 'org.springframework.boot', version: '2.1.6.RELEASE'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.1.6.RELEASE')
  Searched in the following repositories:
    Gradle Central Plugin Repository

 

posted @ 2020-09-07 13:57  —八戒—  阅读(775)  评论(0编辑  收藏  举报