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. Eclipse使用快捷键总结

    1.为方法添加注释:Alt + Shift + J

  2. lintcode 刷题记录··

    单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The ...

  3. CSS的伪类 :before 和 :after

    CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前几天发现了 Creative Link Effects 这个非常有意思 ...

  4. sass判断语句

    @if判断 @if可一个条件单独使用,也可以和@else结合多条件使用 scss.style css.style 三目判断 语法为:if($condition, $if_true, $if_false ...

  5. Memcache 学习笔记(二)---- PHP 脚本操作 Memcache 服务器

     PHP 脚本操作 Memcache 服务器 一.PHP脚本操作Memcache方法 使用 PHP 脚本操作 Memcache,在 PHP 手册中有详细的介绍,我们可以实例化 Memcache 类,根 ...

  6. CSS3,3D效果轮播图

    ---恢复内容开始--- 大家还记得我昨天的3D拖拽立方体吗??我昨天还说过css还可以做轮播图,所以咱们今天就写一下,css的轮播图吧! ....这个轮播图主要是用CSS3里的transform的旋 ...

  7. SQL Server中建立自定义函数

    在SQL Server中用户可以自定义函数,像内置函数一样返回标量值,也可以将结果集用表格变量返回.用户自定义函数的2种类型:1.标量函数:返回一个标量值:2.表格值函数{内联表格值函数.多表格值函数 ...

  8. Linux-vi/vim编辑器常用命令与用法

    vi/vim是什么? Linux世界几乎所有的配置文件都是以纯文本形式存在的,而在所有的Linux发行版系统上都有vi编辑器,因此利用简单的文字编辑软件就能够轻松地修改系统的各种配置了,非常方便.vi ...

  9. [poj 2479] Maximum sum -- 转载

    转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                         ...

  10. vs2013 c# 中调用 c 编写的dll出错的可能错误

    先说出错原因:    堆栈调用顺序  解决办法:     使用   __stdcall 或 使用C#属性 CallingConvention 起因是我想在c#中调用c函数结果出错了 如下 C 头文件 ...