Gradle study notes

Gradle

Getting started with Gradle

Introduction to Gradle

Gradle is a project building tool, similar to maven / Ant, compared to other tools

insert image description here

Gradle installation and configuration

  • Download Gradle that matches your Idea

    1. View the version number of the gradle file under the Idea installation directory /Plugin/lib/gradle/
    2. Go to the official website to download the corresponding version of Gradle download
    3. Select complete compressed package
  • Spring officially recommends that the gradle version is not lower than 6.8, and the course uses 7.x

  • Unzip gradle-7.4-all.zip to the directory

  • Configure environment variables

  1. Specify the gradle directory
  2. Configure the directory /bin to the path
  3. Fixed writing, GRADLE_USER_HOME gradle local warehouse directory
1.新建系统变量
变量名  :   GRADLE_HOME
变量值  :  D:\mytools\gradle-7.4-all\gradle-7.4

2. Path中添加  :  %GRADLE_HOME%\bin

3. 新建系统变量     # 如果不配置默认就是 用户目录\.gradle    例如 C:\Users\Administrator\.gradle
变量名 :  GRADLE_USER_HOME
变量值 : D:\xxx\gradle_repo
  • verify
gradle -v

Gradle configures Alibaba Cloud

  1. Create init.gradle file and write
allprojects {
     
     
repositories {
     
     
	mavenLocal()
 maven {
     
     
   url 'https://maven.aliyun.com/repository/public/'
 }
 maven {
     
     
   url 'https://maven.aliyun.com/repository/spring/'
 }
 maven {
     
     
   url 'https://maven.aliyun.com/repository/spring-plugin/'
 }
 maven {
     
     
   url 'https://maven.aliyun.com/repository/gradle-plugin/'
 }
 mavenCentral()
}

buildscript {
     
     
        repositories {
     
      
           maven{
     
     
            url 'https://maven.aliyun.com/repository/public' //阿里云
           }
       }
 }
}
  1. Priority of init.gradle configuration files
a.  gradle --init-script yourdir/init.gradle -q taskName。

b.  把init.gradle文件放到 USER_HOME/.gradle/ 目录下 

c.  把以.gradle结尾的文件放到 USER_HOME/.gradle/init.d/ 目录下 

d.  把以.gradle结尾的文件放到 GRADLE_HOME/init.d/ 目录下
  • The highest is the command line and manually specify init.gradle, but it is not common

  • Gradle's preferred user directory is C:\Users\Administrator.gradle\ init.gradle

  • The second is C:\Users\Administrator.gradle\init.d\init.gradle

  • The second is the installation directory configured in the environment variable / init.d / init.gradle

Gradle lazy settings

  • Only set %GRADLE_HOME%\bin as the directory to download and install gradle for yourself (used as a command line cmd)
  • Keep Idea and the gradle version you downloaded are the same
  • Idea does not make settings, that is, the system account directory/.gradle is used as the warehouse by default,
  • The gradle page installed by yourself uses the system account directory /.gradle as the warehouse by default
  • You can create a new init.gradle under /.gradle and control the remote warehouse of Idea and external gradle at the same time
  • Using private server configuration is unreliable, prone to problems, and quite anxious. Don't play tricks on learning knowledge, first understand and then become proficient before doing other things.

Common Gradle commands

gradle clean    清空build 目录

gradle classes  编译业务代码和配置文件

gradle test     编译测试diamagnetic , 生成测试报告

gradle build     构建项目

gradle build -x test   跳过测试构建项目

Gradle directory structure

insert image description here

  • The src directory is similar to src in maven
  • build.gradle is similar to pom.xml in maven
  • Only the war project has the webapp directory, the jar project does not
  • gradelw & gradelw.bat executes the gradle command in the specified wrapper version, not the locally installed command.

Gradle Idea build method

  • Use Spring Initializr to build the gradle project

image-20220701145411054

  • create project
    insert image description here

Gradle Wrapper

The role of Grandle Wrapper

It is equivalent to the built-in gradle command of the project, which can be together with the code.

This is consistent with keeping the gradle version environment consistent. Sending it to other developers can also run normally, regardless of how different developers handle a project

The problem of inconsistent gradle versions.

Gradle wrapper implementation

  • gradlew & gradlew.bat actually operates gradle/wrapper/gradle-wrapper.jar in the directory

image-20220702003014387

  • gradle-wrapper.properties is used to configure the version of gradle-wrapper.jar
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME  
zipStorePath=wrapper/dists   
  1. You can set the version of gradle-wrapper by modifying the version number
  2. After the modification in the idea, it will prompt to update and download, and it will be updated when the gradlew command is used in the command line
  3. The updated package will be installed in GRADLE_USER_HOME, (if this system variable is not configured, it will default to the user directory/.gradle)

insert image description here

Guess you like

Origin blog.csdn.net/weixin_48011779/article/details/125567979