Search in sources :

Example 36 with Plugin

use of org.apache.maven.model.Plugin in project pom-manipulation-ext by release-engineering.

the class PluginManipulator method applyChanges.

/**
 * Apply the alignment changes to the list of {@link Project}'s given.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final PluginState state = session.getState(PluginState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    final Set<Plugin> mgmtOverrides = loadRemoteBOM(PluginType.RemotePM);
    final Set<Plugin> pluginOverrides = loadRemoteBOM(PluginType.RemoteP);
    for (final Project project : projects) {
        final Model model = project.getModel();
        if (!mgmtOverrides.isEmpty()) {
            apply(project, model, PluginType.RemotePM, mgmtOverrides);
            changed.add(project);
        }
        if (!pluginOverrides.isEmpty()) {
            apply(project, model, PluginType.RemoteP, pluginOverrides);
            changed.add(project);
        }
    }
    // If we've changed something now update any old properties with the new values.
    if (!changed.isEmpty()) {
        logger.info("Iterating for standard overrides...");
        for (Project project : versionPropertyUpdateMap.keySet()) {
            for (final Map.Entry<String, String> entry : versionPropertyUpdateMap.get(project).entrySet()) {
                // Ignore strict alignment for plugins ; if we're attempting to use a differing plugin
                // its unlikely to be an exact match.
                PropertiesUtils.PropertyUpdate found = PropertiesUtils.updateProperties(session, project, true, entry.getKey(), entry.getValue());
                if (found == PropertiesUtils.PropertyUpdate.NOTFOUND) {
                    // Problem in this scenario is that we know we have a property update map but we have not found a
                    // property to update. Its possible this property has been inherited from a parent. Override in the
                    // top pom for safety.
                    logger.info("Unable to find a property for {} to update", entry.getKey());
                    for (final Project p : changed) {
                        if (p.isInheritanceRoot()) {
                            logger.info("Adding property {} with {} ", entry.getKey(), entry.getValue());
                            p.getModel().getProperties().setProperty(entry.getKey(), entry.getValue());
                        }
                    }
                }
            }
        }
    }
    return changed;
}
Also used : PluginState(org.commonjava.maven.ext.core.state.PluginState) Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PropertiesUtils(org.commonjava.maven.ext.core.util.PropertiesUtils) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Plugin(org.apache.maven.model.Plugin)

Example 37 with Plugin

use of org.apache.maven.model.Plugin in project pom-manipulation-ext by release-engineering.

the class PluginManipulator method loadRemoteBOM.

private Set<Plugin> loadRemoteBOM(PluginType type) throws ManipulationException {
    final RESTState rState = session.getState(RESTState.class);
    final PluginState pState = session.getState(PluginState.class);
    final Set<Plugin> restOverrides = pState.getRemoteRESTOverrides();
    final Set<Plugin> bomOverrides = new LinkedHashSet<>();
    final List<ProjectVersionRef> gavs = pState.getRemotePluginMgmt();
    Set<Plugin> mergedOverrides = new LinkedHashSet<>();
    if (gavs != null) {
        // We used to iterate in reverse order so that the first GAV in the list overwrites the last
        // but due to the simplification moving to a single Set, as that doesn't support replace operation,
        // we now iterate in normal order.
        final Iterator<ProjectVersionRef> iter = gavs.iterator();
        final Properties exclusions = (Properties) session.getUserProperties().clone();
        exclusions.putAll(System.getProperties());
        while (iter.hasNext()) {
            final ProjectVersionRef ref = iter.next();
            if (type == PluginType.RemotePM) {
                bomOverrides.addAll(effectiveModelBuilder.getRemotePluginManagementVersionOverrides(ref, exclusions));
            } else {
                bomOverrides.addAll(effectiveModelBuilder.getRemotePluginVersionOverrides(ref, exclusions));
            }
        }
    }
    // TODO: Remote Plugin (as opposed to Remote PluginManagement) alignment is deprecated. Therefore we don't support combining it with REST.
    if (type == PluginType.RemoteP) {
        if (pState.getPrecedence() != PluginPrecedence.BOM) {
            logger.warn("Remote plugin alignment is only supported with precedence type of BOM");
        }
        mergedOverrides = bomOverrides;
    } else {
        if (pState.getPrecedence() == PluginPrecedence.BOM) {
            mergedOverrides = bomOverrides;
            if (mergedOverrides.isEmpty()) {
                String msg = rState.isEnabled() ? "pluginSource for restURL" : "pluginManagement";
                logger.warn("No dependencies found for pluginSource {}. Has {} been configured? ", pState.getPrecedence(), msg);
            }
        }
        if (pState.getPrecedence() == PluginPrecedence.REST) {
            mergedOverrides = restOverrides;
            if (mergedOverrides.isEmpty()) {
                logger.warn("No dependencies found for pluginSource {}. Has restURL been configured? ", pState.getPrecedence());
            }
        } else if (pState.getPrecedence() == PluginPrecedence.RESTBOM) {
            mergedOverrides = restOverrides;
            mergedOverrides.addAll(bomOverrides);
        } else if (pState.getPrecedence() == PluginPrecedence.BOMREST) {
            mergedOverrides = bomOverrides;
            mergedOverrides.addAll(restOverrides);
        }
    }
    logger.debug("Final remote override list for type {} with precedence {} is {}", type.toString(), pState.getPrecedence(), mergedOverrides);
    return mergedOverrides;
}
Also used : PluginState(org.commonjava.maven.ext.core.state.PluginState) LinkedHashSet(java.util.LinkedHashSet) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) Properties(java.util.Properties) RESTState(org.commonjava.maven.ext.core.state.RESTState) Plugin(org.apache.maven.model.Plugin)

Example 38 with Plugin

use of org.apache.maven.model.Plugin in project pom-manipulation-ext by release-engineering.

the class PluginManipulator method applyOverrides.

/**
 * Set the versions of any plugins which match the contents of the list of plugin overrides.
 *
 * Currently this method takes the remote plugin type (note that remote plugins are deprecated) and the local plugin type.
 * It will ONLY apply configurations, executions and dependencies from the remote pluginMgmt to the local pluginMgmt.
 *   If the local pluginMgmt does not have a matching plugin then, if {@link CommonState#getOverrideTransitive()} is true
 * then it will inject a new plugin into the local pluginMgmt.
 *   It will however apply version changes to both local pluginMgmt and local plugins.
 * Note that if the deprecated injectRemotePlugins is enabled then remote plugin version, executions, dependencies and
 * configurations will also be applied to the local plugins.
 *
 * @param project the current project
 * @param remotePluginType The type of the remote plugin (mgmt or plugins)
 * @param localPluginType The type of local block (mgmt or plugins). Only used to determine whether to inject configs/deps/executions.
 * @param plugins The list of plugins to modify
 * @param pluginVersionOverrides The list of version overrides to apply to the plugins
 * @throws ManipulationException if an error occurs.
 */
