基于groovy语言的DSL编程基础(项目构建)
2016-10-09 23:33
573 查看
Gradle是一种基于依赖的编程语言,你可以在已有的task中自定义task或者依赖规则。对比我们最常用的语言,比如java、Object-C,Gradle就像同时包含了配置虚拟机、字节码解释规则、code语法。Gradle会让这些task按照顺序执行,且只执行一次。有些build tools工具会在任何一个task执行前构建完成一个基于依赖的task队列,便于完成它指定的编译任务,比如com.android.tools.build。
一次Gradle构建包含三个阶段:Initialization(初始化)、Configuration(配置)、Execution(运行)
1)Initialization
Gradle支持同时至少一个项目的构建,在Initialization阶段,Gradle决定哪个项目可以参与构建(build),并为它们分别创建一个Project实例。
2)Configuration
参与构建的所有项目的build script会被执行(从Gradle 1.4开始,有关联的项目才会被配置)
3)Execution
Gradle划分完成在配置阶段被创建的即将执行的task,通过gradle命令参数和当前目录确定这些task是否应该得到执行。
Setting文件
Gradle确定一个默认名为setting.gradle的setting文件,这个文件会在Initialization阶段执行。同时构建多个项目时,必须在所有项目的顶层目录中放置一个setting.gradle文件,这个文件用来确定哪个项目参与接下来的构建过程。如果只有个项目,可以没有setting.gradle文件。
单项目构建举例:
settings.gradle
运行命令:gradle test testBoth
附注:在一段Gradle脚本中,可以通过一个project对象实现对属性的访问和方法的调用。同样的,在setting文件中,可以通过setting(比如Setting类对象)对象实现对属性的访问和方法的调用。
一次Gradle构建包含三个阶段:Initialization(初始化)、Configuration(配置)、Execution(运行)
1)Initialization
Gradle支持同时至少一个项目的构建,在Initialization阶段,Gradle决定哪个项目可以参与构建(build),并为它们分别创建一个Project实例。
2)Configuration
参与构建的所有项目的build script会被执行(从Gradle 1.4开始,有关联的项目才会被配置)
3)Execution
Gradle划分完成在配置阶段被创建的即将执行的task,通过gradle命令参数和当前目录确定这些task是否应该得到执行。
Setting文件
Gradle确定一个默认名为setting.gradle的setting文件,这个文件会在Initialization阶段执行。同时构建多个项目时,必须在所有项目的顶层目录中放置一个setting.gradle文件,这个文件用来确定哪个项目参与接下来的构建过程。如果只有个项目,可以没有setting.gradle文件。
单项目构建举例:
settings.gradle
println 'This is executed during the initialization phase.'build.gradle
println 'This is executed during the configuration phase.' task configured { println 'This is also executed during the configuration phase.' } task test << { println 'This is executed during the execution phase.' } task testBoth { doFirst { println 'This is executed first during the execution phase.' } doLast { println 'This is executed last during the execution phase.' } println 'This is executed during the configuration phase as well.' }
运行命令:gradle test testBoth
> gradle test testBoth This is executed during the initialization phase. This is executed during the configuration phase. This is also executed during the configuration phase. This is executed during the configuration phase as well. :test This is executed during the execution phase. :testBoth This is executed first during the execution phase. This is executed last during the execution phase. BUILD SUCCESSFUL Total time: 1 secs
附注:在一段Gradle脚本中,可以通过一个project对象实现对属性的访问和方法的调用。同样的,在setting文件中,可以通过setting(比如Setting类对象)对象实现对属性的访问和方法的调用。
相关文章推荐
- 使用Gradle构建基于Groovy语言的经典Java EE应用
- 基于JVM的动态语言Groovy 基础知识汇总
- LinuxC/C++编程基础(2) 基于boost::any构建数据容器
- 基于ArcGIS的Python编程秘笈笔记(一):面向ArcGIS的Python语言基础
- 基于Java语言的安卓编程之二十四文件打开
- C语言基础编程之进制转化
- c#语言基础编程—string
- 第5章 Kotlin语言基础 《Kotin 编程思想·实战》
- c#基础语言编程-多态
- 基于Win Socket的网络编程基础
- spark基础(三)------------------------使用maven构建一个基于scala的spark应用程序。
- python 编程语言基础技术框架
- 基于Java语言构建区块链(一)—— 基本原型
- 应用 Rational 工具简化基于 J2EE 的项目第 7 部分 :构建与演示
- 导入开源库到基于Android Studio构建的项目中
- 图文介绍IntelliJ IDEA 创建基于Maven构建的Web项目
- 构建基础的SpringMVC+Hibernate+SpringloC项目
- Linux下C语言编程--基础知识
- 基于Java语言构建区块链(二)—— 工作量证明
- java网络编程基础夯实07-基于TCP/UDP的Socket编程(单线程)