Search in sources :

Example 6 with PluginDescriptor

use of org.apache.maven.plugin.descriptor.PluginDescriptor in project docker-maven-plugin by fabric8io.

the class MojoExecutionService method callPluginGoal.

// Call another goal after restart has finished
public void callPluginGoal(String fullGoal) throws MojoFailureException, MojoExecutionException {
    String[] parts = splitGoalSpec(fullGoal);
    Plugin plugin = project.getPlugin(parts[0]);
    String goal = parts[1];
    if (plugin == null) {
        throw new MojoFailureException("No goal " + fullGoal + " found in pom.xml");
    }
    try {
        String executionId = null;
        if (goal != null && goal.length() > 0 && goal.indexOf('#') > -1) {
            int pos = goal.indexOf('#');
            executionId = goal.substring(pos + 1);
            goal = goal.substring(0, pos);
        }
        PluginDescriptor pluginDescriptor = getPluginDescriptor(project, plugin);
        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
        if (mojoDescriptor == null) {
            throw new MojoExecutionException("Could not find goal '" + goal + "' in plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
        }
        MojoExecution exec = getMojoExecution(executionId, mojoDescriptor);
        pluginManager.executeMojo(session, exec);
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to execute mojo", e);
    }
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) InvocationTargetException(java.lang.reflect.InvocationTargetException) Plugin(org.apache.maven.model.Plugin)

Example 7 with PluginDescriptor

use of org.apache.maven.plugin.descriptor.PluginDescriptor in project m2e-nar by maven-nar.

the class MavenUtils method getConfiguredMojo.

private static <T extends AbstractMojo> T getConfiguredMojo(MavenSession session, MojoExecution mojoExecution, Class<T> asType, Log log) throws CoreException {
    MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
    PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
    ClassRealm pluginRealm = getMyRealm(pluginDescriptor.getClassRealm().getWorld());
    T mojo;
    try {
        mojo = asType.newInstance();
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, MavenNarPlugin.PLUGIN_ID, "Problem when creating mojo", e));
    }
    mojo.setLog(log);
    logger.debug("Configuring mojo " + mojoDescriptor.getId() + " from plugin realm " + pluginRealm);
    Xpp3Dom dom = mojoExecution.getConfiguration();
    PlexusConfiguration pomConfiguration;
    if (dom == null) {
        pomConfiguration = new XmlPlexusConfiguration("configuration");
    } else {
        pomConfiguration = new XmlPlexusConfiguration(dom);
    }
    ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
    populatePluginFields(mojo, mojoDescriptor, pluginRealm, pomConfiguration, expressionEvaluator);
    return mojo;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) PluginParameterExpressionEvaluator(org.apache.maven.plugin.PluginParameterExpressionEvaluator) XmlPlexusConfiguration(org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration) PlexusConfiguration(org.codehaus.plexus.configuration.PlexusConfiguration) CoreException(org.eclipse.core.runtime.CoreException) XmlPlexusConfiguration(org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration) PluginParameterExpressionEvaluator(org.apache.maven.plugin.PluginParameterExpressionEvaluator) ExpressionEvaluator(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator) DuplicateRealmException(org.codehaus.plexus.classworlds.realm.DuplicateRealmException) CoreException(org.eclipse.core.runtime.CoreException) ComponentConfigurationException(org.codehaus.plexus.component.configurator.ComponentConfigurationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 8 with PluginDescriptor

use of org.apache.maven.plugin.descriptor.PluginDescriptor in project syndesis by syndesisio.

the class ExtractConnectorDescriptorsMojo method execute.

@Override
@SuppressWarnings({ "PMD.EmptyCatchBlock", "PMD.CyclomaticComplexity" })
public void execute() throws MojoExecutionException, MojoFailureException {
    ArrayNode root = new ArrayNode(JsonNodeFactory.instance);
    URLClassLoader classLoader = null;
    try {
        PluginDescriptor desc = (PluginDescriptor) getPluginContext().get("pluginDescriptor");
        List<Artifact> artifacts = desc.getArtifacts();
        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        buildingRequest.setRemoteRepositories(remoteRepositories);
        for (Artifact artifact : artifacts) {
            ArtifactResult result = artifactResolver.resolveArtifact(buildingRequest, artifact);
            File jar = result.getArtifact().getFile();
            classLoader = createClassLoader(jar);
            if (classLoader == null) {
                throw new IOException("Can not create classloader for " + jar);
            }
            ObjectNode entry = new ObjectNode(JsonNodeFactory.instance);
            addConnectorMeta(entry, classLoader);
            addComponentMeta(entry, classLoader);
            if (entry.size() > 0) {
                addGav(entry, artifact);
                root.add(entry);
            }
        }
        if (root.size() > 0) {
            saveCamelMetaData(root);
        }
    } catch (ArtifactResolverException | IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (classLoader != null) {
            try {
                classLoader.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) URLClassLoader(java.net.URLClassLoader) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 9 with PluginDescriptor

use of org.apache.maven.plugin.descriptor.PluginDescriptor in project tycho by eclipse.

the class PluginRealmHelper method execute.

public void execute(MavenSession session, MavenProject project, Runnable runnable, PluginFilter filter) throws MavenExecutionException {
    for (Plugin plugin : project.getBuildPlugins()) {
        if (plugin.isExtensions()) {
            // https://cwiki.apache.org/MAVEN/maven-3x-class-loading.html
            continue;
        }
        try {
            lifecyclePluginResolver.resolveMissingPluginVersions(project, session);
            PluginDescriptor pluginDescriptor;
            try {
                pluginDescriptor = compatibilityHelper.getPluginDescriptor(plugin, project, session);
            } catch (PluginResolutionException e) {
                // if the plugin really does not exist, the Maven build will fail later on anyway -> ignore for now (cf. bug #432957)
                logger.debug("PluginResolutionException while looking for components from " + plugin, e);
                continue;
            }
            if (pluginDescriptor != null) {
                if (pluginDescriptor.getArtifactMap().isEmpty() && pluginDescriptor.getDependencies().isEmpty()) {
                    // force plugin descriptor reload to workaround http://jira.codehaus.org/browse/MNG-5212
                    // this branch won't be executed on 3.0.5+, where MNG-5212 is fixed already
                    PluginDescriptorCache.Key descriptorCacheKey = compatibilityHelper.createKey(plugin, project, session);
                    pluginDescriptorCache.put(descriptorCacheKey, null);
                    pluginDescriptor = compatibilityHelper.getPluginDescriptor(plugin, project, session);
                }
                if (filter == null || filter.accept(pluginDescriptor)) {
                    ClassRealm pluginRealm;
                    MavenProject oldCurrentProject = session.getCurrentProject();
                    session.setCurrentProject(project);
                    try {
                        pluginRealm = buildPluginManager.getPluginRealm(session, pluginDescriptor);
                    } finally {
                        session.setCurrentProject(oldCurrentProject);
                    }
                    if (pluginRealm != null) {
                        ClassLoader origTCCL = Thread.currentThread().getContextClassLoader();
                        try {
                            Thread.currentThread().setContextClassLoader(pluginRealm);
                            runnable.run();
                        } finally {
                            Thread.currentThread().setContextClassLoader(origTCCL);
                        }
                    }
                }
            }
        } catch (PluginManagerException e) {
            throw newMavenExecutionException(e);
        } catch (PluginResolutionException e) {
            throw newMavenExecutionException(e);
        } catch (PluginVersionResolutionException e) {
            throw newMavenExecutionException(e);
        } catch (PluginDescriptorParsingException e) {
            throw newMavenExecutionException(e);
        } catch (InvalidPluginDescriptorException e) {
            throw newMavenExecutionException(e);
        }
    }
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) PluginVersionResolutionException(org.apache.maven.plugin.version.PluginVersionResolutionException) PluginResolutionException(org.apache.maven.plugin.PluginResolutionException) MavenProject(org.apache.maven.project.MavenProject) InvalidPluginDescriptorException(org.apache.maven.plugin.InvalidPluginDescriptorException) PluginDescriptorParsingException(org.apache.maven.plugin.PluginDescriptorParsingException) PluginManagerException(org.apache.maven.plugin.PluginManagerException) PluginDescriptorCache(org.apache.maven.plugin.PluginDescriptorCache) Plugin(org.apache.maven.model.Plugin)

Example 10 with PluginDescriptor

use of org.apache.maven.plugin.descriptor.PluginDescriptor in project tycho by eclipse.

the class P2DependencyResolver method getDependencyMetadata.

protected Map<String, IDependencyMetadata> getDependencyMetadata(final MavenSession session, final MavenProject project, final List<TargetEnvironment> environments, final OptionalResolutionAction optionalAction) {
    final Map<String, IDependencyMetadata> metadata = new LinkedHashMap<>();
    metadata.put(null, generator.generateMetadata(new AttachedArtifact(project, project.getBasedir(), null), environments, optionalAction));
    // let external providers contribute additional metadata
    try {
        pluginRealmHelper.execute(session, project, new Runnable() {

            @Override
            public void run() {
                try {
                    for (P2MetadataProvider provider : plexus.lookupList(P2MetadataProvider.class)) {
                        Map<String, IDependencyMetadata> providedMetadata = provider.getDependencyMetadata(session, project, null, optionalAction);
                        if (providedMetadata != null) {
                            metadata.putAll(providedMetadata);
                        }
                    }
                } catch (ComponentLookupException e) {
                // have not found anything
                }
            }
        }, new PluginFilter() {

            @Override
            public boolean accept(PluginDescriptor descriptor) {
                return isTychoP2Plugin(descriptor);
            }
        });
    } catch (MavenExecutionException e) {
        throw new RuntimeException(e);
    }
    return metadata;
}
Also used : PluginFilter(org.eclipse.tycho.core.maven.utils.PluginRealmHelper.PluginFilter) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) LinkedHashMap(java.util.LinkedHashMap) AttachedArtifact(org.eclipse.tycho.p2.facade.internal.AttachedArtifact) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) MavenExecutionException(org.apache.maven.MavenExecutionException) IDependencyMetadata(org.eclipse.tycho.p2.metadata.IDependencyMetadata) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)25 Plugin (org.apache.maven.model.Plugin)12 File (java.io.File)7 MojoDescriptor (org.apache.maven.plugin.descriptor.MojoDescriptor)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 MojoFailureException (org.apache.maven.plugin.MojoFailureException)6 MavenProject (org.apache.maven.project.MavenProject)5 IOException (java.io.IOException)3 URLClassLoader (java.net.URLClassLoader)3 Artifact (org.apache.maven.artifact.Artifact)3 MavenSession (org.apache.maven.execution.MavenSession)3 MojoDescriptorCreator (org.apache.maven.lifecycle.internal.MojoDescriptorCreator)3 PluginVersionRequest (org.apache.maven.plugin.version.PluginVersionRequest)3 PluginVersionResolver (org.apache.maven.plugin.version.PluginVersionResolver)3 PluginInfo (org.apache.maven.plugins.help.DescribeMojo.PluginInfo)3 MavenPluginManagerHelper (org.apache.maven.reporting.exec.MavenPluginManagerHelper)3 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2