Hello,
I am trying to build a convinience plugin based on top of the gradles ear plugin. For simplicity reasons, lets say I’m only trying to set initializeInOrder=true
.
What I expected was to be able to do that:
public class MyEarPlugin {
@Override
public void apply(final Project project) {
pluginManager.apply(EarPlugin.class);
project.getConvention().getPlugin(EarPluginConvention.class).getDeploymentDescriptor().setInitializeInOrder(true);
}
}
But this works only as long as I do not add a deploymentDescriptor
configuration to my build.gradle
file:
apply plugin: 'MyEarPlugin'
ear {
deploymentDescriptor {
securityRole {
roleName = 'User'
description = 'the ordinary guy'
}
}
}
As soon as I do that the configuration from my plugin is gone.
It took me very long to recognize that the convention from my plugin is another object instance than the one from the script. So I’ve written this code:
apply plugin: 'MyEarPlugin'
ear {
deploymentDescriptor {
org.my.EarUtil.configure(delegate)
securityRole {
roleName = 'User'
description = 'the ordinary guy'
}
}
}
public class EarUtil {
public static void configure(DeploymentDescriptor descriptor){
// FIXME: why is that so?
final EarPluginConvention earPluginConvention = project.getConvention().getPlugin(EarPluginConvention.class);
Ear task = (Ear) project.getTasksByName("ear",false).iterator().next();
DeploymentDescriptor taskDeploymentDescriptor = task.getDeploymentDescriptor();
//just some debugging
System.out.println("Script descriptor: " + deploymentDescriptor);
System.out.println("Task descriptor: " + taskDeploymentDescriptor);
System.out.println("Convention descriptor: " + earPluginConvention.getDeploymentDescriptor());
// now it works
descriptor.setInitializeInOrder(true);
// feels like a very dirty hack
earPluginConvention.setDeploymentDescriptor(deploymentDescriptor);
}
}
The hacky part is nessesary for other parts of my real plugin to be able to read the users configurations later in the configuration phase (actually I need them in the eclipse.wtp.component.file.whenMerged(action)
).
Here’s the output of the System.outs:
Script descriptor: org.gradle.plugins.ear.descriptor.internal.DefaultDeploymentDescriptor_Decorated@5e68108a
Task descriptor: org.gradle.plugins.ear.descriptor.internal.DefaultDeploymentDescriptor_Decorated@17f9b169
Convention descriptor: org.gradle.plugins.ear.descriptor.internal.DefaultDeploymentDescriptor_Decorated@17f9b169
But I don’t understand why this is even a thing and why the convention and task have another deploymentDescriptor instance than the build.gradle
script.
Could anyone point me into the right direction? I’ve found nothing in the plugin development guide and I’m not sure if I missunderstood some core concepts of the gradle configuration lifecycle.
kind regards
Daniel