# Keep default value of task property

**URL:** <https://discuss.gradle.org/t/keep-default-value-of-task-property/41848>\
**Category:** Help/Discuss\
**Created:** [January 4, 2022, 1:17pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848 "2022-01-04T13:17:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![christianGen](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@christianGen](https://discuss.gradle.org/u/christianGen)\
**Post date:** [January 4, 2022, 1:17pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/1 "2022-01-04T13:17:31Z")

</div>

In my plugin I’ve created an extension and want to set properties of a task only when the extension property has been set.

The task `BootJar` for instance has a sensible default value for `mainClass`.

Is there a way to not set the tasks `mainClass` property unless the extension property has been set?

```gradle
// ...
interface AppExtension {
    val mainClass: Property<String>
    // a lot of other properties removed
}
val app = extensions.create<AppExtension>("app")

tasks.named<BootJar>("bootJar") {
    // === v === mainClass.set should only be called if app.mainClass is set
    mainClass.set(app.mainClass)
}

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 4, 2022, 1:54pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/2 "2022-01-04T13:54:08Z")

</div>

Something like this should probably do what you want:

```gradle
val bootJar by tasks.getting(BootJar::class)
app.mainClass.convention(bootJar.mainClass)
bootJar.mainClass.set(app.mainClass)

```

---

<div class="post-metadata">

**Author:** ![christianGen](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@christianGen](https://discuss.gradle.org/u/christianGen)\
**Post date:** [January 5, 2022, 11:03am UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/3 "2022-01-05T11:03:03Z")

</div>

Good idea, but unfortunately this runs into a `StackOverflowError`.

My next attempt was to change your code to:

```gradle
val bootJar by tasks.getting(BootJar::class)
app.mainClass.convention(bootJar.mainClass.get()) // <-- note the .get() here
bootJar.mainClass.set(app.mainClass)

```

This however gave me the following error-message:

```gradle
An exception occurred applying plugin request [id: 'app-conventions']
> Failed to apply plugin 'app-conventions'.
   > Failed to query the value of task ':proj2:backend:bootJar' property 'mainClass'.
      > Querying the mapped value of task ':proj2:backend:bootJarMainClassName' property 'outputFile' before task ':proj2:backend:bootJarMainClassName' has completed is not supported

```

Any other ideas I could try?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 5, 2022, 12:03pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/4 "2022-01-05T12:03:35Z")

</div>

Ah, right.  
This cannot work of course.  
You cannot get just the provider that is set for the `mainClass` property to reuse it.  
You should probably simply manually set your extension property to the the same convention as the boot jar property is set to.  
Otherwise it becomes ugly.  
You would then probably have to use an `afterEvaluate` and hope it got set before, then check whether it was set and conditionally configure the boot jar property.

---

<div class="post-metadata">

**Author:** ![christianGen](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@christianGen](https://discuss.gradle.org/u/christianGen)\
**Post date:** [January 5, 2022, 12:33pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/5 "2022-01-05T12:33:20Z")

</div>

What do you think about:

```gradle
interface DmAppExtension {
    var mainClass: Property<String> // Note that mainClass is a var now
    // other properties
}

val bootJar by tasks.getting(BootJar::class)

val app = extensions.create<AppExtension>("app")
app.fileName.convention(project.provider { project.name })
app.mainClass = bootJar.mainClass // !!! interesting Code here !!!

```

I guess making the property a `var` makes it easy to accidentally set it using `=` instead of `set()`.

[Edit:]  
The following code prevents users of the extension from replacing the property itself.

```gradle
open class AppExtension @Inject constructor(
    project: Project
) {
    val name: Property<String> = project.objects.property(String::class.java)
        .convention(project.provider { project.name })
    val fileName: Property<String> = project.objects.property(String::class.java)
        .convention(project.provider { "${project.name}.jar" })
    var mainClass: Property<String> = project.objects.property(String::class.java)
    internal fun setMainClass(newMainClass: Property<String>) { mainClass = newMainClass }
}

val app = extensions.create<DmAppExtension>("app")
app.mainClass = bootJar.mainClass

```

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 5, 2022, 1:13pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/6 "2022-01-05T13:13:09Z")

</div>

Hm, maybe if you make the mainClass property a constructor arg, then you can make it `val`, can’t you?

And btw. better inject the `ObjectFactory` and `ProviderFactory` directly rather than the `Project` .

Or maybe more idiomatic, keep the convention setting out of the extension and just have all properties `abstract` except for the `mainClass` that you set through the constructor, then you don’t need any injections at all.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [January 5, 2022, 1:25pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/9 "2022-01-05T13:25:54Z")

</div>

Like this:

```gradle
abstract class AppExtension(
    val mainClass: Property<String>
) {
    abstract val name: Property<String>
    abstract val fileName: Property<String>
}

val bootJar by tasks.getting(BootJar::class)
val app = extensions.create<AppExtension>("app", bootJar.mainClass).apply {
    name.convention(project.provider { project.name })
    fileName.convention(project.provider { "${project.name}.jar" })
}

```

The caveat it has is, that if someone modifies the property on the `BootJar` task it will also modify the one on the extension of course as they are the same now. If that is ok for you, that’s probably the way to go and much better than using `afterEvaluate`.

---

<div class="post-metadata">

**Author:** ![christianGen](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@christianGen](https://discuss.gradle.org/u/christianGen)\
**Post date:** [January 5, 2022, 1:41pm UTC](https://discuss.gradle.org/t/keep-default-value-of-task-property/41848/10 "2022-01-05T13:41:11Z")

</div>

**Thank you!**

I’ve really learned a lot from your replies!
