Gradle this article is enough for 08 wrapper

Keywords: Programming Gradle Apache jvm Oracle

Use of wrapper

Gradle Wrapper is the recommended usage of gradle. This article will illustrate how to use it with specific examples.

Gradle Wrapper

The Gradle Wrapper is actually a script, which can be used to download and use the specified version of gradle. It can be downloaded before use as required, effectively avoiding environment consistency problems such as local machine settings. Although the installation of gradle is very simple, the development of using gradle wrapper is able to create the gradle project in a more standardized way.

Usage mode

The usage of the Gradle Wrapper is mainly as follows:

  • Step 1: Download gradle
  • Step 2: extract and store the downloaded gradle to the directory specified by GRADLE_USER_HOME
  • Step 3: use the extracted gradle
graph LR
    GradleBuild -- 1.Download distribution --> Server
    GradleBuild -- 1.Store and unpack distribution --> GradleUserHome
    GradleUserHome -- 3.Use distribution --> GradleBuild
    

Preparation beforehand

Using the following code example, the content will not be explained any more. For details, please refer to:

orrincn:wrapper orrin$ ls
build.gradle
orrincn:wrapper orrin$ cat build.gradle 
println "[phase:configuration] build.gradle ..."

task compile {
  group 'compile'
  description 'compile task'
  println "[phase:configuration] compile"
  doFirst {
    println "[phase:execution] compile :doFirst()"
  }
}

tasks.create(name: 'test',dependsOn: compile) {
  group 'test'
  description 'test task'
  println "[phase:configuration] test"
  doLast {
    println "[phase:execution] test:doLast()"
  }
}

tasks.create("packaging") {
  group 'packaging'
  description 'packaging task'
  dependsOn test
  enabled   true
  println "[phase:configuration] packaging"
  doLast {
    println "[phase:execution] packaging:doLast()"
  }
}

class Install extends DefaultTask{
  String installObjectName

  @TaskAction
  void checkObject() {
    println "[phase:execution] install:checkObject   (${installObjectName})"
  }

  @TaskAction
  void installObject() {
    println "[phase:execution] install:installObject (${installObjectName})"
  }
}

task install(type: Install) {
  group 'install'
  description 'install task'
  installObjectName 'test.jar'

  println "[phase:configuration] install"
  doFirst {
    println "[phase:execution] install:doFirst()"
  }
  doLast {
    println "[phase:execution] install:doLast()"
  }
}
install.dependsOn packaging
install.onlyIf { packaging.enabled }
orrincn:wrapper orrin$

gradle wrapper

Through gradle tasks, you can confirm that there is a built-in task like wrapper in Build Setup tasks.

orrincn:wrapper orrin$ gradle tasks
...ellipsis
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
...ellipsis
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
orrincn:wrapper orrin$

The description of wrapper is that this task is used to create gradle wrapper files. Next, let's take a look at the specific files created

orrincn:wrapper orrin$ ls
build.gradle
orrincn:wrapper orrin$ gradle wrapper

> Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
orrincn:wrapper orrin$ ls
build.gradle gradle       gradlew      gradlew.bat
orrincn:wrapper orrin$ tree
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat

2 directories, 5 files
orrincn:wrapper orrin$ 

Document description:

  • gradlew: Shell script used to execute wrapper command under linux or Unix
  • gradlew.bat: batch script for executing wrapper command under Windows
  • gradle-wrapper.jar: code implementation for downloading gradle
  • gradle-wrapper.properties: the configuration information used by the wrapper, such as the version of gradle
  • By default, the information of gradle-wrapper.properties is as follows
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

Gradeuser home can be set by environment variable. The default gradeuser home is home /. Grader. According to the information here, the downloaded Grader will be decompressed to HOME/.gradle, according to the information here, the downloaded gradle will be unzipped to {HOME}/.gradle/wrapper/dists

Usage scenarios

Currently, the version of gradle installed in the system is 6.2.0, but you want to use the 6.2.1 gradle in the current project, so the gradle wrapper can show your skill

Set the version of gradle used by the wrapper

You can use the command to modify the information of the gradle version used by the wrapper

