This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

preface

The text has been included in my GitHub repository. Welcome to Star: github.com/bin39232820… The best time to plant a tree was ten years ago, and the second best time is now

omg

Why this article? When COMPILING Spring source code, I used Gradle for a long time, and then I heard that SpringBoot was going to give up Maven, so I must learn Gradle, at least to understand. So today this is to learn to record their own pull.

Introduction of Gradle

Gradle is an open source automated build tool that was designed for the flexibility to build almost any type of application. Here are some of his key attributes:

  • High performance: Gradle uses a caching mechanism to speed up builds
  • Jvm-based: Gradle is a JVM-based tool, which is a good thing for developers familiar with Java, because you can use standard Java APIs in your build logic, such as custom tasks or plug-ins. This also makes Gradle easy to implement across platforms.
  • Declarative Conventions: Gradle takes advantage of Maven’s strengths. Taking a Java project as an example, it’s easy to perform a build by putting the right files in the right place and using the right plug-ins
  • Extensibility: Gradle has good extensibility. You can extend Gradle by customizing task types or even custom build models. Android’s build tools, for example, introduce many new build concepts such as flavours and build types
  • IDE support. Several major ides support importing Gradle builds and interacting with Gradle via graphical interfaces.

Gradle advantages

  • Gradle combines the best of Ant and Maven
  • Configuration, compilation, and packaging are great
  • Compact, Groovy-based scripts are handy
  • Flexible, modify the existing build lifecycle and write a few lines of configuration

Download Gradle

  • Install and configure the JDK first
  • Install gradle.org/install/
  • Download services.gradle.org/distributio…

Gradle installation and configuration

  • To decompress gradle-4.0-all.zip, remove the -all directory
  • Configure the environment variable GRADLE_HOME=D:\gradle-4.0
  • Configure the environment variable PATH=%JAVA_HOME%\BIN; %GRADLE_HOME%\BIN; …

The project structure

In fact, at first glance, most of it is similar to Maven

  • Gradle folder: A cache for gradle. The cache can be used to speed up build times
  • Idea folder: idea generated, unrelated to Gradle, omitted
  • Gradle -> Wrapper: The current version of the Gradle folder contains only the Wrapper folder, which is used to store GradleWraper jar packages and configuration files
  • SRC folder: used to store source code and resource files. This folder is not declared by Gradle, but by the Java plug-in
  • Gradle: The build.gradle file in the root directory is the build script for your project. It is arguably the most important file in your Gradle project
  • Gradlew, gradlew. Bat: Execution script of Gradle Wrapper for Unix-like systems and Windows systems respectively
  • Settings. gradle: The Project Settings file, most importantly for setting which projects are involved in the multi-project build

Write a test class, then we will find a build file, this is the compiled file!

First build. Gradle

Earlier we said that build.gradle is a build script file that defines a project and several tasks

When we type gradle build (or gradlew build) in the command line to build the project, Gradle will automatically find the build.gradle file in the current directory and build according to the script defined in it.

For a small demo, we create a new file called build.gradle in an empty directory

task hello { doLast { println 'Hello world! '}}Copy the code

Open the command line in the current directory and type gradle -q hello. The following output is displayed.

 gradle -q hello
Hello world!
Copy the code

In this demo, we define a task hello in build.gradle and add an action to it — print “Hello wold!” Then we execute the task when we run gradle -q hello on the command line.

Parse the configuration of a Java project

Plugins {id 'Java' id "checkstyle" id "com.github. Spotbugs "version "4.3.0"} group 'com.github '1.0-snapshot' sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 Repositories {mavenCentral()} test {useJUnitPlatform()} def jacksonVersion = '2.11.2' def nettyVersion = '4.1.42.final' def Slf4jVersion = '1.7.25' def lombokVersion = '1.18.12' def junitVersion = 'junit-jupiter-api:5.6.1' def CommonsCodecVersion = '1.14' def reflectionsVersion = '0.9.12' dependencies { Jackson compile "com. Fasterxml. Jackson. Core: Jackson - databind: ${jacksonVersion}" / / network: Compile "io.netty: Netty -all:${nettyVersion}" compile "io.netty: Netty -all:${nettyVersion}" compile "io.netty: Netty -all:${nettyVersion}" Slf4j: slf4J-api :${slf4jVersion}" compile "org.slf4j: slf4J-simple :${slf4jVersion}" Reflections compile "org. Reflections: reflections: ${reflectionsVersion}" / / codec: Commons-codec :commons-codec:${commonsCodecVersion}" compile :commons-codec: commons-codec:${commonsCodecVersion}" lombok compileOnly "org.projectlombok:lombok:${lombokVersion}" annotationProcessor "org.projectlombok:lombok:${lombokVersion}" testCompileOnly "org.projectlombok:lombok:${lombokVersion}" TestAnnotationProcessor "org. Projectlombok: lombok: ${lombokVersion}" / / test: junit5 testImplementation "org.junit.jupiter:${junitVersion}" testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' // Interface test: REst-assured testImplementation 'IO. Rest-assured :rest-assured:4.3.1'} apply from: rootProject.file("gradle/git-hooks.gradle") apply from: rootProject.file("gradle/checkstyle.gradle") apply from: rootProject.file("gradle/pmd.gradle") apply from: rootProject.file("gradle/spotbugs.gradle")Copy the code
  • The first thing we defined was our plug-in
  • Then some information about the current project
  • Warehouse The remote central warehouse we use
  • Unit testing
  • Define the versions of dependencies, and then use those dependencies
  • Referencing various plug-ins

The following article is well written

  • Introduction to Gradle

Daily for praise

Ok everyone, that’s all this article is about, can see here people ah, are really fans.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!