Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before the build. We can use different ways to add the init script to a build. For example we can use the command-line option -I or --init-script, place the script in the init.d directory of our GRADLE_HOME directory or USER_HOME/.gradle directory or place a file init.gradle in our USER_HOME/.gradle directory.

We can also use the apply(from:) method to include such a script in our build file. We can reference a file location, but also a URL. Imagine we place an init script on our company intranet to be shared by all developers, then we can include the script with the apply(from:) method. In the following build file we use this syntax to include the script:

0.apply plugin: 'java'
1.apply from: 'http://intranet/source/quality.gradle'
2. 
3.version = '2.1.1'
4.group = 'com.mrhaki.gradle.sample

The following script is an init script where we add the Checkstyle plugin to projects with the Java plugin and the Codenarc plugin to projects with the Groovy plugin. Because the Groovy plugin extends the Java plugin the Checkstyle plugin is added to the Groovy project as well.

Notice we also add the downloadCheckstyleConfig task. With this task we download from the intranet the Checkstyle configuration that needs to be used by the Checkstyle tasks.

00.// File: quality.gradle
01.allprojects {
02.afterEvaluate { project ->
03.def groovyProject = project.plugins.hasPlugin('groovy')
04.def javaProject = project.plugins.hasPlugin('java')
05. 
06.if (javaProject) {
07.// Add Checkstyle plugin.
08.project.apply plugin: 'checkstyle'
09. 
10.// Task to download common Checkstyle configuration
11.// from company intranet.
12.task downloadCheckstyleConfig(type: DownloadFileTask) {
13.description = 'Download company Checkstyle configuration'
14. 
15.url = 'http://intranet/source/company-style.xml'
16.destinationFile = checkstyle.configFile
17.}
18. 
19.// For each Checkstyle task we make sure
20.// the company Checkstyle configuration is
21.// first downloaded.
22.tasks.withType(Checkstyle) {
23.it.dependsOn 'downloadCheckstyleConfig'
24.}
25.}
26. 
27.if (groovyProject) {
28.// Add Codenarc plugin.
29.project.apply plugin: 'codenarc'
30.}
31.}
32.}
33. 
34.class DownloadFileTask extends DefaultTask {
35.@Input
36.String url
37. 
38.@OutputFile
39.File destinationFile
40. 
41.@TaskAction
42.def downloadFile() {
43.destinationFile.bytes = new URL(url).bytes
44.}
45.}

Code written with Gradle 1.2

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects的更多相关文章

  1. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

  2. Gradle Goodness: Task Output Annotations Create Directory Automatically

    Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...

  3. 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  4. Gradle Goodness: Run a Build Script With a Different Name

    Normally Gradle looks for a build script file with the name build.gradle in the current directory to ...

  5. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

  6. Gradle Goodness: Add Incremental Build Support to Tasks

    Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unle ...

  7. Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations

    In a previous post we learned how we can use the inputs and outputs properties to set properties or ...

  8. Gradle Goodness: Display Available Tasks

    To see which tasks are available for our build we can run Gradle with the command-line option -t or ...

  9. Gradle Goodness: Working with Live Task Collection

    Gradle support the definition of so called live collections. These collections are mostly created ba ...

随机推荐

  1. 用iframe踩的坑

    1.无法监控iframe加载成功与否 经测试,火狐及chorme都不支持onerror事件,而且,不管iframe加载是否成功,都会触发onload事件. 1)通过postmessage消息提示是否加 ...

  2. HTML 发表说说 制作方法

    ==================================================================================================== ...

  3. 关于zepto 选择特定值的input 报错问题

    zepto 选择特定值的input  时,需要用单引号或双引号引用这个特定值 否则 报错

  4. 13_Redis安全

    [1.设置密码] [2.绑定IP] [3.命令禁止或重命名] [4.修改默认端口]

  5. Android DB类,支持MDB,SQLITE,SQLSERVER,支持查询、事务,对象直接插入和更新操作等

    直做数据库,最近花了点时间把自己常用的东西封装在一起. DBHelper using System; using System.Collections.Generic; using System.Te ...

  6. Tesseract-OCR-02-Tesseract-OCR 的安装与 环境变量配置

    Windows 下 Tesseract-OCR 的安装与 环境变量配置 本篇介绍Windows下Tesseract-OCR的安装与环境配置,然后做一个图片的文字识别测试 Windows下 Tesser ...

  7. 【Udacity】解决:字幕遮挡视频内容怎么办?Udacity字幕大小调整

    字幕有没有办法显示在最下方,否则把内容挡住了,根本看不清老师所指的内容 Chrome 的小插件,能方便地调节视频的字幕大小,譬如:Udacity 的学习视频字幕大小不能调节,有时候不想它占太多位置,而 ...

  8. 【Udacity】数据的集中程度:众数、平均数和中位数

    重视Code Review 极致--目标是成为优秀的开发者 Data tells a story!(数据会讲故事) 分析过程对于建模非常的重要,可以帮助我们减少实际上不相关的特征被错误的加入到模型中, ...

  9. Tomcat下JDBC连接样例

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. CentOS 附加软件包

    本人初学 CentOS,安装软件与 windows 下区别很大,大部分得通过 yum install xxx .这有个问题,一方面 yum 资源有限,另一方面 yum 默认装的版本较低.比如 Cent ...