Search in sources :

Example 31 with Plugin

use of org.apache.maven.model.Plugin in project mule by mulesoft.

the class MavenUtils method addSharedLibraryDependency.

/**
 * Adds a shared library to the pom model. If the plugin does not exists yet in the model then it will create it.
 *
 * @param model the pom model
 * @param dependency the descriptor of the dependency
 */
public static void addSharedLibraryDependency(Model model, Dependency dependency) {
    Build build = model.getBuild();
    if (build == null) {
        build = new Build();
        model.setBuild(build);
    }
    List<Plugin> plugins = build.getPlugins();
    if (plugins == null) {
        plugins = new ArrayList<>();
        build.setPlugins(plugins);
    }
    Optional<Plugin> pluginOptional = plugins.stream().filter(plugin -> plugin.getGroupId().equals(MULE_MAVEN_PLUGIN_GROUP_ID) && plugin.getArtifactId().equals(MULE_MAVEN_PLUGIN_ARTIFACT_ID)).findFirst();
    List<Plugin> finalPlugins = plugins;
    Plugin plugin = pluginOptional.orElseGet(() -> {
        Plugin muleMavenPlugin = new Plugin();
        muleMavenPlugin.setGroupId(MULE_MAVEN_PLUGIN_GROUP_ID);
        muleMavenPlugin.setArtifactId(MULE_MAVEN_PLUGIN_ARTIFACT_ID);
        finalPlugins.add(muleMavenPlugin);
        return muleMavenPlugin;
    });
    Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
    if (configuration == null) {
        configuration = new Xpp3Dom("configuration");
        plugin.setConfiguration(configuration);
    }
    Xpp3Dom sharedLibrariesDom = configuration.getChild(SHARED_LIBRARIES_ELEMENT);
    if (sharedLibrariesDom == null) {
        sharedLibrariesDom = new Xpp3Dom(SHARED_LIBRARIES_ELEMENT);
        configuration.addChild(sharedLibrariesDom);
    }
    Xpp3Dom sharedLibraryDom = new Xpp3Dom("sharedLibrary");
    sharedLibrariesDom.addChild(sharedLibraryDom);
    Xpp3Dom groupIdDom = new Xpp3Dom("groupId");
    groupIdDom.setValue(dependency.getGroupId());
    sharedLibraryDom.addChild(groupIdDom);
    Xpp3Dom artifactIdDom = new Xpp3Dom("artifactId");
    artifactIdDom.setValue(dependency.getArtifactId());
    sharedLibraryDom.addChild(artifactIdDom);
}
Also used : META_INF(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor.META_INF) FileUtils.createFile(org.mule.runtime.core.api.util.FileUtils.createFile) URL(java.net.URL) JarUtils.getUrlWithinJar(org.mule.runtime.core.internal.util.JarUtils.getUrlWithinJar) ArtifactDescriptorCreateException(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptorCreateException) Dependency(org.apache.maven.model.Dependency) MULE_ARTIFACT_PATH_INSIDE_JAR(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_ARTIFACT_PATH_INSIDE_JAR) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) MULE_MAVEN_PLUGIN_ARTIFACT_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) JarUtils.loadFileContentFrom(org.mule.runtime.core.internal.util.JarUtils.loadFileContentFrom) MULE_PLUGIN_POM(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_PLUGIN_POM) File.separator(java.io.File.separator) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) JarUtils.getUrlsWithinJar(org.mule.runtime.core.internal.util.JarUtils.getUrlsWithinJar) DIRECTORY(org.apache.commons.io.filefilter.DirectoryFileFilter.DIRECTORY) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IOException(java.io.IOException) String.format(java.lang.String.format) File(java.io.File) List(java.util.List) Plugin(org.apache.maven.model.Plugin) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) FileFilter(java.io.FileFilter) Paths(java.nio.file.Paths) MULE_MAVEN_PLUGIN_GROUP_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_GROUP_ID) Optional(java.util.Optional) FileReader(java.io.FileReader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) InputStream(java.io.InputStream) Model(org.apache.maven.model.Model) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) Plugin(org.apache.maven.model.Plugin)

Example 32 with Plugin

use of org.apache.maven.model.Plugin in project wildfly-swarm by wildfly-swarm.

the class MultiStartMojo method startProject.

@SuppressWarnings("unchecked")
protected void startProject(MavenProject project, String executionId, XmlPlexusConfiguration process) throws InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, MojoFailureException, MojoExecutionException, PluginManagerException {
    Plugin plugin = this.project.getPlugin("org.wildfly.swarm:wildfly-swarm-plugin");
    Xpp3Dom config = getConfiguration(project, executionId);
    Xpp3Dom processConfig = getProcessConfiguration(process);
    Xpp3Dom globalConfig = getGlobalConfig();
    Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(processConfig, config);
    mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, globalConfig);
    PluginDescriptor pluginDescriptor = this.pluginManager.loadPlugin(plugin, project.getRemotePluginRepositories(), this.repositorySystemSession);
    MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("start");
    MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, mergedConfig);
    mavenSession.setCurrentProject(project);
    this.pluginManager.executeMojo(mavenSession, mojoExecution);
    List<SwarmProcess> launched = (List<SwarmProcess>) mavenSession.getPluginContext(pluginDescriptor, project).get(SWARM_PROCESS);
    List<SwarmProcess> procs = (List<SwarmProcess>) getPluginContext().get(SWARM_PROCESS);
    if (procs == null) {
        procs = new ArrayList<>();
        getPluginContext().put(SWARM_PROCESS, procs);
    }
    procs.addAll(launched);
    mavenSession.setCurrentProject(this.project);
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) MojoExecution(org.apache.maven.plugin.MojoExecution) ArrayList(java.util.ArrayList) List(java.util.List) SwarmProcess(org.wildfly.swarm.tools.exec.SwarmProcess) Plugin(org.apache.maven.model.Plugin)

Example 33 with Plugin

use of org.apache.maven.model.Plugin 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 34 with Plugin

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

the class DistributionEnforcingManipulator method getManagedPluginMap.

private Map<String, Plugin> getManagedPluginMap(final ModelBase base) {
    if (base instanceof Model) {
        final Build build = ((Model) base).getBuild();
        if (build == null) {
            return Collections.emptyMap();
        }
        final PluginManagement pm = build.getPluginManagement();
        if (pm == null) {
            return Collections.emptyMap();
        }
        final Map<String, Plugin> result = pm.getPluginsAsMap();
        if (result == null) {
            return Collections.emptyMap();
        }
        return result;
    }
    return Collections.emptyMap();
}
Also used : PluginManagement(org.apache.maven.model.PluginManagement) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) Plugin(org.apache.maven.model.Plugin)

Example 35 with Plugin

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

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