Home

Awesome

gradle-override-plugin

Support Status Gradle Plugin Portal Maven Central Build Apache 2.0

This plugin is no longer supported.

Plugin for overriding properties in a project from the command line. The main use case for this functionality is to avoid the introduction of "magic" system/project properties for providing user-driven property values as part of build logic. Property names are provided as String and are looked up by dot notation. If no matching property can be resolved, the build fails.

The world without the plugin

A typical scenario without the plugin looks as such:

extensions.create('example', MyExtension)

class MyExtension {
    String myProp
    Integer otherProp
    Boolean flag
}

example {
    myProp = System.getProperty('my.prop') ?: 'hello'
    otherProp = System.getProperty('other.prop') ? System.getProperty('other.prop').toInteger() : 1
    flag = System.getProperty('flag') ? System.getProperty('other.prop').toBoolean() : true
}

In this scenario, the build script user can override the default value assigned to the extension property myProp by providing the system property my.prop on the command line. This works. However, the build script author has to implement this functionality for every property that is supposed to be overriden from the command line. With an increasing amount of code this becomes very tedious. Furthermore, the build script user has to know the name of system property.

Usage

Applying the Plugin

To apply this plugin if using Gradle 2.1 or newer

plugins {
  id 'com.netflix.nebula.override' version '4.0.0'
}

If using an older version of Gradle

buildscript {
    repositories { mavenCentral() }

    dependencies {
        classpath 'com.netflix.nebula:gradle-override-plugin:4.0.0'
    }
}

apply plugin: 'com.netflix.nebula.override'

Overriding properties with the plugin

Overriding properties is not limited to specific Gradle domain objects. You can override all writeable properties. Some examples:

You can override properties through the following methods:

Override properties provided through system properties take precedence over the ones provided by environment variables.

Let's consider the following, modified build script code:

apply plugin: 'nebula.override'

class MyExtension {
    String myProp
}

example {
    myProp = 'hello'
}

The plugin defers overriding existing properties until the project is parsed during the configuration phase to allow for changing values of all properties. Property values are automatically converted to the correct type. From the command line the build script user can override the default value hello of myProp by providing -Doverride.example.myProp=newValue.

Future improvements

Gradle Compatibility Tested

Built with Oracle JDK7 Tested with Oracle JDK8

Gradle VersionWorks
2.2.1yes
2.3yes
2.4yes
2.5yes
2.6yes
2.7yes
2.8yes

LICENSE

Copyright 2014-2015 Netflix, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.