标签:
引言:通过上一篇的论述,我们知道gradle脚本是如假包换的groovy代码,但是这个groovy代码是运行在他的上下文环境里面的,学名叫语义模型。这一篇我们就来看看他的语义模型到底是什么。
gradle的下载安装并没有什么出乎意料的东西,进入他的官网(http://gradle.org/),下载gradle压缩包,解压,配置bin路径到Path即可。
我安装在c盘下,进入目录C:\gradle-2.4\docs\userguide,打开userguide.pdf文件,这个就是gradle的官方帮助文档。
我们试下第六章的hello world,创建一个文件夹,在里面创建一个文件build.gradle.输入以下的代码:
task hello { doLast { println ‘Hello world!‘ } }
命令行输入gradle -q hello,
打印
Hello world!
看文档对这个Demo的说明:We call this build.gradle
file a build script, although strictly speaking it is a build configuration script, as we will see later. The build script defines a project and its tasks.按照帮助文档的内容,大概可以猜测出来,每一个build.gradle文件都可以实例一个project类。用谷歌搜索下gradle和project就可以应证这个判断。
我们打开C:\gradle-2.4\docs\javadoc\org\gradle\api找到Project.html,这里可以找到更多的信息。
This interface is the main API you use to interact with Gradle from your build file. 你的build.gradle就是来配置这个接口的!看下文档对Project生命周期的
There is a one-to-one relationship between a Project
and a "build.gradle"
file. During build initialisation, Gradle assembles a Project
object for each project which is to participate in the build, as follows:
Settings
instance for the build. 首先创建一个Settings的实例。"settings.gradle"
script, if present, against the Settings
object to configure it. 如果当前目录有settings.gradle,那么用这个文件来配置Settings实例。Settings
object to create the hierarchy of Project
instances. 用Settings实例来创建Project的继承关系。Project
by executing its "build.gradle"
file, if present, against the project. The projects are evaluated in breadth-wise order, such that a project is evaluated before its child projects. This order can be overridden by calling evaluationDependsOnChildren()
or by adding an explicit evaluation dependency usingevaluationDependsOn(String)
. 最后,如果项目中存在build.gradle就通过它来执行每个Project。项目中,项目分别依次横向执行,以此子项目一定在父项目之后执行。这个规则可以通过调用evaluationdependsonchildren()或加入一个明确的依赖usingevaluationdependson(String)。至此,project中所有的方法,理论上我们在gradle脚本中都可以调用。我们试着编译一个java方法,打开帮助文档chaper 7。
创建一个build.gradle文件,和一个src文件。build.gradle文件中输入:
apply plugin:‘java‘
src中创建com/test/Main.java,输入java代码:
package com.test; public class Main { public static void main(String[] args) { System.out.println("hello gradle!"); } }
在命令行中输入 gradle build
13051041@CNHQ-13051041T /cygdrive/d/gradleDemo/javaDemo # gradle build :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar :assemble :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build BUILD SUCCESSFUL Total time: 2.562 secs
已经可以编译成功了!我们先看下apply是个什么函数,在project的帮助文档中搜索下,发现这是另外一个接口PluginAware提供的方法,详情如下:
apply void apply(Map<String,?> options) Applies a plugin or script, using the given options provided as a map. Does nothing if the plugin has already been applied. The given map is applied as a series of method calls to a newly created ObjectConfigurationAction. That is, each key in the map is expected to be the name of a method ObjectConfigurationAction and the value to be compatible arguments to that method. The following options are available: from: A script to apply. Accepts any path supported by Project.uri(Object). plugin: The id or implementation class of the plugin to apply. to: The target delegate object or objects. The default is this plugin aware object. Use this to configure objects other than this object. Parameters: options - the options to use to configure and ObjectConfigurationAction before “executing” it
看的出是导入了一个叫java的插件,project中对插件的定义是:插件可以用于模块化和重用项目配置。
标签:
原文地址:http://www.cnblogs.com/chenjie0949/p/4757702.html