This message was deleted.
# community-support
s
This message was deleted.
c
Instead of extending the Exec task, inject ExecOperations into a custom task and use that as needed at execution time.
p
@Chris Lee, is that specifically for JavaExec tasks? I need to call several different command-line utilities with the Exec tasks.
c
ExecOperations has two methods - one for general exec, one for javaExec.
p
inject ExecOperations into a custom task
Are you suggesting that I extend
DefaultTask
? And use
@Inject
in my extension class?
c
Yep, extend DefaultTask, annotate constructor with @Inject and have ExecOperations as a constructor parameter.
Here is an example of doing this - for a BuildService, but concept is the same.
p
@Chris Lee, I appreciate the help here, and I'm having trouble translating it to the Groovy syntax I need. Would my class to extend
DefaultTask
look like this?
Copy code
class TransformXslt extends DefaultTask {
    
    @Inject
    TransformXslt(ExecOperations execOps) {}

    @TaskAction
    def transform {
        execOps.exec {
            // Some invocation of a command
        }
    }
}
c
think so. Haven’t touched Groovy in a while. You could equally do that in Kotlin.
p
Thanks! Do you have an example of a task that uses the custom type you're defining in
GitRepository
?
I'm wondering how the build task would set execOps when using that type.
c
The execOps is injected by Gradle at the time the task is created - you don’t need to set it.
Sorry - I don’t have a custom task handy atm that is using ExecOps.
p
Thanks for your help!
👍 1