SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…

Abstract

As Java Web development, many of you are using Maven as a build tool. Gradle, as a build tool championed by Google, is widely used in Android development, and has a tendency to replace Maven in Java Web. Does Gradle really smell that good? Today let’s give Gradle a taste of what it can do, using my scaffolding project Mall-Tiny as an example.

Introduction of Gradle

Gradle is an open source automated build tool that is flexible and performs extremely well. Build scripts can be written using Groovy or Kotlin DSL. From mobile development to microservices, from small teams to large enterprises, Gradle increases developer productivity.

Gradle has the following features:

  • Highly customizable: Gradle is very flexible by modeling in a customizable, extensible way.
  • Fast builds: Gradle builds quickly by reusing previously executed outputs, processing only changed inputs, and executing tasks in parallel.
  • Powerful: Gradle is the official Build tool for Android and supports many popular languages and technologies.

Gradle experience

Next I will change my scaffolding project Mall – Tiny from Using Maven to using Gradle to experience using Gradle.

Create a Gradle project

  • You need to download the Gradle installation package first. It is recommended that you download the full version of Gradle with the source code (otherwise, Gradle properties will not be annotated) from gradle.org/releases/

  • After downloading, unpack and create a SpringBoot project in IDEA.

  • Select create a Gradle project.

  • Then choose to use the Gradle version we downloaded earlier and enter the directory where you unzipped it.

  • After the project is created, a very simple Gradle project directory structure is as followsbuild.gradleandsettings.gradleThese two files.

Gradle plugin introduction

In the newly created build.gradle file, we can find the following three plug-ins:

plugins {
    id 'org.springframework.boot' version '2.3.0. RELEASE'
    id 'io.spring.dependency-management' version '1.0.11. RELEASE'
    id 'java'
}
Copy the code

org.springframework.boot

Gradle plugin is provided by SpringBoot. It is convenient for us to use SpringBoot. You can control the version of SpringBoot by changing the version.

io.spring.dependency-management

A Gradle plug-in (similar to Maven) that provides dependent version management.

For example, when we used Maven to manage the Druid version, we first defined the dependent version in

.

<dependencyManagement>
    <dependencies>
        <! Druid connection pool -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

There is no need to fill in the version number when importing the dependency, which has the advantage of unifying the dependent versions.

<dependencies>
    <! Druid connection pool -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
    </dependency>
</dependencies>
Copy the code

In Gradle you can use Gradle in this way.

dependencies {
    implementation 'com.alibaba:druid-spring-boot-starter'
}

dependencyManagement {
    dependencies {
        dependency 'com. Alibaba: druid - spring - the boot - starter: 1.1.10'}}Copy the code

java

The Java plug-in adds common Java compilation, testing, and other functionality to your project, and is the basis for many other JVM language Gradle plug-ins.

Turn the Maven Gradle

Converting a Maven project to Gradle is as simple as converting the pom.xml dependencies to build. Gradle dependencies.

  • For example, the Hutool dependency is written like this in Maven:
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.5.7</version>
</dependency>
Copy the code
  • Gradle is written like this on a single line:
dependencies {
    implementation 'cn. Hutool: hutool -all: 4.5.7'
}
Copy the code
  • Sometimes the Gradle download dependency is slow, here change the URL to Ali Cloud Maven repository address to speed up;
repositories {
    maven { url 'https://maven.aliyun.com/repository/public' }
    mavenCentral()
}
Copy the code
  • And a whole onebuild.gradleAll dependencies have been added.
plugins {
    id 'org.springframework.boot' version '2.3.0. RELEASE'
    id 'io.spring.dependency-management' version '1.0.11. RELEASE'
    id 'java'
}

group = 'com.macro.mall.tiny'
version = '1.0.0 - the SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    maven { url 'https://maven.aliyun.com/repository/public' }
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-configuration-processor'
    implementation 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    implementation 'com.alibaba:druid-spring-boot-starter'
    implementation 'mysql:mysql-connector-java'
    implementation 'io.springfox:springfox-swagger2'
    implementation 'io.springfox:springfox-swagger-ui'
    implementation 'io.swagger:swagger-models'
    implementation 'io.swagger:swagger-annotations'
    implementation 'cn.hutool:hutool-all'
    implementation 'io.jsonwebtoken:jjwt'
    implementation 'com.baomidou:mybatis-plus-boot-starter'
    implementation 'com.baomidou:mybatis-plus-generator'
    implementation 'org.apache.velocity:velocity-engine-core'
}

dependencyManagement {
    dependencies {
        dependency 'com. Alibaba: druid - spring - the boot - starter: 1.1.10'
        dependency 'mysql: mysql connector - Java: 8.0.16'
        dependency 'the IO. Springfox: springfox - swagger2:2.9.2'
        dependency 'IO. Springfox: springfox swagger - UI: 2.9.2'
        dependency 'the IO. Swagger: swagger - models: 1.6.0'
        dependency 'IO. Swagger: swagger - annotations: 1.6.0'
        dependency 'cn. Hutool: hutool -all: 4.5.7'
        dependency 'the IO. Jsonwebtoken: JJWT: 0.9.0'
        dependency 'com. Baomidou: mybatis - plus - the boot - starter: 3.3.2 rainfall distribution on 10-12'
        dependency 'com. Baomidou: mybatis - plus - generator: 3.3.2 rainfall distribution on 10-12'
        dependency 'org. Apache. Velocity, velocity - engine - core: 2.2'
    }
}

test {
    useJUnitPlatform()
}
Copy the code
  • Finally, all you need to do is copy the original code. Gradle has been transformed.

Contrast the Maven

Gradle builds quickly. We will clean the project and try it to see how fast it is.

  • First, use the previous Maven project, clean it directly and then package it.

  • The console output is as follows32s;

  • Then use the current Gradle project, also clean after the package (Gradle using the bootjar command), package the build;

  • The console output is as follows15s, more than twice as fast!

  • Gradle builds twice as fast as Maven, and that’s fine!

conclusion

Gradle is an official Google recommended build tool. Groovy scripts are very flexible to use if you know how to write them, and the syntax is simple and they are fast to build!

The resources

Gradle official documentation: docs.gradle.org

Project source code address

Github.com/macrozheng/…

In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!