发布日期:2016-08-05 13:36:15

Gradle命令学习

  • Gradle的build.gradle其实是groovy script.(Gradle expresses its build files in Groovy. )
  • Gradle的基本build活动就是tasks.可以通过Gradle tasks命令来列出当前Root Project中可使用的任务。

Gradle tasks可以列出当前build.gradle中所有可以使用的tasks,包括自定义的task。主要有几类任务:Build任务,Build Stepup任务, Documentation任务 ,Help任务, Verification任务,自定义任务。

E:\test>gradle tasks
: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. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
components - Displays the components produced by root project 'test'. [incubating]
dependencies - Displays all dependencies declared in root project 'test'.
dependencyInsight - Displays the insight into a specific dependency in root project 'test'.
help - Displays a help message.
model - Displays the configuration model of root project 'test'. [incubating]
projects - Displays the sub-projects of root project 'test'.
properties - Displays the properties of root project 'test'.
tasks - Displays the tasks runnable from root project 'test'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

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.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

Task声明和Task Action

task %taskName% <<{
       println "This is task action"
}

或者
task %taksName%
%taskName << {
     println " this is task action"
}

gradle执行build的lifecycle phases: initialization(决定哪些projects参与build) < configration ( Task任务对象被组装到一个内部对象模型)< execution (任务安在dependency关系顺序执行)

 所有Task都是对象,继承 DefaultTask。DefaultTask有这些方法:

  • dependsOn(task)
  • doFirst(closure)
  • doLast(closure)
  • onlyIf(closure)

DefaultTask有这些属性:

  • didWork
  • enabled
  • path
  • logger (DEBUG, INFO, LIFECYCLE, WARN, QUIET, ERROR)
  • logging
  • description
  • temporaryDir
  • Dynamic properties

每个Task都一个Task Type。比如一些常用类型:Copy, Jar, JavaExec等, 我们也可以自定义task Type。 所有自定义的task type都必须继承DefaultTask或其子类。

自定义的Task Type的代码可以放在4个地方,最简单的就是在build script中,建议还是放在buildSrc中更易管理。

 

实际使用中我们很少编写自己的Task Type, 因为已经有很多plugin编写好了,只要学会使用就可以了。

发表评论