Hi guys,
I’m having difficulties re-configuring tasks in a multi-project module because I would like to access them after they are configured, but the whenTaskAdded/withType deliver task objects after they are created but before they are configured (e.g. when a configure closure is specified when adding the task).
Here is my scenario, maybe somebody could point out a different mechanism to achieve what I’m trying to:
-
I have a “container install” plugin which provides task for installing a container. For the purposes of this example let’s assume it is a web container, so the plugin provides an “install” task rule which would create an instance of the web container somewhere on the file system - the instance will be named according to the rule and there cannot be 2 instances with same name
-
I have a separate “container deploy” plugin which allows deploying built artifacts to a container. The “container deploy” plugin adds a “deploy” task which needs to find the container installation directory using its “container name” and deploy the artifacts However, since I wanted the “deploy” tasks to be independent from the “container” plugin, so that they could also be used to deploy into a container which is already installed instead of being created as part of the current build, the “deploy” tasks do NOT have an explicit dependency to the install task.
Thus this covers two scenarios: - have an already installed container, apply just “container deploy” and configure “deploy” task specifying its location - apply “container install” to root project, apply “container deploy” to each subproject - the root project installs the container and each subproject deploys its artifacts into it
For the latter scenario I need to re-configure the “deploy” tasks with the location of the container, and I’m trying to do this in the “container install” plugin:
“container deploy” # apply
// add deploy task rule “deploy” for installing into specified container
String container = … // extract container name from task name
project.tasks.add([name: "deploy$container ", type: Deploy.class], {
containerName = container
})
“container install” # apply project.gradle.projectsEvaluated {
project.rootProject.subprojects { Project proj->
proj.tasks.withType(Deploy.class) { Deploy deploy ->
String containerName = deploy.getContainerName() // returns NULL since Deploy task is NOT configured yet
deploy.conventionMapping.containerLocation
= { containerConvention.baseInstallDir + File.separator + containerName }
deploy.dependsOn(project.tasks.getByName(“install$containerName”))
}
} }
However, since the withType {} closure is called after adding the Deploy task to the TaskContainer but BEFORE it is configured, trying to access ‘containerName’ in the closure returns null
I know that the container name can be extracted by parsing the task name, but in the real example I have more configuration settings that I would like to access in the withType {} closure. I have been thinking of adding this re-configuration in the “container deploy” plugin - e.g. instead of “container install” to confiure “deploy” tasks, deploy tasks might search for “container install” and re-configure themselves, but this does not seem very elegant.
Does somebody sees anything wrong in this approach? Are there any other suggestions for how this could be accomplished?
Regards,
Detelin