# Provide default values for dynamic properties

**URL:** <https://discuss.gradle.org/t/provide-default-values-for-dynamic-properties/4520>\
**Category:** Old Forum Archive\
**Created:** [August 31, 2012, 4:50pm UTC](https://discuss.gradle.org/t/provide-default-values-for-dynamic-properties/4520 "2012-08-31T16:50:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![quidryan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/quidryan/32/73_2.png) [@quidryan](https://discuss.gradle.org/u/quidryan)\
**Post date:** [August 31, 2012, 4:50pm UTC](https://discuss.gradle.org/t/provide-default-values-for-dynamic-properties/4520/1 "2012-08-31T16:50:00Z")

</div>

In an init script before I’ve applied any plugins, I’m running into a common pattern when I want to set a variable to some default, but I want to be able to overridden via gradle.properties or via command line arguments. I end up writing something like this, e.g.

```gradle
def propOverride = rootProject.hasProperty('prop') && rootProject.prop
        def prop = propOverride?:false // default value

```

This is because given the part of the lifecycle I’m in, if I set the property I’ll be blindly overriding what someone has already provide. If I just reference the property, e.g. rootProject.prop, it’s not guaranteed to be around. One thing Ant really has going for it is that properties are immutable, so the first one to set the variable (in a well defined order) wins. Which enables a user to specify properties via the command line and get the most expected behavior. I’d like to provide a form of a conventionMapping for these dynamic properties. Is something like this possible:

```gradle
rootProject.conventionMapping.with {
    ext.prop = { 'defaultValue' }
}

```

I could creation a real convention for these, but I’m so early in the lifecycle that it’s not practical.

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [September 10, 2012, 11:00am UTC](https://discuss.gradle.org/t/provide-default-values-for-dynamic-properties/4520/2 "2012-09-10T11:00:00Z")

</div>

For some other work we are doing, we are starting to discuss general configurability of the model from the command line. It’s almost certain that this will be geared towards overriding “defaults” so will address this issue. At the moment, the spec is at: [https://github.com/gradle/gradle/blob/master/design-docs/task-configuration-from-command-line.md](https://github.com/gradle/gradle/blob/master/design-docs/task-configuration-from-command-line.md). It speaks about _task_ configuration, but the discussion is leaning towards using this to work on the general problem. We’ll be talking more about this on the dev list over the coming weeks.

As for an immediate solution…

You can’t convention map an extension property. You’d simply have to create it with the default value, and then looking for the user input and assign if applicable.

---

<div class="post-metadata">

**Author:** ![quidryan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/quidryan/32/73_2.png) [@quidryan](https://discuss.gradle.org/u/quidryan)\
**Post date:** [September 10, 2012, 4:35pm UTC](https://discuss.gradle.org/t/provide-default-values-for-dynamic-properties/4520/3 "2012-09-10T16:35:00Z")

</div>

Thanks for the answer.
