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. 原生JS编写getByClass、addClass、removeClass、hasClass

    前言: 年后换了工作,在现在的公司写交互主要使用JS原生:刚刚入门前端的时候写交互一直用的原生JS,虽然用的不怎么样.后来去之前的公司之后,leader主张把jQuery用好,JS原生自然就熟练了:一 ...

  2. javascript 文字大小自动适应文本框 (文字大小自动调整)

    javascript 文字大小自动适应文本框 (文字大小自动调整) TOC 思考 思考一:面积法 思考二:微调法 代码 在进行类似微博墙之类的展示页面中,经常会遇到这样的需求:在固定大小的区域放入字数 ...

  3. 两种实现光标点插入range

    一.insertNode <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  4. 05_zookeeper的ACL

    [ACL概述] ACL:access control Lists,权限控制. * 针对节点可以设置相关的读写等权限,目的是为了保障数据安全性. * 权限permissions可以指定不同的权限范围以及 ...

  5. 学习C++从入门到精通的的十本最经典书籍

    原文:http://blog.csdn.net/a_302/article/details/17558369 最近想学C++,找了一下网上推荐的书籍,转载过来给大家分享 转载自http://c.chi ...

  6. Android setTag()/getTag()

    View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来. 可以用在多个Button添加一个监听器,每个Button都设置不同的setTag ...

  7. 【转】msyql使用-用户创建/权限配置

    MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...

  8. SpringMVC学习(二)——基于xml配置的springMVC项目(maven+spring4)

    可运行的附件地址:http://files.cnblogs.com/files/douJiangYouTiao888/springWithXML.zip 项目说明: 作者环境:maven3+jdk1. ...

  9. Studying TCP's Throughput and Goodput using NS

    Studying TCP's Throughput and Goodput using NS What is Throughput Throughput is the amount of data r ...

  10. Oracle物化视图详解

    现实工作中会有多个数据源同步到一个数据库完成数据分析的场景,这些数据可以不是实时同步的,我们一般通过定时任务抽取数据到统计分析库给应用使用. 一般的同步方式可以通过时间戳做全量和增量数据同步(存在原数 ...