Search in sources :

Example 6 with PluginExecution

use of org.apache.maven.model.PluginExecution in project tycho by eclipse.

the class OsgiSourceMojo method isRelevantProjectImpl.

protected static boolean isRelevantProjectImpl(MavenProject project, BuildPropertiesParser buildPropertiesParser) {
    String packaging = project.getPackaging();
    boolean relevant = PackagingType.TYPE_ECLIPSE_PLUGIN.equals(packaging) || PackagingType.TYPE_ECLIPSE_TEST_PLUGIN.equals(packaging);
    if (!relevant) {
        return false;
    }
    // this assumes that sources generation has to be explicitly enabled in pom.xml
    Plugin plugin = project.getPlugin("org.eclipse.tycho:tycho-source-plugin");
    if (plugin == null) {
        return false;
    }
    for (PluginExecution execution : plugin.getExecutions()) {
        if (execution.getGoals().contains(GOAL)) {
            boolean requireSourceRoots = Boolean.parseBoolean(getParameterValue(execution, "requireSourceRoots", "false"));
            if (requireSourceRoots) {
                return true;
            }
            boolean hasAdditionalFilesets = getConfigurationElement((Xpp3Dom) execution.getConfiguration(), "additionalFileSets") != null;
            if (hasAdditionalFilesets) {
                return true;
            }
            BuildProperties buildProperties = buildPropertiesParser.parse(project.getBasedir());
            if (buildProperties.getJarToSourceFolderMap().size() > 0 || buildProperties.getSourceIncludes().size() > 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) BuildProperties(org.eclipse.tycho.core.shared.BuildProperties) Plugin(org.apache.maven.model.Plugin)

Example 7 with PluginExecution

use of org.apache.maven.model.PluginExecution in project tycho by eclipse.

the class OsgiSourceMojoTest method createStubProject.

private MavenProject createStubProject(String packaging, String testResourceFolder, boolean enableSourePlugin, boolean requireSourceRoots) {
    MavenProject stubProject = new MavenProject();
    stubProject.setPackaging(packaging);
    if (enableSourePlugin) {
        Build build = new Build();
        stubProject.setBuild(build);
        Plugin tychoSourcePlugin = new Plugin();
        tychoSourcePlugin.setGroupId("org.eclipse.tycho");
        tychoSourcePlugin.setArtifactId("tycho-source-plugin");
        PluginExecution execution = new PluginExecution();
        execution.setGoals(asList("plugin-source"));
        if (requireSourceRoots) {
            Xpp3Dom config = new Xpp3Dom("configuration");
            Xpp3Dom requireSourceRootsDom = new Xpp3Dom("requireSourceRoots");
            requireSourceRootsDom.setValue("true");
            config.addChild(requireSourceRootsDom);
            execution.setConfiguration(config);
        }
        tychoSourcePlugin.setExecutions(asList(execution));
        build.setPlugins(asList(tychoSourcePlugin));
    }
    stubProject.setFile(new File("src/test/resources/sourceMojo/" + testResourceFolder + "/pom.xml"));
    return stubProject;
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MavenProject(org.apache.maven.project.MavenProject) Build(org.apache.maven.model.Build) File(java.io.File) Plugin(org.apache.maven.model.Plugin)

Example 8 with PluginExecution

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

the class ModelIO method getRemotePluginVersionOverrides.

private Set<Plugin> getRemotePluginVersionOverrides(final PluginType type, final ProjectVersionRef ref, final Properties userProperties) throws ManipulationException {
    logger.debug("Resolving remote {} POM: {}", type, ref);
    final Set<Plugin> pluginOverrides = new HashSet<>();
    final Map<ProjectRef, ProjectVersionRef> pluginOverridesPomView = new HashMap<>();
    final Model m = resolveRawModel(ref);
    try {
        final MavenPomView pomView = galleyWrapper.readPomView(ref);
        final List<PluginView> deps;
        if (type == PluginType.PluginMgmt) {
            deps = pomView.getAllManagedBuildPlugins();
        } else {
            deps = pomView.getAllBuildPlugins();
        }
        for (final PluginView p : deps) {
            pluginOverridesPomView.put(p.asProjectRef(), p.asProjectVersionRef());
        }
    } catch (GalleyMavenException e) {
        throw new ManipulationException("Unable to resolve: %s", e, ref);
    }
    logger.debug("Found pluginOverridesResolvedVersions {} ", pluginOverridesPomView);
    // set of plugins with versions to handle those.
    for (Map.Entry<ProjectRef, ProjectVersionRef> entry : pluginOverridesPomView.entrySet()) {
        Plugin p = new Plugin();
        p.setArtifactId(entry.getKey().getArtifactId());
        p.setGroupId(entry.getKey().getGroupId());
        p.setVersion(entry.getValue().getVersionString());
        pluginOverrides.add(p);
    }
    // TODO: active profiles!
    if (m.getBuild() != null && m.getBuild().getPluginManagement() != null) {
        Iterator<Plugin> plit = null;
        if (type == PluginType.PluginMgmt && m.getBuild().getPluginManagement() != null) {
            logger.debug("Returning override of " + m.getBuild().getPluginManagement().getPlugins());
            plit = m.getBuild().getPluginManagement().getPlugins().iterator();
        } else if (type == PluginType.Plugins && m.getBuild().getPlugins() != null) {
            logger.debug("Returning override of " + m.getBuild().getPlugins());
            plit = m.getBuild().getPlugins().iterator();
        }
        while (plit != null && plit.hasNext()) {
            Plugin p = plit.next();
            ProjectRef pr = new SimpleProjectRef(p.getGroupId(), p.getArtifactId());
            if ((isNotEmpty(p.getVersion()) && p.getVersion().startsWith("${")) || isEmpty(p.getVersion())) {
                // Property reference to something in the remote pom. Resolve and inline it now.
                String newVersion = resolveProperty(userProperties, m.getProperties(), p.getVersion());
                // TODO: Complete replacement with PomView
                if (newVersion.startsWith("${") || newVersion.length() == 0) {
                    // Use PomView as that contains a pre-resolved list of plugins.
                    newVersion = pluginOverridesPomView.get(pr).getVersionString();
                }
                logger.debug("Replacing plugin override version " + p.getVersion() + " with " + newVersion);
                p.setVersion(newVersion);
            }
            // Replacing the element with the fully parsed element from the Model.
            pluginOverrides.remove(p);
            pluginOverrides.add(p);
            // resolve any properties.
            if (p.getConfiguration() != null) {
                processChildren(userProperties, m, (Xpp3Dom) p.getConfiguration());
            }
            if (p.getExecutions() != null) {
                List<PluginExecution> exes = p.getExecutions();
                for (PluginExecution pe : exes) {
                    if (pe.getConfiguration() != null) {
                        processChildren(userProperties, m, (Xpp3Dom) pe.getConfiguration());
                    }
                }
            }
            if (p.getDependencies() != null) {
                for (Dependency d : p.getDependencies()) {
                    if (!isEmpty(d.getVersion()) && d.getVersion().startsWith("${")) {
                        logger.debug("Processing dependency {} and updating with {} ", d, resolveProperty(userProperties, m.getProperties(), d.getVersion()));
                        d.setVersion(resolveProperty(userProperties, m.getProperties(), d.getVersion()));
                    }
                }
            }
            logger.debug("Added plugin override for {} with configuration \n" + p.getConfiguration() + " and executions " + p.getExecutions() + " and dependencies " + p.getDependencies(), p.getId());
        }
    } else {
        throw new ManipulationException("Attempting to align to a BOM that does not have a " + type.toString() + " section");
    }
    return pluginOverrides;
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Dependency(org.apache.maven.model.Dependency) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) Model(org.apache.maven.model.Model) PluginView(org.commonjava.maven.galley.maven.model.view.PluginView) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) Plugin(org.apache.maven.model.Plugin) HashSet(java.util.HashSet)

Example 9 with PluginExecution

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

the class PluginInjectingManipulator method applyChanges.

/**
 * If enabled, grab the execution root pom (which will be the topmost POM in terms of directory structure). Check for the
 * presence of the project-sources-maven-plugin in the base build (/project/build/plugins/). Inject a new plugin execution for creating project
 * sources if this plugin has not already been declared in the base build section.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final PluginInjectingState state = session.getState(PluginInjectingState.class);
    // This manipulator will only run if its enabled *and* at least one other manipulator is enabled.
    if (state.isEnabled() && session.anyStateEnabled(State.activeByDefault)) {
        for (final Project project : projects) {
            if (project.isExecutionRoot()) {
                logger.info("Examining {} to apply sources/metadata plugins.", project);
                final Model model = project.getModel();
                Build build = model.getBuild();
                if (build == null) {
                    build = new Build();
                    model.setBuild(build);
                }
                boolean changed = false;
                final Map<String, Plugin> pluginMap = build.getPluginsAsMap();
                if (state.isProjectSourcesPluginEnabled() && !pluginMap.containsKey(PROJECT_SOURCES_COORD)) {
                    final PluginExecution execution = new PluginExecution();
                    execution.setId(PROJECT_SOURCES_EXEC_ID);
                    execution.setPhase(INITIALIZE_PHASE);
                    execution.setGoals(Collections.singletonList(PROJECT_SOURCES_GOAL));
                    final Plugin plugin = new Plugin();
                    plugin.setGroupId(PROJECT_SOURCES_GID);
                    plugin.setArtifactId(PROJECT_SOURCES_AID);
                    plugin.setVersion(state.getProjectSourcesPluginVersion());
                    plugin.addExecution(execution);
                    build.addPlugin(plugin);
                    changed = true;
                }
                if (state.isBuildMetadataPluginEnabled() && !pluginMap.containsKey(BMMP_COORD)) {
                    final PluginExecution execution = new PluginExecution();
                    execution.setId(BMMP_EXEC_ID);
                    execution.setPhase(INITIALIZE_PHASE);
                    execution.setGoals(Collections.singletonList(BMMP_GOAL));
                    final Xpp3Dom xml = new Xpp3Dom("configuration");
                    final Map<String, Object> config = new HashMap<>();
                    config.put("createPropertiesReport", true);
                    config.put("hideCommandLineInfo", false);
                    config.put("hideJavaOptsInfo", false);
                    config.put("activateOutputFileMapping", true);
                    config.put("addJavaRuntimeInfo", true);
                    // Default name is build.properties but we currently prefer build.metadata.
                    config.put("propertiesOutputFile", "build.metadata");
                    // Deactivate features we don't want.
                    config.put("createXmlReport", false);
                    config.put("addLocallyModifiedTagToFullVersion", false);
                    config.put("addToGeneratedSources", false);
                    config.put("validateCheckout", false);
                    config.put("forceNewProperties", true);
                    config.put("addBuildDateToFullVersion", false);
                    config.put("addHostInfo", false);
                    config.put("addBuildDateInfo", false);
                    config.put("addOsInfo", false);
                    config.put("addMavenExecutionInfo", false);
                    config.put("addToFilters", false);
                    final Xpp3Dom additionalLocations = new Xpp3Dom("addToLocations");
                    final Xpp3Dom additionalLocation = new Xpp3Dom("addToLocation");
                    xml.addChild(additionalLocations);
                    additionalLocations.addChild(additionalLocation);
                    additionalLocation.setValue("${session.executionRootDirectory}");
                    for (final Map.Entry<String, Object> entry : config.entrySet()) {
                        final Xpp3Dom child = new Xpp3Dom(entry.getKey());
                        if (entry.getValue() != null) {
                            child.setValue(entry.getValue().toString());
                        }
                        xml.addChild(child);
                    }
                    execution.setConfiguration(xml);
                    final Plugin plugin = new Plugin();
                    plugin.setGroupId(BMMP_GID);
                    plugin.setArtifactId(BMMP_AID);
                    plugin.setVersion(state.getBuildMetadataPluginVersion());
                    plugin.addExecution(execution);
                    build.addPlugin(plugin);
                    changed = true;
                }
                if (changed) {
                    return Collections.singleton(project);
                }
            }
        }
    }
    return Collections.emptySet();
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) HashMap(java.util.HashMap) Project(org.commonjava.maven.ext.common.model.Project) PluginInjectingState(org.commonjava.maven.ext.core.state.PluginInjectingState) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) HashMap(java.util.HashMap) Map(java.util.Map) Plugin(org.apache.maven.model.Plugin)

Example 10 with PluginExecution

use of org.apache.maven.model.PluginExecution 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)

Aggregations

PluginExecution (org.apache.maven.model.PluginExecution)32 Plugin (org.apache.maven.model.Plugin)22 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)18 ArrayList (java.util.ArrayList)8 Build (org.apache.maven.model.Build)6 Dependency (org.apache.maven.model.Dependency)6 Model (org.apache.maven.model.Model)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 Element (org.jdom.Element)4 Iterator (java.util.Iterator)3 MavenProject (org.apache.maven.project.MavenProject)3 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)3 File (java.io.File)2 ConfigurationContainer (org.apache.maven.model.ConfigurationContainer)2 Repository (org.apache.maven.model.Repository)2 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)2 Project (org.commonjava.maven.ext.common.model.Project)2 CorePlugin (org.talend.core.CorePlugin)2