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

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

转载于:https://www.cnblogs.com/GoAhead/p/4188044.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值