Example usage for org.apache.maven.plugin.descriptor PluginDescriptor getMojo

List of usage examples for org.apache.maven.plugin.descriptor PluginDescriptor getMojo

Introduction

In this page you can find the example usage for org.apache.maven.plugin.descriptor PluginDescriptor getMojo.

Prototype

public MojoDescriptor getMojo(String goal) 

Source Link

Usage

From source file:com.soebes.maven.plugins.iterator.IteratorMojo.java

License:Apache License

/**
 * Taken from MojoExecutor of Don Brown. Make it working with Maven 3.1.
 * //from ww  w .ja v  a  2  s . c o m
 * @param plugin
 * @param goal
 * @param configuration
 * @param env
 * @throws MojoExecutionException
 * @throws PluginResolutionException
 * @throws PluginDescriptorParsingException
 * @throws InvalidPluginDescriptorException
 * @throws PluginManagerException
 * @throws PluginConfigurationException
 * @throws MojoFailureException
 */
private void executeMojo(Plugin plugin, String goal, Xpp3Dom configuration) throws MojoExecutionException,
        PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException,
        MojoFailureException, PluginConfigurationException, PluginManagerException {

    if (configuration == null) {
        throw new NullPointerException("configuration may not be null");
    }

    PluginDescriptor pluginDescriptor = getPluginDescriptor(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 = mojoExecution(mojoDescriptor, configuration);
    pluginManager.executeMojo(getMavenSession(), exec);
}

From source file:io.fabric8.maven.docker.service.MojoExecutionService.java

License:Apache License

public void callPluginGoal(String fullGoal) throws MojoFailureException, MojoExecutionException {
    String[] parts = splitGoalSpec(fullGoal);
    Plugin plugin = project.getPlugin(parts[0]);
    String goal = parts[1];//from  ww  w.  j av  a  2  s.  com

    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);
    }
}

From source file:io.sarl.maven.compiler.AbstractSarlMojo.java

License:Apache License

/** Execute another MOJO.
 *
 * @param groupId - identifier of the MOJO plugin group.
 * @param artifactId - identifier of the MOJO plugin artifact.
 * @param version - version of the MOJO plugin version.
 * @param goal - the goal to run./* www  .j  a v  a  2s .  c om*/
 * @param configuration - the XML code for the configuration.
 * @param dependencies - the dependencies of the plugin.
 * @throws MojoExecutionException when cannot run the MOJO.
 * @throws MojoFailureException when the build failed.
 */
protected void executeMojo(String groupId, String artifactId, String version, String goal, String configuration,
        Dependency... dependencies) throws MojoExecutionException, MojoFailureException {
    final Plugin plugin = new Plugin();
    plugin.setArtifactId(artifactId);
    plugin.setGroupId(groupId);
    plugin.setVersion(version);
    plugin.setDependencies(Arrays.asList(dependencies));

    getLog().debug(Locale.getString(AbstractSarlMojo.class, "LAUNCHING", plugin.getId())); //$NON-NLS-1$

    final PluginDescriptor pluginDescriptor = this.mavenHelper.loadPlugin(plugin);
    if (pluginDescriptor == null) {
        throw new MojoExecutionException(
                Locale.getString(AbstractSarlMojo.class, "PLUGIN_NOT_FOUND", plugin.getId())); //$NON-NLS-1$
    }
    final MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
    if (mojoDescriptor == null) {
        throw new MojoExecutionException(Locale.getString(AbstractSarlMojo.class, "GOAL_NOT_FOUND", goal)); //$NON-NLS-1$
    }

    final Xpp3Dom mojoXml;
    try {
        mojoXml = toXpp3Dom(mojoDescriptor.getMojoConfiguration());
    } catch (PlexusConfigurationException e1) {
        throw new MojoExecutionException(e1.getLocalizedMessage(), e1);
    }
    Xpp3Dom configurationXml = null;
    if (configuration != null && !configuration.isEmpty()) {
        try (StringReader sr = new StringReader(configuration)) {
            try {
                configurationXml = Xpp3DomBuilder.build(sr);
            } catch (XmlPullParserException | IOException e) {
                getLog().debug(e);
            }
        }
    }
    if (configurationXml != null) {
        configurationXml = Xpp3DomUtils.mergeXpp3Dom(configurationXml, mojoXml);
    } else {
        configurationXml = mojoXml;
    }

    getLog().debug(Locale.getString(AbstractSarlMojo.class, "CONFIGURATION_FOR", //$NON-NLS-1$
            plugin.getId(), configurationXml.toString()));

    final MojoExecution execution = new MojoExecution(mojoDescriptor, configurationXml);

    this.mavenHelper.executeMojo(execution);
}

