Gradle这篇文章就够用了08wrapper的使用

编程

Gradle Wrapper

Gradle Wrapper实际上就是一个脚本,使用它可以下载和使用指定版本的gradle,根据需要进行在使用之前进行下载,有效避免本地机器的设定等环境一致性问题。

虽然gradle的安装已经非常简单,但是使用gradle wrapper是的开发这能够以一种更为标准化的方式创建gradle项目。

使用方式

Gradle Wrapper的使用方式主要是按照如下步骤:

  • Step 1: 下载gradle
  • Step 2: 将下载的gradle解压并存储至GRADLE_USER_HOME所指定的目录
  • Step 3: 使用解压的gradle

graph LR

GradleBuild -- 1.Download distribution --> Server

GradleBuild -- 1.Store and unpack distribution --> GradleUserHome

GradleUserHome -- 3.Use distribution --> GradleBuild

事前准备

使用下面的代码示例,内容不再说明,具体说明信息请参看:

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

通过gradle tasks可以确认到在Build Setup tasks中有wrapper这样一个内建的任务。

orrincn:wrapper orrin$ gradle tasks

...省略

------------------------------------------------------------

All tasks runnable from root project

------------------------------------------------------------

Build Setup tasks

-----------------

init - Initializes a new Gradle build.

wrapper - Generates Gradle wrapper files.

...省略

BUILD SUCCESSFUL in 0s

1 actionable task: 1 executed

orrincn:wrapper orrin$

wrapper的说明是这个任务是用来创建gradle wrapper文件,接下里我们来看一下具体创建了那些文件

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$

文件说明:

  • gradlew:linux或者Unix下用于执行wrapper命令的Shell脚本
  • gradlew.bat:Windows下用于执行wrapper命令的批处理脚本
  • gradle-wrapper.jar:用于下载Gradle的相关代码实现
  • gradle-wrapper.properties:wrapper所使用的配置信息,比如gradle的版本等信息

  • 缺省状况下的gradle-wrapper.properties的信息如下

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

GRADLE_USER_HOME可以通过环境变量的方式设定,缺省的GRADLE_USER_HOME为 HOME/.gradle,根据这里的信息可以看到,下载的gradle将会解压到

HOME/.gradle,根据这里的信息可以看到,下载的gradle将会解压到{HOME}/.gradle/wrapper/dists

使用场景

当前系统安装的gradle的版本为6.2.0,但是在当前的project里面希望使用6.2.1的gradle,这时gradle wrapper就可以一显身手了

设定wrapper使用的gradle版本

可以使用命令来修改wrapper所使用的gradle版本信息

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$

可以看到,实际这条命令只是修改了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

当然设定也可以通过gradle-distribution-url选项或者直接修改配置文件来更快实现,至于具体版本,gradle的安装文件包符合:

gradle-版本信息-bin|all.zip的格式,按照这种格式即可

wrapper使用

只需要设定下载url所指定的gradle版本信息,wrapper的准备就已经就绪,随便使用任何一个wrapper的命令,第一次执行都会首先下载gradle并解压到${HOME}/.gradle/wrapper/dists,在在执行之前我们可以看到目前还没有任何内容

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$

gradle wrapper之所以有被叫做wrapper,就是因为封了一层,所以gradle的基本命令在这里都可以使用,这里直接使用上文确认gradle版本所用的-v

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$

所以看到第一次执行有Downloading Downloading https://services.gradle.org/distributions/gradle-6.2.1-bin.zip的动作,而具体的版本正是之前所设定的,gradle的下载和解压对我们来说都是透明的,为什么wrapper能找到这个6.2.1的版本呢

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

gradle-6.2.1-bin

orrincn:wrapper orrin$

可以看到,wrapper把它下载并解压到这里了,当然也可以使用系统已经安装的gradle,这种方式直接设定GRADLE_USER_HOME即可。而且下载和设定的动作只会发生一次,除非你手动再次将其删除,现在再次使用gradlew就会清晰的多了

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$

接下来就可以想使用gradle一样使用gradlew了,比如需要执行install的任务

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$

总结

gradle wrapper对于较为熟悉设定用户来说,似乎更像一个鸡肋的功能,因为gradle本身的设定就非常简单,但是对于团队开发来说,这种事情如果能够坐等透明化和标准化对于项目来说自然是很不错的一个方式,也是我们在推行DevOps时对于开发环境标准化的一个小的实践。

以上是 Gradle这篇文章就够用了08wrapper的使用 的全部内容, 来源链接: utcz.com/z/515152.html

回到顶部