阿男的小窝

View the Project on GitHub

Gradle学习笔记・01・Gradle的安装与使用

Gradle的下载地址在这里1

下载解压后,在.bash_profile中配置相关路径:

# Gradle
export GRADLE_HOME=/Users/weli/projs/gradle-2.14
export PATH=$PATH:$GRADLE_HOME/bin

测试Gradle可以使用:

$ gradle -version

------------------------------------------------------------
Gradle 2.14
------------------------------------------------------------

Build time:   2016-06-14 07:16:37 UTC
Revision:     cba5fea19f1e0c6a00cc904828a6ec4e11739abc

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)
OS:           Mac OS X 10.11.5 x86_64

写一个build脚本:

task helloWorld << {
	println 'Hello, world!'
}

执行脚本:

$ gradle helloWorld
:helloWorld
Hello, world!

BUILD SUCCESSFUL

Total time: 3.921 secs

写一个依赖关系,文件命名为build.gradle

task hello << {
	println 'Hello, '
}

task helloWorld(dependsOn: hello) << {
	println 'world!'
}

执行任务:

$ gradle helloWorld
:hello
Hello,
:helloWorld
world!

BUILD SUCCESSFUL

Total time: 3.183 secs
cute:learn-gradle weli$ gradle -q helloWorld
Hello,
world!

此外,现在使用「SDKMAN!」2也可以直接安装gradle了:

$ sdk install gradle
==== BROADCAST =================================================================
* 20/12/17: Gradle 4.4.1 released on SDKMAN! #gradle
* 13/12/17: Kotlin 1.2.10 released on SDKMAN! #kotlin
* 12/12/17: Kscript 2.3.0 released on SDKMAN! #kscript
================================================================================

Downloading: gradle 4.4.1

In progress...

######################################################################## 100.0%

Installing: gradle 4.4.1
Done installing!

Do you want gradle 4.4.1 to be set as default? (Y/n):

Setting gradle 4.4.1 as default.


ATTENTION: A new version of SDKMAN is available...

The current version is 5.6.0+287, but you have 5.1.4+78.

Would you like to upgrade now? (Y/n)

Updating SDKMAN...
######################################################################## 100.0%


Successfully upgraded SDKMAN!

Open a new terminal to start using SDKMAN 5.6.0+287.

To join our BETA channel, simply follow the instructions on:

   http://sdkman.io/install.html

Enjoy!!!
  1. http://gradle.org/gradle-download/ 

  2. http://sdkman.io/