From source file:io.takari.maven.plugins.configurator.MojoConfigurationProcessor.java

License:Open Source License

public PlexusConfiguration mojoConfigurationFor(Object mojoInstance,
        PlexusConfiguration pluginConfigurationFromMaven) throws ComponentConfigurationException {
    try (InputStream is = mojoInstance.getClass().getResourceAsStream("/META-INF/maven/plugin.xml")) {
        PluginDescriptor pd = pluginDescriptorBuilder.build(new InputStreamReader(is, "UTF-8")); // closes input stream too
        String goal = determineGoal(mojoInstance.getClass().getName(), pd);
        PlexusConfiguration defaultMojoConfiguration = pd.getMojo(goal).getMojoConfiguration();
        PlexusConfiguration mojoConfiguration = extractAndMerge(goal, pluginConfigurationFromMaven,
                defaultMojoConfiguration);
        return mojoConfiguration;
    } catch (Exception e) {
        throw new ComponentConfigurationException(e);
    }//ww w. j  av  a  2 s  . c  o  m
}

From source file:io.wcm.maven.plugins.cq.InstallMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    Plugin plugin = new Plugin();
    plugin.setGroupId("org.apache.sling");
    plugin.setArtifactId("maven-sling-plugin");
    plugin.setVersion(this.slingPluginVersion);

    try {//from w  w w .  jav a  2 s . c om
        PluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptor(plugin,
                project.getRemotePluginRepositories(), session.getRepositorySession());
        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo("install");
        MojoExecution mojoExecution = new MojoExecution(pluginDescriptor.getMojo("install"));

        Xpp3Dom config = convertConfiguration(mojoDescriptor.getMojoConfiguration());
        config.getChild("slingUrl").setValue(this.slingConsoleUrl);
        config.getChild("user").setValue(this.slingConsoleUser);
        config.getChild("password").setValue(this.slingConsolePassword);
        config.getChild("mountByFS").setValue("false");
        mojoExecution.setConfiguration(config);

        buildPluginManager.executeMojo(session, mojoExecution);
    } catch (Throwable ex) {
        throw new MojoExecutionException("Faild to execute plugin: " + plugin, ex);
    }

}

From source file:org.codehaus.mojo.sonar.Bootstraper.java

License:Open Source License

private void executeMojo(MavenProject project, MavenSession session) throws MojoExecutionException {
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {//  ww w  .java2s  .  com

        RepositoryRequest repositoryRequest = new DefaultRepositoryRequest();
        repositoryRequest.setLocalRepository(session.getLocalRepository());
        repositoryRequest.setRemoteRepositories(project.getPluginArtifactRepositories());

        Plugin plugin = createSonarPlugin();

        List<RemoteRepository> remoteRepositories = session.getCurrentProject().getRemotePluginRepositories();

        PluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptor(plugin, remoteRepositories,
                session.getRepositorySession());

        String goal = "sonar";

        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
        if (mojoDescriptor == null) {
            throw new MojoExecutionException("Unknown mojo goal: " + goal);
        }
        MojoExecution mojoExecution = new MojoExecution(plugin, goal, "sonar" + goal);

        mojoExecution.setConfiguration(convert(mojoDescriptor));

        mojoExecution.setMojoDescriptor(mojoDescriptor);

        // olamy : we exclude nothing and import nothing regarding realm import and artifacts

        DependencyFilter artifactFilter = new DependencyFilter() {
            public boolean accept(DependencyNode arg0, List<DependencyNode> arg1) {
                return true;
            }
        };

        pluginManager.setupPluginRealm(pluginDescriptor, session,
                Thread.currentThread().getContextClassLoader(), Collections.<String>emptyList(),
                artifactFilter);

        Mojo mojo = pluginManager.getConfiguredMojo(Mojo.class, session, mojoExecution);
        Thread.currentThread().setContextClassLoader(pluginDescriptor.getClassRealm());
        mojo.execute();

    } catch (Exception e) {
        throw new MojoExecutionException("Can not execute Sonar", e);
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}

From source file:org.eclipse.m2e.editor.xml.internal.mojo.MojoParameterMetadataProvider.java

License:Open Source License

List<MojoParameter> loadMojoParameters(PluginDescriptor desc, String goal, IProgressMonitor monitor)
        throws CoreException {

    PlexusConfigHelper helper = new PlexusConfigHelper();

    if (goal.equals("*")) { //$NON-NLS-1$
        List<MojoParameter> parameters = new ArrayList<>();
        Set<String> collected = new HashSet<>();
        for (MojoDescriptor mojo : desc.getMojos()) {
            for (MojoParameter p : loadMojoParameters(desc, mojo, helper, monitor)) {
                if (collected.add(p.getName())) {
                    parameters.add(p);//from w  ww. jav a2  s . c om
                }
            }
        }
        return parameters;
    }
    return loadMojoParameters(desc, desc.getMojo(goal), helper, monitor);
}

From source file:org.jszip.maven.AbstractJSZipMojo.java

License:Apache License

protected MojoDescriptor findMojoDescriptor(PluginDescriptor pluginDescriptor,
        Class<? extends Mojo> mojoClass) {
    MojoDescriptor mojoDescriptor = null;
    for (MojoDescriptor d : pluginDescriptor.getMojos()) {
        if (mojoClass.getName().equals(d.getImplementation())) {
            mojoDescriptor = pluginDescriptor.getMojo(d.getGoal());
            break;
        }//from   ww w . ja  v a  2s. c  o m
    }

    if (mojoDescriptor == null) {
        getLog().error("Cannot find goal that corresponds to " + mojoClass);
        throw new IllegalStateException("This plugin should always have the " + mojoClass.getName() + " goal");
    }
    return mojoDescriptor;
}

From source file:org.twdata.maven.mojoexecutor.MojoExecutor.java

License:Apache License

/**
 * Entry point for executing a mojo/*from w w w .  j ava  2 s . com*/
 *
 * @param plugin        The plugin to execute
 * @param goal          The goal to execute
 * @param configuration The execution configuration
 * @param env           The execution environment
 * @throws MojoExecutionException If there are any exceptions locating or executing the mojo
 */
public static void executeMojo(Plugin plugin, String goal, Xpp3Dom configuration, ExecutionEnvironment env)
        throws MojoExecutionException {
    if (configuration == null) {
        throw new NullPointerException("configuration may not be null");
    }
    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);
        }

        MavenSession session = env.getMavenSession();

        PluginDescriptor pluginDescriptor = env.getPluginManager().loadPlugin(plugin,
                env.getMavenProject().getRemotePluginRepositories(), session.getRepositorySession());
        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 = mojoExecution(mojoDescriptor, executionId, configuration);
        env.getPluginManager().executeMojo(session, exec);
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to execute mojo", e);
    }
}

