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:

view sourceprint?

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.

view sourceprint?

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

时间: 2024-12-20 14:24:58

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

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

Example LINUX init Script

From time to time, people want me to create LINUX init scripts for them. I usually just take an existing one for another service and change it up to work for my new application, but most of them have become so long these days that I end up having to

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 execute a build. But we can easily use a different name or directory for the build file. We only have to use the -b or --build-file command-line opti

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 group and if we look at the output of the tasks task we can see our tasks grouped in section with the given name. In the next sample we create a new t

Gradle的init.d的工作流程

init.d gradle.projectsLoaded { println "Hi from loaded" } script.run-> //script = init_c691rc.... , 也是后面closure  _run_closure1 的delegate 通过groovy的closure调用机制 ->DefaultGradle.java public void projectsLoaded(Closure closure) { buildListenerB

Gradle Goodness: Add Incremental Build Support to Tasks

Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build. Suppose we have a task that generates a file. T

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 --tasks. Gradle outputs the available tasks from our build script. By default only the tasks which are dependencies on other tasks are shown. To see a

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 files that need to be checked to see if a task is up to date. In this post we learn how a custom task class can use annotations to set input propertie