第一模块:课程介绍
课程名称:新一代构建工具gradle
课程章节:第4章 高级应用
主讲老师:skyding
第二模块:课程内容
熟悉自定义任务的创建,了解gradle的生命周期
第三模块:课程收获
使用任务自定义创建目录
- 声明任务
task makeJavaDir(){
}
我们在里面定义一些目录
def paths = ['src/main/java11']
再声明一个闭包,用来进行目录的创建
def createDir = {
path ->
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
}
}
闭包创建完成后,就可以创建动作了。
在doFirst后执行这个任务
task makeJavaDir(){
def paths = ['src/main/java']
doFirst{
paths.forEach(createDir);
}
}
点击绿色小箭头,开始执行任务
注意这个顺序,createDir必须要在makeJavaDir前面,不然会报找不到方法的错误。
执行完看下效果
同意的,自定义任务,在右侧也可以看到。
到这里,我们就完成了一个创建文件夹的自定义任务了。
任务依赖
我们可以让任务之间进行依赖,比如还有一个创建web目录的任务
task makeWebDir(){
dependsOn 'makeJavaDir'
def paths = ['src/main/webapp']
doLast{
paths.forEach(createDir);
}
}
我们执行这个任务的时候,会不会执行JavaDir的任务呢?
点击执行查看下效果
23:23:36: 正在执行 'makeWebDir'…
> Task :makeJavaDir
> Task :makeWebDir
BUILD SUCCESSFUL in 213ms
2 actionable tasks: 2 executed
23:23:37: 执行完成 'makeWebDir'。
它会执行第一个,然后再去执行第二个任务。
后续可以尝试把这些东西做成插件之类的。
其他
除了在右侧查看任务之外,还可以运行gradle命令来查看
gradle tasks --all
运行结果
PS D:\WorkSpace\SpringSpace\todo> gradle tasks --all
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project 'todo'
------------------------------------------------------------
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 'todo'.
dependencies - Displays all dependencies declared in root project 'todo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'todo'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of root project 'todo'.
projects - Displays the sub-projects of root project 'todo'.
properties - Displays the properties of root project 'todo'.
tasks - Displays the tasks runnable from root project 'todo'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Other tasks
-----------
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
components - Displays the components produced by root project 'todo'. [deprecated]
dependentComponents - Displays the dependent components of components in root project 'todo'. [deprecated]
makeJavaDir
makeWebDir
model - Displays the configuration model of root project 'todo'. [deprecated]
prepareKotlinBuildScriptModel
processResources - Processes main resources.
processTestResources - Processes test resources.
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
当然使用idea会更加直观,但是在只有终端的情况下,命令也很实用的。
设置任务组和描述
还可以对任务进行分组,只要在任务的地方加上一个group就好了。
这样我们的任务就在myTask这个分组下了