From source file:org.universAAL.maven.MyMojoExecutorV15.java

License:Apache License

/**
 * Entry point for executing a mojo//from w w w .j a v a  2s . c om
 * 
 * @param plugin
 *            The plugin to execute
 * @param goal
 *            The goal to execute
 * @param configuration
 *            The execution configuration
 * @param env
 *            The execution environment
 * @throws MojoExecutionException
 *             If there are any exceptions locating or executing the mojo
 */
public static void executeMojoImpl(final Plugin plugin, final String goal, final Xpp3Dom configuration,
        final ExecutionEnvironmentM2 env) throws MojoExecutionException {
    Map<String, PluginExecution> executionMap = null;
    try {
        MavenSession session = env.getMavenSession();

        List buildPlugins = env.getMavenProject().getBuildPlugins();

        String executionId = null;
        if (goal != null && goal.length() > 0 && goal.indexOf('#') > -1) {
            int pos = goal.indexOf('#');
            executionId = goal.substring(pos + 1);
            String newgoal = goal.substring(0, pos);
            System.out.println("Executing goal " + newgoal + " with execution ID " + executionId);
        }

        // You'd think we could just add the configuration to the mojo
        // execution, but then it merges with the plugin
        // config dominate over the mojo config, so we are forced to fake
        // the config as if it was declared as an
        // execution in the pom so that the merge happens correctly
        if (buildPlugins != null && executionId == null) {
            for (Object buildPlugin : buildPlugins) {
                Plugin pomPlugin = (Plugin) buildPlugin;

                if (plugin.getGroupId().equals(pomPlugin.getGroupId())
                        && plugin.getArtifactId().equals(pomPlugin.getArtifactId())) {
                    PluginExecution exec = new PluginExecution();
                    exec.setConfiguration(configuration);
                    executionMap = getExecutionsAsMap(pomPlugin);
                    executionMap.put(FAKE_EXECUTION_ID, exec);
                    executionId = FAKE_EXECUTION_ID;
                    break;
                }
            }
        }

        PluginDescriptor pluginDescriptor = env.getPluginManager().verifyPlugin(plugin, env.getMavenProject(),
                session.getSettings(), session.getLocalRepository());
        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal);
        if (mojoDescriptor == null) {
            throw new MojoExecutionException("Unknown mojo goal: " + goal);
        }
        MojoExecution exec = mojoExecution2(mojoDescriptor, executionId, configuration);
        env.getPluginManager().executeMojo(env.getMavenProject(), exec, env.getMavenSession());
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to execute mojo", e);
    } finally {
        if (executionMap != null) {
            executionMap.remove(FAKE_EXECUTION_ID);
        }
    }
}