DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Nov. 04, 12 · Interview
Like (0)
Save
Tweet
Share
8.69K Views

Join the DZone community and get the full member experience.

Join For Free

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:

apply plugin: 'java'
apply from: 'http://intranet/source/quality.gradle'

version = '2.1.1'
group = 'com.mrhaki.gradle.sample

he 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.

// File: quality.gradle
allprojects {
    afterEvaluate { project ->
        def groovyProject = project.plugins.hasPlugin('groovy')
        def javaProject = project.plugins.hasPlugin('java')

        if (javaProject) {
            // Add Checkstyle plugin.
            project.apply plugin: 'checkstyle'

            // Task to download common Checkstyle configuration
            // from company intranet.
            task downloadCheckstyleConfig(type: DownloadFileTask) {
                description = 'Download company Checkstyle configuration'
                
                url = 'http://intranet/source/company-style.xml'
                destinationFile = checkstyle.configFile
            }

            // For each Checkstyle task we make sure
            // the company Checkstyle configuration is 
            // first downloaded.
            tasks.withType(Checkstyle) { 
                it.dependsOn 'downloadCheckstyleConfig'
            }
        }

        if (groovyProject) {
            // Add Codenarc plugin.
            project.apply plugin: 'codenarc'
        }
    }
}

class DownloadFileTask extends DefaultTask {
    @Input
    String url

    @OutputFile
    File destinationFile

    @TaskAction
    def downloadFile() {
        destinationFile.bytes = new URL(url).bytes
    }
}

Code written with Gradle 1.2

 

 

 

Gradle

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Efficiently Computing Permissions at Scale: Our Engineering Approach
  • Explainer: Building High Performing Data Product Platform
  • 5 Tips for Optimizing Your React App’s Performance
  • How to Configure AWS Glue Job Using Python-Based AWS CDK

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: