Gradle学习笔记:Gradle工具安装及环境配置
使用Gradle自动化构建项目
Gradle是新一代的自动化构建工具。Gradle可以完成Ant、Maven的所有工作,甚至整合Ant或Maven的功能。
优点如下:
Gradle工具下载:https://services.gradle.org/distributions/
下载Gradle时有3个选择:源代码包(文件名包含src)、二进制文件包(文件名包含bin)和完整包(文件名包含all)。通常建议下载完整包,该包内包含了Gradle的源代码、二进制文件和文档。
下载Gradle
gradle-4.10.2-all.zip
下载地址:https://downloads.gradle-dn.com/distributions/gradle-4.10.2-all.zip
环境配置
解压到这个目录:D:\6.CommonTools\gradle-4.10.2-all ,解压后的文件结构:
将 D:\6.CommonTools\gradle-4.10.2-all\gradle-4.10.2\bin 添加到path
运行是否安装成功?
运行命令:gradle
D:\6.CommonTools\gradle-4.10.2-all\gradle-4.10.2\bin>gradle.bat
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* What went wrong:
Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at https://docs.gradle.org/4.10.2/userguide/gradle_daemon.html
Please read the following process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for 1048576KB object heap
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
提示访问链接: https://docs.gradle.org/4.10.2/userguide/gradle_daemon.html
解决方法:
进入 C:\Users\用户名.gradle 目录,手动新建一个文件:gradle.properties,gradle.properties文件内容添加 org.gradle.daemon=false 一行
再次打开cmd终端,运行ok,如下:
Gradle构建文件和创建任务
一个典型的Gradle项目层次结构:
Gradle构建文件本质上是一个Groovy源文件,该文件的语法完全符合Groovy语法。
Demo示例
创建Task最简单的方式
创建一个文件:build.gradle,内容为:
// 定义hello1任务,传入的代码块负责配置该任务
task hello1 {
println "配置第一个任务"
}
输入:gradle build.gradle
虽然输出了,但是报了一个异常,如何解决?
通过下面方式运行:
先执行:gradle tasks
再执行:gradle build
运行后不会提示错误信息。
或者 输入从命令 gradle hello1 运行
为Task添加Action
// 定义hello1任务,传入的代码块负责配置该任务
task hello1 {
println "配置第一个任务"
}
// 定义hello2任务,传入的代码块负责配置该任务
task hello2 {
println "配置第二个任务"
// 调用doLast方法为Task添加Action
doLast {
// 使用循环
for(i in 0..<5){
println i
}
}
// 调用doFrist方法为Task添加Action
doFirst {
// 定义变量
def s = "fkjava.org"
// 输出字符串模板
println "开始执行第二个任务:$s"
}
}
运行结果:
若此时输入 gradle hello1 运行,结果如下:
// 定义hello1任务,传入的代码块负责配置该任务
task hello1 {
println "配置第一个任务"
}
// 定义hello2任务,传入的代码块负责配置该任务
task hello2 {
println "配置第二个任务"
// 调用doLast方法为Task添加Action
doLast {
// 使用循环
for(i in 0..<5){
println i
}
}
// 调用doFrist方法为Task添加Action
doFirst {
// 定义变量
def s = "fkjava.org"
// 输出字符串模板
println "开始执行第二个任务:$s"
}
}
// 调用Project的tasks属性(TaskContainer)的create方法来创建task
tasks.create(name: 'showTasks') {
doLast{
// 查看Project的tasks属性的类型
println "tasks属性的类型:${tasks.class}"
// 遍历tasks属性
tasks.each {e ->
println e
}
}
}
// 定义hello1任务,传入的代码块负责配置该任务
task hello1 {
println "配置第一个任务"
}
// 定义hello2任务,传入的代码块负责配置该任务
task hello2 {
println "配置第二个任务"
// 调用doLast方法为Task添加Action
doLast {
// 使用循环
for(i in 0..<5){
println i
}
}
// 调用doFrist方法为Task添加Action
doFirst {
// 定义变量
def s = "fkjava.org"
// 输出字符串模板
println "开始执行第二个任务:$s"
}
}
// 调用Project的tasks属性(TaskContainer)的create方法来创建task
tasks.create(name: 'showTasks') {
doLast{
// 查看Project的tasks属性的类型
println "tasks属性的类型:${tasks.class}"
// 遍历tasks属性
tasks.each {e ->
println e
}
}
}
// 调用Project的tasks属性(TaskContainer)的create方法来创建Task
// dependsOn指定该Task依赖hello2,该Task的类型是Copy(文件拷贝)
tasks.create(name: 'fkTask', dependsOn:'hello2', type: Copy) {
from 'books.xml'
into 'dist'
}
E:\20_gradledemo\01\1.2\gradle01\src\main\java\lee\HelloWorld.java
package lee;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
E:\20_gradledemo\01\1.2\gradle01\build.gradle
// 定义hello1任务,传入的代码块负责配置该任务
task hello1 {
println "配置第一个任务"
}
// 定义hello2任务,传入的代码块负责配置该任务
task hello2 {
println "配置第二个任务"
// 调用doLast方法为Task添加Action
doLast {
// 使用循环
for(i in 0..<5){
println i
}
}
// 调用doFrist方法为Task添加Action
doFirst {
// 定义变量
def s = "fkjava.org"
// 输出字符串模板
println "开始执行第二个任务:$s"
}
}
// 调用Project的tasks属性(TaskContainer)的create方法来创建task
tasks.create(name: 'showTasks') {
doLast{
// 查看Project的tasks属性的类型
println "tasks属性的类型:${tasks.class}"
// 遍历tasks属性
tasks.each {e ->
println e
}
}
}
// 调用Project的tasks属性(TaskContainer)的create方法来创建Task
// dependsOn指定该Task依赖hello2,该Task的类型是Copy(文件拷贝)
tasks.create(name: 'fkTask', dependsOn:'hello2', type: Copy) {
from 'books.xml'
into 'dist'
}
// 应用名为'java'的插件,主要为了引入JavaCompile、JavaExec两个任务
apply plugin: 'java'
// 指定该任务的类型为JavaCompile
task compile(type: JavaCompile){
source = fileTree('src/main/java')
classpath = sourceSets.main.compileClasspath
destinationDir = file('build/classes/main')
options.fork = true
options.incremental = true
}
// 指定该任务的类型为JavaExec
task run(type: JavaExec, dependsOn: 'compile'){
classpath = sourceSets.main.runtimeClasspath
// 指定主类为lee.HelloWorld
main = 'lee.HelloWorld'
}
Gradle属性定义
为已有属性指定属性值
// 为Project内置属性指定属性值
version = 1.0
description = 'Project的属性'
// 定义showProps任务,显示Project和Task内置的属性
task showProps {
// 为Task内置属性指定属性值
description = 'Task的属性'
doLast {
println version
// 输出Task属性
println description
// 由于Task和Project都有description属性,
// 因此下面要显式指定访问project的description属性
println project.description
}
}
通过ext添加属性
ext.prop1 = '添加的项目属性一'
ext.prop2 = '添加的项目属性二'
// 使用ext方法,传入代码块来设置属性
ext {
prop3 = '添加的项目属性三'
prop4 = '添加的项目属性四'
}
task showAddedProps {
ext.prop1 = '添加的任务属性一'
ext.prop2 = '添加的任务属性二'
// 使用ext方法,传入代码块来设置属性
ext {
prop3 = '添加的任务属性三'
prop4 = '添加的任务属性四'
}
doLast {
println prop1
println project.prop1
println prop2
println project.prop2
println prop3
println project.prop3
println prop4
println project.prop4
}
}
通过-P选项添加属性
通过JVM参数添加属性
增量式构建
// 定义fileContentCopy任务
task fileContentCopy {
// 定义代表source目录的文件集
def sourceTxt = fileTree("source")
def dest = file('dist.txt')
// 定义该任务的输入、输出
inputs.dir sourceTxt
outputs.file dest
doLast {
// 调用File对象的withPrintWriter方法
dest.withPrintWriter { writer ->
// 调用sourceTxt的each方法遍历每个文件
sourceTxt.each { s ->
writer.write(s.text)
}
}
}
}
Gradle插件和Java、application插件
E:\20_gradledemo\01\1.2\gradle03\build.gradle
// 应用名为'java'的插件,主要为了引入JavaCompile、JavaExec两个任务
apply plugin: 'java'
// 指定该任务的类型为JavaCompile
task compile(type: JavaCompile){
source = fileTree('src/main/java')
classpath = sourceSets.main.compileClasspath
destinationDir = file('build/classes/main')
options.fork = true
options.incremental = true
}
// 指定该任务的类型为JavaExec
task run(type: JavaExec, dependsOn: 'compile'){
classpath = sourceSets.main.runtimeClasspath
// 指定主类为lee.HelloWorld
main = 'lee.HelloWorld'
}
运行结果:
E:\20_gradledemo\01\1.2\gradle03>gradle tasks --all
> Task :tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle03'.
components - Displays the components produced by root project 'gradle03'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle03'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle03'.
dependentComponents - Displays the dependent components of components in root project 'gradle03'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradle03'. [incubating]
projects - Displays the sub-projects of root project 'gradle03'.
properties - Displays the properties of root project 'gradle03'.
tasks - Displays the tasks runnable from root project 'gradle03'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Other tasks
-----------
compile
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
processResources - Processes main resources.
processTestResources - Processes test resources.
run
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
BUILD SUCCESSFUL in 5s
1 actionable task: 1 executed
E:\20_gradledemo\01\1.2\gradle03>
apply plugin: 'java'
apply plugin: 'application'
// 指定run任务执行的主类
mainClassName = 'lee.HelloWorld'
task showProjectProps {
doLast {
properties.each {
e -> println e
}
}
}
// 配置被依赖的源代码路径
sourceSets {
fkframework
}
// 为run任务的classpath增加fkframework的类
run.classpath = sourceSets.main.runtimeClasspath +
files(sourceSets.fkframework.output.classesDir)
// 配置compileJava任务依赖compileFkframeworkJava任务
compileJava.dependsOn compileFkframeworkJava
sourceSets {
main {
// 将fkframework生成的字节码文件的存储路径添加到编译时类路径中
compileClasspath = compileClasspath + files(fkframework.output.classesDir)
}
test {
// 将fkframework生成的字节码文件的存储路径添加到运行时类路径中
runtimeClasspath = runtimeClasspath + files(fkframework.output.classesDir)
}
}
依赖管理
参考:疯狂Android讲义第四版

浙公网安备 33010602011771号