{
MavenPluginFacet plugins = project.getFacet(MavenPluginFacet.class);
DependencyFacet deps = project.getFacet(DependencyFacet.class);
Dependency pluginCoordinates = DependencyBuilder.create().setGroupId(plugin.getDependency().getGroupId())
.setArtifactId(plugin.getDependency().getArtifactId());
MavenPlugin managedPlugin = null;
if (plugins.hasManagedPlugin(pluginCoordinates))
{
managedPlugin = plugins.getManagedPlugin(pluginCoordinates);
}
MavenPlugin existing = null;
// existing represents the plugin(management) as it exists currently throughout the entire hierarchy
if (managed && plugins.hasEffectiveManagedPlugin(pluginCoordinates))
{
existing = plugins.getEffectiveManagedPlugin(pluginCoordinates);
if (plugins.hasManagedPlugin(pluginCoordinates) && !mergeWithExisting)
{
// If no merge, existing should not have any of the direct managed-plugin configuration
existing = diff(existing, plugins.getManagedPlugin(pluginCoordinates));
}
}
else if (plugins.hasEffectivePlugin(pluginCoordinates))
{
existing = plugins.getEffectivePlugin(pluginCoordinates);
if (plugins.hasPlugin(pluginCoordinates) && !mergeWithExisting)
{
// If no merge, existing should not have any of the direct plugin configuration
existing = diff(existing, plugins.getPlugin(pluginCoordinates));
}
}
MavenPlugin filteredPlugin = plugin;
// The filtered plugin preserve the hierarchy, by preventing installing properties already defined with the same
// values
if (existing != null && preserveHierarchyPrecedence)
{
filteredPlugin = diff(plugin, existing);
}
// Preserve direct plugin-management inheritance
if (!managed && managedPlugin != null)
{
// The plugin section does not exists but a plugin management section in the direct pom does
filteredPlugin = diff(filteredPlugin, managedPlugin);
}
MavenPlugin mergedPlugin = filteredPlugin;
// merged plugin is a merge with the direct plugin(management)
if (mergeWithExisting)
{
if (managed && managedPlugin != null)
{
mergedPlugin = plugins.merge(mergedPlugin, managedPlugin);
}
else if (!managed && plugins.hasPlugin(pluginCoordinates))
{
mergedPlugin = plugins.merge(mergedPlugin, plugins.getPlugin(pluginCoordinates));
}
}
// Resolve version
String versionToInstall = plugin.getDependency().getVersion();
if (mergedPlugin.getDependency().getVersion() == null)
{
// null version means no version was specified or already defined in the hierarchy
if (versionToInstall == null)
{
versionToInstall = promptVersion(deps, pluginCoordinates, null).getVersion();