码迷,mamicode.com
首页 > 其他好文 > 详细

Gradle Task入门演示二

时间:2015-09-22 20:35:21      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:

Gradle Task入门演示二

https://docs.gradle.org/current/userguide/more_about_tasks.html

Defining tasks

有多种方式可以定义task,现在再来看下面这几种方式,

task(helloTask) << {
    println "hello"
}

task的名字helloTask,还可以使用引号,

task(‘helloTask2‘) << {
    println "hello"
}

还可以使用tasks.create 定义一个task。

tasks.create(name: ‘helloTask3‘) << {
    println ‘hello‘
}


Locating tasks

定位task

task printTaskPath << {
    println tasks.getByPath(‘hello‘).path
    println tasks.getByPath(‘:hello‘).path
}

执行task输出,

:hello

:hello


Configuring tasks

配置task。如下声明和定义类型为Copy的task,同时定义myCopy的属性。

Copy myCopy = task(myCopy, type: Copy)
myCopy.from ‘resources‘
myCopy.into ‘target‘
myCopy.include(‘**/*.txt‘, ‘**/*.xml‘, ‘**/*.properties‘)

上面这种方式时最平常的方式来定义属性,另外一种方式,这种方式使用闭包。

task myCopy(type: Copy)

myCopy {
   from ‘resources‘
   into ‘target‘
   include(‘**/*.txt‘, ‘**/*.xml‘, ‘**/*.properties‘)
}

还有如下方式使用闭包定义属性,

task copy(type: Copy) {
   from ‘resources‘
   into ‘target‘
   include(‘**/*.txt‘, ‘**/*.xml‘, ‘**/*.properties‘)
}


Order task with mustRunAfter

使用mustRunAfter来定义task的执行顺序,如下,

task pre << {
    println ‘pre‘
}

task after << {
    println ‘after‘
}

after.mustRunAfter pre

执行task,

?  Gradle-Showcase  gradle -q after pre
pre
after

可以看到task是按照 mustRunAfter定义的顺序执行的。


Overwriting a task

当定义相同名称的task时,可以使用overwrite重写前一个task。

task copy(type: Copy)

task copy(overwrite: true) << {
    println(‘I am the new one.‘)
}

=====END=====

Gradle Task入门演示二

标签:

原文地址:http://my.oschina.net/xinxingegeya/blog/509760

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!