有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com/1.12/userguide/userguide.html

本文原创,转载请注明出处:http://blog.csdn.net/maosidiaoxian/article/details/41006627

关于我对Gradle的翻译,以Github上的项目及http://gradledoc.qiniudn.com 上的文档为准。如有发现翻译有误的地方,将首先在以上两个地方更新。因时间精力问题,博客中发表的译文基本不会同步改动。

第十四章. 教程 - 杂七杂八

14.1.
创建文件夹

有一个常见的情况是,多个任务都依赖于某个文件夹的存在。当然。你能够在这些任务的開始增加mkdir来解决问题。

但这是种臃肿的解决方法。这里有一个更好的解决方式
(仅适用于这些须要这个文件夹的任务有着dependsOn的关系的情况):

演示样例 14.1. 使用 mkdir 创建文件夹

build.gradle

classesDir = new File('build/classes')
task resources << {
classesDir.mkdirs()
// do something
}
task compile(dependsOn: 'resources') << {
if (classesDir.isDirectory()) {
println 'The class directory exists. I can operate'
}
// do something
}

gradle
-q compile
的输出结果

> gradle -q compile
The class directory exists. I can operate

14.2.
Gradle 属性和系统属性

Gradle 提供了很多方式将属性加入到您的构建中。 从Gradle 启动的 JVM,你能够使用-D命令行选项向它传入一个系统属性。 Gradle命令的-D选项和java命令的-D选项有着相同的效果。

