Why can't I extend Exec class?

(Steve Cohen) #1

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

If I click on the error marking I get


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?

(James Justinic) #2

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

(Schalk Cronjé) #3

Yes, the Exec extension should work.

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

(Steve Cohen) #4

No, the workaround was too easy :slight_smile:

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