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. DataTables固定表格宽度(设置横向滚动条)

    当表格的列比较多的时候,可能就需要固定表格的宽度了,默认的100%宽已经不适应了.默认的100%宽要实现改变窗口大小也100%的话,在table元素上添加width="100%", ...

  2. Redis数据类型及常用命名总结

    Redis数据类型: Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合).  1.String(字符串) ...

  3. python全栈开发之路

    一.Python基础 python简介 python数据类型(数字\字符串\列表) python数据类型(元组\字典) python数据类型(集合) python占位符%s,%d,%r,%f prin ...

  4. linux cut: invalid byte, character or field list Try 'cut --help' for more information.

    1. 概述 centos执行简单shell 脚本 报错 cut: invalid byte, character or field listTry 'cut --help' for more info ...

  5. JavaScript 数组对象常用属性

    concat() 用于连接两个或多个数组.该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. var a = ["aa","ccc"]; var b ...

  6. 万网云解析全面升级开放,支持海外IP解析!

    基于万网过去18年来的专业域名解析服务经验,万网云解析新版实现了承载超过300万域名的全面升级,它是万网DNS域名解析系统的全新升级,目前已正式发布上线,详见万网首页:http://www.net.c ...

  7. 【阿里云产品公测】在ACE上部署WP测试体验

      ACE服务其实已经有很多类似的服务提供商了,无论收费的还是免费的,  但是到现在为止还没有体验过,正好借着这次机会,来体验一下阿里云的ACE服务. ' !2NSv   /IQ$[WR cx   B ...

  8. C# 使用Guid类生成不重复的随机数

    什么是Guid GUID(全局唯一标识符)         全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID ...

  9. IIFE

    一.IIFE IIFE:immediately-invoked function expression,即时调用函数表达式. 如果一个函数,在定义的时候,就想直接调用它,就是一个IIFE. 函数执行方 ...

  10. IIS6.0配置正常,但是显示“网页无法访问”,Httperr.log中显示全是“Connections_refused”,问题总结

    转自:http://blog.csdn.net/foxeatapple/article/details/21983869 最近部门的Web服务器突然无法访问! 加班解决! 问题症状: 1.“Inter ...