private void applyOverrides(Project project, PluginType remotePluginType, final PluginType localPluginType, final HashMap<ProjectVersionRef, Plugin> plugins, final Set<Plugin> pluginVersionOverrides) throws ManipulationException {
    if (plugins == null) {
        throw new ManipulationException("Original plugins should not be null");
    }
    final PluginState pluginState = session.getState(PluginState.class);
    final CommonState commonState = session.getState(CommonState.class);
    final HashMap<String, ProjectVersionRef> pluginsByGA = new LinkedHashMap<>();
    // Secondary map of original plugins group:artifact to pvr mapping.
    for (ProjectVersionRef pvr : plugins.keySet()) {
        // We should NEVER have multiple group:artifact with different versions in the same project. If we do,
        // like with dependencies, the behaviour is undefined - although its most likely the last-wins.
        pluginsByGA.put(pvr.asProjectRef().toString(), pvr);
    }
    for (final Plugin override : pluginVersionOverrides) {
        Plugin plugin = null;
        String newValue = override.getVersion();
        // override version.
        if (pluginsByGA.containsKey(override.getKey())) {
            // Potential match of override group:artifact to original plugin group:artifact.
            String oldValue = pluginsByGA.get(override.getKey()).getVersionString();
            plugin = plugins.get(pluginsByGA.get(override.getKey()));
            if (commonState.getStrict()) {
                if (!PropertiesUtils.checkStrictValue(session, oldValue, newValue)) {
                    if (commonState.getFailOnStrictViolation()) {
                        throw new ManipulationException("Plugin reference {} replacement: {} of original version: {} violates the strict version-alignment rule!", plugin.getId(), newValue, oldValue);
                    } else {
                        logger.warn("Plugin reference {} replacement: {} of original version: {} violates the strict version-alignment rule!", plugin.getId(), newValue, oldValue);
                        // a new property either.
                        continue;
                    }
                }
            }
        }
        logger.debug("Plugin override {} and local plugin {} with remotePluginType {} / localPluginType {}", override.getId(), plugin, remotePluginType, localPluginType);
        if (plugin != null) {
            if (localPluginType == PluginType.LocalPM) {
                if (override.getConfiguration() != null) {
                    logger.debug("Injecting plugin configuration" + override.getConfiguration());
                    if (plugin.getConfiguration() == null) {
                        plugin.setConfiguration(override.getConfiguration());
                        logger.debug("Altered plugin configuration: " + plugin.getKey() + "=" + plugin.getConfiguration());
                    } else if (plugin.getConfiguration() != null) {
                        logger.debug("Existing plugin configuration: " + plugin.getConfiguration());
                        if (!(plugin.getConfiguration() instanceof Xpp3Dom) || !(override.getConfiguration() instanceof Xpp3Dom)) {
                            throw new ManipulationException("Incorrect DOM type " + plugin.getConfiguration().getClass().getName() + " and" + override.getConfiguration().getClass().getName());
                        }
                        if (pluginState.getConfigPrecedence() == Precedence.REMOTE) {
                            plugin.setConfiguration(Xpp3DomUtils.mergeXpp3Dom((Xpp3Dom) override.getConfiguration(), (Xpp3Dom) plugin.getConfiguration()));
                        } else if (pluginState.getConfigPrecedence() == Precedence.LOCAL) {
                            plugin.setConfiguration(Xpp3DomUtils.mergeXpp3Dom((Xpp3Dom) plugin.getConfiguration(), (Xpp3Dom) override.getConfiguration()));
                        }
                        logger.debug("Altered plugin configuration: " + plugin.getKey() + "=" + plugin.getConfiguration());
                    }
                } else {
                    logger.debug("No remote configuration to inject from " + override.toString());
                }
                if (override.getExecutions() != null) {
                    Map<String, PluginExecution> newExecutions = override.getExecutionsAsMap();
                    Map<String, PluginExecution> originalExecutions = plugin.getExecutionsAsMap();
                    for (PluginExecution pe : newExecutions.values()) {
                        if (originalExecutions.containsKey(pe.getId())) {
                            logger.warn("Unable to inject execution " + pe.getId() + " as it clashes with an existing execution");
                        } else {
                            logger.debug("Injecting execution {} ", pe);
                            plugin.getExecutions().add(pe);
                        }
                    }
                } else {
                    logger.debug("No remote executions to inject from " + override.toString());
                }
                if (!override.getDependencies().isEmpty()) {
                    // TODO: ### Review this - is it still required?
                    logger.debug("Checking original plugin dependencies versus override");
                    // First, remove any Dependency from the original Plugin if the GA exists in the override.
                    Iterator<Dependency> originalIt = plugin.getDependencies().iterator();
                    while (originalIt.hasNext()) {
                        Dependency originalD = originalIt.next();
                        Iterator<Dependency> overrideIt = override.getDependencies().iterator();
                        while (overrideIt.hasNext()) {
                            Dependency newD = overrideIt.next();
                            if (originalD.getGroupId().equals(newD.getGroupId()) && originalD.getArtifactId().equals(newD.getArtifactId())) {
                                logger.debug("Removing original dependency {} in favour of {} ", originalD, newD);
                                originalIt.remove();
                                break;
                            }
                        }
                    }
                    // Now merge them together. Only inject dependencies in the management block.
                    logger.debug("Adding in plugin dependencies {}", override.getDependencies());
                    plugin.getDependencies().addAll(override.getDependencies());
                }
            }
            // Explicitly using the original non-resolved original version.
            String oldVersion = plugin.getVersion();
            // will never be null.
            if (!PropertiesUtils.cacheProperty(project, commonState, versionPropertyUpdateMap, oldVersion, newValue, plugin, false)) {
                if (oldVersion != null && oldVersion.equals("${project.version}")) {
                    logger.debug("For plugin {} ; version is built in {} so skipping inlining {}", plugin, oldVersion, newValue);
                } else if (oldVersion != null && oldVersion.contains("${")) {
                    throw new ManipulationException("NYI : Multiple embedded properties for plugins.");
                } else {
                    plugin.setVersion(newValue);
                    logger.info("Altered plugin version: " + override.getKey() + "=" + newValue);
                }
            }
        } else // get the correct config.
        if (remotePluginType == PluginType.RemotePM && localPluginType == PluginType.LocalPM && commonState.getOverrideTransitive() && (override.getConfiguration() != null || override.getExecutions().size() > 0)) {
            project.getModel().getBuild().getPluginManagement().getPlugins().add(override);
            logger.info("Added plugin version: " + override.getKey() + "=" + newValue);
        } else // TODO: Deprecated section.
        if (remotePluginType == PluginType.RemoteP && localPluginType == PluginType.LocalP && pluginState.getInjectRemotePlugins() && (override.getConfiguration() != null || override.getExecutions().size() > 0)) {
            project.getModel().getBuild().getPlugins().add(override);
            logger.info("For non-pluginMgmt, added plugin version : " + override.getKey() + "=" + newValue);
        }
    }
}
Also used : PluginState(org.commonjava.maven.ext.core.state.PluginState) CommonState(org.commonjava.maven.ext.core.state.CommonState) PluginExecution(org.apache.maven.model.PluginExecution) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Dependency(org.apache.maven.model.Dependency) LinkedHashMap(java.util.LinkedHashMap) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) Plugin(org.apache.maven.model.Plugin)