此外,您也能够通过属性文件向您的project对象加入属性。您能够把一个gradle.properties文件放在
Gradle 的用户主文件夹 (默觉得USER_HOME/.gradle
。或您的项目文件夹中。对于多项目构建。您能够将gradle.properties文件放在不论什么子项目的文件夹中。通过project对象,能够訪问到gradle.properties里的属性。用户的主文件夹中的属性文件比项目文件夹中的属性文件更先被訪问到。

你也能够通过使用-P命令行选项来直接向您的项目对象加入属性。在很多其它的使用方法中。您甚至能够通过系统和环境属性把属性直接传给项目对象。

比如,假设你在一个持续集成server上执行构建。但你没有这台机器的管理员权限,而你的构建脚本须要一些不能让其它人知道的属性值。那么,您就不能使用-P选项。

在这样的情况下,您能够在项目管理部分
(对普通用户不可见) 加入一个环境属性。[6]假设环境属性遵循ORG_GRADLE_PROJECT_propertyName=
somevalue
的模式。这里的propertyName会被加入到您的项目对象中。对系统属性我们也支持同样的机制。唯一的差别是,它是org.gradle.projectpropertyName的模式。

通过gradle.properties文件,你还能够设置系统属性。假设此类文件中的属性有一个systemProp.的前缀,该属性和它的值会被加入到系统属性,且不带此前缀。在多项目构建中,除了在根项目之外的不论什么项目里的 systemProp.属性集都将被忽略。也就是。仅仅有根项目gradle.properties文件中的systemProp.属性会被作为系统属性。

未例 14.2. 使用 gradle.properties 文件设置属性

gradle.properties

gradlePropertiesProp=gradlePropertiesValue
systemProjectProp=shouldBeOverWrittenBySystemProp
envProjectProp=shouldBeOverWrittenByEnvProp
systemProp.system=systemValue

build.gradle

task printProps << {
println commandLineProjectProp
println gradlePropertiesProp
println systemProjectProp
println envProjectProp
println System.properties['system']
}

gradle
-q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
的输出结果

> gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
commandLineProjectPropValue
gradlePropertiesValue
systemPropertyValue
envPropertyValue
systemValue

14.2.1.检查项目的属性

当你要使用一个变量时。你能够仅通过其名称在构建脚本中訪问一个项目的属性。假设此属性不存在,则会引发异常。而且构建失败。假设您的构建脚本依赖于一些可选属性,而这些属性用户可能在比方 gradle.properties 文件里设置,您就须要在訪问它们之前先检查它们是否存在。你能够通过用法hasProperty('propertyName') 来进行检查,它返回truefalse

14.3.
使用外部构建脚本配置项目

您能够使用外部构建脚本来配置当前项目。Gradle 构建语言的全部内容在外部脚本中也能够使用。

您甚至能够在外部脚本中应用其它脚本。

演示样例14.3. 使用外部构建脚本配置项目

build.gradle

apply from: 'other.gradle'

other.gradle

println "configuring $project"
task hello << {
println 'hello from other script'
}

gradle
-q hello
的输出结果

> gradle -q hello
configuring root project 'configureProjectUsingScript'
hello from other script

14.4.
配置随意对象

您能够用下面很易理解的方式配置随意对象。

演示样例 14.4. 配置随意对象

build.gradle

task configure << {
pos = configure(new java.text.FieldPosition(10)) {
beginIndex = 1
endIndex = 5
}
println pos.beginIndex
println pos.endIndex
}

gradle
-q configure
的输出结果

> gradle -q configure
1
5

14.5. 使用外部脚本配置随意对象

你还能够使用外部脚本配置随意对象

演示样例14.5. 使用脚本配置随意对象

build.gradle

task configure << {
pos = new java.text.FieldPosition(10)
// Apply the script
apply from: 'other.gradle', to: pos
println pos.beginIndex
println pos.endIndex
}

other.gradle

beginIndex = 1;
endIndex = 5;

gradle
-q configure
的输出结果

> gradle -q configure
1
5

14.6.
缓存

为了提高响应速度。默认情况下 Gradle 会缓存全部已编译的脚本。

这包含全部构建脚本。初始化脚本和其它脚本。你第一次执行一个项目构建时, Gradle 会创建.gradle文件夹。用于存放已编译的脚本。下次你执行此构建时。
假设该脚本自它编译后没有被改动。Gradle 会使用这个已编译的脚本。否则该脚本会又一次编译,并把最新版本号存在缓存中。假设您通过--recompile-scripts选项执行
Gradle ,会丢弃缓存的脚本,然后又一次编译此脚本并将其存在缓存中。通过这样的方式,您能够强制 Gradle 又一次生成缓存。


[6TeamcityBamboo就是提供这个功能的
CI server的样例。

Gradle 1.12 翻译——第十四章. 教程 - 杂七杂八的更多相关文章

  1. Gradle 1.12 翻译——第十六章. 使用文件

    有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...

  2. Gradle 1.12翻译——第十九章. Gradle 守护进程

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  3. Gradle 1.12 翻译——第十五章. 任务详述

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  4. Gradle 1.12 翻译——第十二章 使用Gradle 图形用户界面

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  5. Gradle 1.12用户指南翻译——第二十四章. Groovy 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  6. Gradle 1.12 翻译——第十八章. 日志

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  7. Gradle 1.12用户指南翻译——第三十四章. JaCoCo 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  8. Gradle 1.12用户指南翻译——第四十四章. 分发插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  9. Gradle 1.12用户指南翻译——第六十四章. 发布到Ivy(新)

    其他章节的翻译请参见:http://blog.csdn.net/column/details/gradle-translation.html翻译项目请关注Github上的地址:https://gith ...

随机推荐

  1. SQLHelper简单版(基础版)

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  2. Activity 启动模式

    Activity的启动模式有四种,分别是standard.singleTop.singleTask.singleInstance.   Android是通过回退栈的模式来管理Activity实例的.栈 ...

  3. zoj 1083 Frame Stacking

    其实就是一个拓补排序.(动态记录第i个之上的j存不存在,反过来就是第j个之下的i) 首先确立每个框的位置(题目明确说了每一边都不会被完全覆盖)./*可以通过搜索,搜索到该框的所有四个角*/||如果题目 ...

  4. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  5. thinkphp phpexcel导出

    近期做一个项目涉及到商品信息的批量导出与导入,遂记录了下来,框架是tp框架3.2.3(tp5.0性质是一样的,无非是加载方法与所放目录不一样罢了),运用的是phpexcel,闲话不多说,上代码 1.首 ...

  6. Sublime 配置&插件推荐

    sublime 配置&插件推荐 Sublime编辑器的新鲜特性同时选中多个 先选中一个 再Command + D Command + P @搜索函数 #搜索关键字迷你地图 安装package ...

  7. 国内BI工具/报表工具厂商简介

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  8. javascript学习笔记(window .alert 是什么)

    <script language="javascript"> var abc="25"; window .alert(abc); </scri ...

  9. 将dll放进exe[.Net]

    原文:将dll放进exe[.Net] 两种方案: 1.使用ILMerge工具. 缺点:需离开工程,使用第三方工具(ILMerge). 2.将dll作为Resource放进exe,exe执行时动态加载( ...

  10. python字符串操作总结

    python中有各种字符串操作,一开始python有个专门的string模块,要使用需先import string.后来从python2.0开始,string方法改用str.method()形式调用, ...