Wrapping a tool with AbstractExecWrapperTask
The AbstractExecWrapperTask is a simplified way of abstract tools into gradlesque tasks. Unlike the other abstraction execution task types mentioned above, it does not expose the full command-line options to the build script author, but rather allows a plugin author to provide suitable functional abstractions. For instance the Terraform plugin provides a hierarchy of tasks that wrap around the Terraform executable, deals with initialisation and simplifies integration of a very popular tool into a build pipeline in a very gradlesque way.
This abstract task also relies on the creation of suitable extension derived from AbstractToolExtension. The result is a very flexible DSL. This can be illustrated by the following example which is also a good starting point for any plugin author wanting to abstract a tool in such a way.
Step 1 - Create an execution specification
The details for creating execution specifications has been described earlier. You can use the one which is best suited to your application.
In this example we are using AbstractCommandExecSpec as the base execution specification.
@CompileStatic
class MyCmdExecSpec extends AbstractCommandExecSpec<MyCmdExecSpec> {
MyCmdExecSpec(ConfigCacheSafeOperations po) {
super(ConfigCacheSafeOperations.from(po))
}
}
Step 2 - Create the task
@CompileStatic
@CompileStatic
class MySimpleWrapperTask extends AbstractExecWrapperTask<MyCmdExecSpec> { (1)
MySimpleWrapperTask() {
super()
this.execSpec = new MyCmdExecSpec(this)
}
@Override
protected MyCmdExecSpec getExecSpec() { (2)
this.execSpec
}
@Override
protected Provider<File> getExecutableLocation() { (3)
}
private final MyCmdExecSpec execSpec
}
1 | Extend AbstractExecWrapperTask. |
2 | Implement a method that will rreturn an instance of your execution specification - in this example, MyCmdExecSpec . |
3 | Implement a method that will return the location of the executable. It can be a name or a full path. |