Example 39 with Plugin

use of org.apache.maven.model.Plugin in project pom-manipulation-ext by release-engineering.

the class DistributionEnforcingManipulatorTest method projectUnchangedWhenModeIsNone.

@Test
public void projectUnchangedWhenModeIsNone() throws Exception {
    final Plugin plugin = new Plugin();
    plugin.setGroupId(MAVEN_PLUGIN_GROUPID);
    plugin.setArtifactId(MAVEN_DEPLOY_ARTIFACTID);
    plugin.setConfiguration(simpleSkipConfig(true));
    final Build build = new Build();
    build.addPlugin(plugin);
    final Model model = new Model();
    model.setModelVersion("4.0.0");
    model.setGroupId("org.foo");
    model.setArtifactId("bar");
    model.setVersion("1");
    model.setBuild(build);
    applyTest(none, model, null);
}
Also used : Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) Plugin(org.apache.maven.model.Plugin) Test(org.junit.Test)

Example 40 with Plugin

use of org.apache.maven.model.Plugin in project pom-manipulation-ext by release-engineering.

the class DistributionEnforcingManipulatorTest method assertSkip.

private void assertSkip(final Model model, final String profileId, final boolean deploy, final boolean state) {
    BuildBase build = null;
    if (profileId != null) {
        final List<Profile> profiles = model.getProfiles();
        if (profiles != null) {
            for (final Profile profile : profiles) {
                if (profileId.equals(profile.getId())) {
                    build = profile.getBuild();
                }
            }
        }
    } else {
        build = model.getBuild();
    }
    assertThat(build, notNullValue());
    final Plugin plugin = build.getPluginsAsMap().get(ga(MAVEN_PLUGIN_GROUPID, deploy ? MAVEN_DEPLOY_ARTIFACTID : MAVEN_INSTALL_ARTIFACTID));
    assertThat(plugin, notNullValue());
    assertThat(plugin.getConfiguration().toString().contains("<skip>" + state + "</skip>"), equalTo(true));
}
Also used : BuildBase(org.apache.maven.model.BuildBase) Profile(org.apache.maven.model.Profile) Plugin(org.apache.maven.model.Plugin)

Aggregations

Plugin (org.apache.maven.model.Plugin)140 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)39 MavenProject (org.apache.maven.project.MavenProject)26 Build (org.apache.maven.model.Build)22 PluginExecution (org.apache.maven.model.PluginExecution)22 ArrayList (java.util.ArrayList)20 Dependency (org.apache.maven.model.Dependency)17 File (java.io.File)15 Model (org.apache.maven.model.Model)15 HashMap (java.util.HashMap)12 PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)12 CoreException (org.eclipse.core.runtime.CoreException)11 IOException (java.io.IOException)9 List (java.util.List)8 PluginManagement (org.apache.maven.model.PluginManagement)8 Map (java.util.Map)7 MavenSession (org.apache.maven.execution.MavenSession)7 ReportPlugin (org.apache.maven.model.ReportPlugin)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6