# Why can't I extend Exec class?

**URL:** <https://discuss.gradle.org/t/why-cant-i-extend-exec-class/24887>\
**Category:** Help/Discuss\
**Created:** [November 22, 2017, 4:13pm UTC](https://discuss.gradle.org/t/why-cant-i-extend-exec-class/24887 "2017-11-22T16:13:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sc1478](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sc1478/32/4341_2.png) [@sc1478](https://discuss.gradle.org/u/sc1478)\
**Post date:** [November 22, 2017, 4:13pm UTC](https://discuss.gradle.org/t/why-cant-i-extend-exec-class/24887/1 "2017-11-22T16:13:01Z")

</div>

I wish to extend Exec in a plugin I am writing to define a task class that executes a command line and also has an extension to hold some data.

However, I write a groovy class extending Exec and immediately get errors : ![execextend](https://global.discourse-cdn.com/gradle/original/2X/4/4a1fe3cd7757ec41ddfeb4504d70ebc6d5bf803a.png)

If I click on the error marking I get

 ![execextend2](https://global.discourse-cdn.com/gradle/original/2X/c/c82933c32f9189135283d23c3fefdeb026ed8fdf.png).

Why can’t the Exec task be extended?

No such errors occur when extending DefaultTask and providing a @TaskAction that calls project.exec(), which might be the workaround I should use. But what is so special about the Exec task class?

---

<div class="post-metadata">

**Author:** ![jjustinic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jjustinic/32/9005_2.png) [@jjustinic](https://discuss.gradle.org/u/jjustinic)\
**Post date:** [November 23, 2017, 12:00am UTC](https://discuss.gradle.org/t/why-cant-i-extend-exec-class/24887/2 "2017-11-23T00:00:03Z")

</div>

There isn’t anything special about the `Exec` task that should prevent you from extending it. It seems like you might be having an issue with groovy-eclipse, not the actual code. Your code, as written, should compile just fine with plain `groovyc`. Have you attempted to build it without the IDE?

In either case, I think you will find that many plugin authors consider the work-around you mentioned to be preferred over task extension, often with additional delegation (i.e. the task delegates to an exec runner API, which encapsulates the actual implementation, such as `project.exec()`, `ProcessBuilder`, or others).

---

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [November 23, 2017, 12:04pm UTC](https://discuss.gradle.org/t/why-cant-i-extend-exec-class/24887/3 "2017-11-23T12:04:07Z")

</div>

Yes, the Exec extension should work.

You should also look at the Grolifant library ([https://ysb33rorg.gitlab.io/grolifant](https://ysb33rorg.gitlab.io/grolifant)) which will allow you to more crafty things around external tool execution)

---

<div class="post-metadata">

**Author:** ![sc1478](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sc1478/32/4341_2.png) [@sc1478](https://discuss.gradle.org/u/sc1478)\
**Post date:** [November 27, 2017, 4:27pm UTC](https://discuss.gradle.org/t/why-cant-i-extend-exec-class/24887/4 "2017-11-27T16:27:48Z")

</div>

> [@jjustinic](#):
>
> Have you attempted to build it without the IDE?

No, the workaround was too easy 🙂

Still, I wonder, if DefaultTask can be extended in Eclipse Groovy, why can’t Exec?
