Gradle Task 入门 1
2020-07-29 本文已影响0人
这个世界是虚拟的
Task 在哪?
//一般我们使用的task 都是使用Project 下的 task 方法, 具体可以进入 org.gradle.api.project 进行查看
package org.gradle.api
/**
* <p>Creates a {@link Task} with the given name and adds it to this project. Calling this method is equivalent to
* calling {@link #task(java.util.Map, String)} with an empty options map.</p>
*
* <p>After the task is added to the project, it is made available as a property of the project, so that you can
* reference the task by name in your build file. See <a href="#properties">here</a> for more details</p>
*
* <p>If a task with the given name already exists in this project, an exception is thrown.</p>
*
* @param name The name of the task to be created
* @return The newly created task object
* @throws InvalidUserDataException If a task with the given name already exists in this project.
*/
Task task(String name) throws InvalidUserDataException;
Task 简单使用
声明Task
定义一个Task, 并打印版本
task printVersion {
//定义优先执行的方法
doFirst {
println "1 preparing print version"
}
doLast {
println "2 Version : $version"
}
}
另一种定义方法,这里需要注意的是,printVersion 必须已经定义好了, 比如上面的定义和下面的代码一起使用。 否则会有报错,Gradle不会自动识别printVersion 为一个task
printVersion.doLast {
println "3 using Logger print version"
logger.quiet("Version : $version")
}
声明带参数的Task
//参数的方式
task printVersionWithParam(group: 'versioning', description: 'prints version') {
doLast {
println "balabala ... ..."
}
}
//setter的方式
task printVersionUsingSetter() {
group = 'versioning'
description = 'prints version'
doLast {
println "balabala ... ..."
}
}
task 供使用的参数有以下参数,比较简单,就不多说了
String TASK_NAME = "name";
String TASK_DESCRIPTION = "description";
String TASK_GROUP = "group";
String TASK_TYPE = "type";
String TASK_DEPENDS_ON = "dependsOn";
String TASK_OVERWRITE = "overwrite";
String TASK_ACTION = "action";
String TASK_CONSTRUCTOR_ARGS = "constructorArgs";
运行 task
// 查看所有task
gradle(w) -q taks --all
// 执行你定义个task,以刚才我们定义的一个task为例
gradle(w) printVersionUsingSetter