orrincn:wrapper orrin$ gradle wrapper --gradle-version 6.2.1 --distribution-type bin
Starting a Gradle Daemon (subsequent builds will be faster)

> Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install

BUILD SUCCESSFUL in 3s
1 actionable task: 1 executed
orrincn:wrapper orrin$

As you can see, the actual command only modifies the download file information of the configuration file of the gradle wrapper

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

Of course, the settings can also be achieved faster by using the gradle distribution URL option or directly modifying the configuration file. As for the specific version, the installation package of gradle conforms to: gradle - version information - bin|all.zip format. Follow this format

wrapper use

Just set the information of the gradle version specified in the download url, and the wrapper is ready. Use any command of the wrapper. The first execution will download the gradle and extract it to ${HOME}/.gradle/wrapper/dists. Before execution, we can see that there is no content at present

orrincn:wrapper orrin$ ls
build.gradle gradle       gradlew      gradlew.bat
orrincn:wrapper orrin$ ls ${HOME}/.gradle/wrapper/dists
ls: /Users/orrin/.gradle/wrapper/dists: No such file or directory
orrincn:wrapper orrin$ 

The reason why gradle wrapper s are called wrappers is that they cover a layer, so the basic commands of gradle can be used here. Here, you can directly use the - v used to confirm the version of gradle

orrincn:wrapper orrin$ ./gradlew -v
Downloading https://services.gradle.org/distributions/gradle-6.2.1-bin.zip
.........10%.........20%.........30%..........40%.........50%.........60%..........70%.........80%.........90%..........100%

------------------------------------------------------------
Gradle 6.2.1
------------------------------------------------------------

Build time:   2020-02-24 20:24:10 UTC
Revision:     aacbcb7e587faa6a8e7851751a76183b6187b164

Kotlin:       1.3.61
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          1.8.0_152 (Oracle Corporation 25.152-b16)
OS:           Mac OS X 10.15.4 x86_64

orrincn:wrapper orrin$

So you can see that the first execution has Downloading Downloading https://services.gradle.org/distributions/gradle-6.2.1-bin.zip. The specific version is exactly what was set before. The download and decompression of gradle are transparent to us. Why can wrapper find the 6.2.1 version

orrincn:wrapper orrin$ ls ${HOME}/.gradle/wrapper/dists
gradle-6.2.1-bin
orrincn:wrapper orrin$ 

As you can see, the wrapper has downloaded it and decompressed it here. Of course, you can also use the gradle installed in the system. In this way, you can directly set gradle'user'home. Besides, the downloading and setting will only happen once. Unless you delete it manually again, it will be much clearer to use gradlew again

orrincn:wrapper orrin$ ./gradlew -v

------------------------------------------------------------
Gradle 6.2.1
------------------------------------------------------------

Build time:   2020-02-24 20:24:10 UTC
Revision:     aacbcb7e587faa6a8e7851751a76183b6187b164

Kotlin:       1.3.61
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          1.8.0_152 (Oracle Corporation 25.152-b16)
OS:           Mac OS X 10.15.4 x86_64

orrincn:wrapper orrin$

Next, you can use gradlew like you use gradle, for example, you need to perform the install task

orrincn:wrapper orrin$ ./gradlew install
Starting a Gradle Daemon (subsequent builds will be faster)

> Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install

> Task :compile
[phase:execution] compile :doFirst()

> Task :test
[phase:execution] test:doLast()

> Task :packaging
[phase:execution] packaging:doLast()

> Task :install
[phase:execution] install:doFirst()
[phase:execution] install:installObject (test.jar)
[phase:execution] install:checkObject   (test.jar)
[phase:execution] install:doLast()

BUILD SUCCESSFUL in 3s
4 actionable tasks: 4 executed
orrincn:wrapper orrin$ 

summary

Gradle Wrappers are more like a chicken rib function for users who are familiar with setting, because the setting of gradle itself is very simple, but for team development, if we can wait for transparency and standardization, it is a good way for the project, and also a small practice for development environment standardization when we implement DevOps.

Posted by jfontein on Sun, 05 Apr 2020 18:29:11 -0700