Example usage for org.apache.maven.plugin.descriptor MojoDescriptor setGoal

List of usage examples for org.apache.maven.plugin.descriptor MojoDescriptor setGoal

Introduction

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

Prototype

public void setGoal(String goal) 

Source Link

Usage

From source file:com.photon.maven.plugins.android.AbstractAndroidMojoTestCase.java

License:Apache License

/**
 * Copy the project specified into a temporary testing directory. Create the {@link MavenProject} and
 * {@link ManifestUpdateMojo}, configure it from the <code>plugin-config.xml</code> and return the created Mojo.
 * <p>// w  ww .j a  v  a 2 s. c  o m
 * Note: only configuration entries supplied in the plugin-config.xml are presently configured in the mojo returned.
 * That means and 'default-value' settings are not automatically injected by this testing framework (or plexus
 * underneath that is suppling this functionality)
 * 
 * @param resourceProject
 *            the name of the goal to look for in the <code>plugin-config.xml</code> that the configuration will be
 *            pulled from.
 * @param resourceProject
 *            the resourceProject path (in src/test/resources) to find the example/test project.
 * @return the created mojo (unexecuted)
 * @throws Exception
 *             if there was a problem creating the mojo.
 */
protected T createMojo(String resourceProject) throws Exception {
    // Establish test details project example
    String testResourcePath = "src/test/resources/" + resourceProject;
    testResourcePath = FilenameUtils.separatorsToSystem(testResourcePath);
    File exampleDir = new File(getBasedir(), testResourcePath);
    Assert.assertTrue("Path should exist: " + exampleDir, exampleDir.exists());

    // Establish the temporary testing directory.
    String testingPath = "target/tests/" + this.getClass().getSimpleName() + "." + getName();
    testingPath = FilenameUtils.separatorsToSystem(testingPath);
    File testingDir = new File(getBasedir(), testingPath);

    if (testingDir.exists()) {
        FileUtils.cleanDirectory(testingDir);
    } else {
        Assert.assertTrue("Could not create directory: " + testingDir, testingDir.mkdirs());
    }

    // Copy project example into temporary testing directory
    // to avoid messing up the good source copy, as mojo can change
    // the AndroidManifest.xml file.
    FileUtils.copyDirectory(exampleDir, testingDir);

    // Prepare MavenProject
    final MavenProject project = new MojoProjectStub(testingDir);

    // Setup Mojo
    PlexusConfiguration config = extractPluginConfiguration("android-maven-plugin", project.getFile());
    @SuppressWarnings("unchecked")
    final T mojo = (T) lookupMojo(getPluginGoalName(), project.getFile());

    // Inject project itself
    setVariableValueToObject(mojo, "project", project);

    // Configure the rest of the pieces via the PluginParameterExpressionEvaluator
    //  - used for ${plugin.*}
    MojoDescriptor mojoDesc = new MojoDescriptor();
    // - used for error messages in PluginParameterExpressionEvaluator
    mojoDesc.setGoal(getPluginGoalName());
    MojoExecution mojoExec = new MojoExecution(mojoDesc);
    // - Only needed if we start to use expressions like ${settings.*}, ${localRepository}, ${reactorProjects}
    // MavenSession context = null; // Messy to declare, would rather avoid using it.
    // - Used for ${basedir} relative paths
    PathTranslator pathTranslator = new DefaultPathTranslator();
    // - Declared to prevent NPE from logging events in maven core
    Logger logger = new ConsoleLogger(Logger.LEVEL_DEBUG, mojo.getClass().getName());

    MavenSession context = createMock(MavenSession.class);

    expect(context.getExecutionProperties()).andReturn(project.getProperties());
    expect(context.getCurrentProject()).andReturn(project);
    replay(context);

    // Declare evalator that maven itself uses.
    ExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(context, mojoExec, pathTranslator,
            logger, project, project.getProperties());
    // Lookup plexus configuration component
    ComponentConfigurator configurator = (ComponentConfigurator) lookup(ComponentConfigurator.ROLE, "basic");
    // Configure mojo using above
    ConfigurationListener listener = new DebugConfigurationListener(logger);
    configurator.configureComponent(mojo, config, evaluator, getContainer().getContainerRealm(), listener);

    return mojo;
}

From source file:fr.jcgay.maven.plugin.buildplan.model.builder.MojoExecutionBuilder.java

License:Apache License

public MojoExecution build() {

    if (useMojoDescriptorConstructor) {

        MojoDescriptor descriptor = new MojoDescriptor();
        descriptor.setGoal(goal);
        descriptor.setPhase(phase);/*from ww w.j  a  va2  s .  c o  m*/
        PluginDescriptor pluginDescriptor = new PluginDescriptor();
        pluginDescriptor.setArtifactId(artifactId);
        descriptor.setPluginDescriptor(pluginDescriptor);

        return new MojoExecution(descriptor, executionId);
    } else {

        Plugin plugin = new Plugin();
        plugin.setArtifactId(artifactId);
        return new MojoExecution(plugin, goal, executionId);
    }
}

From source file:org.efaps.maven_java5.EfapsAnnotationDescriptorExtractor.java

License:Open Source License

/**
 * Scan given class name for a mojo (using the annotations).
 *
 * @param _cl         class loader// www.  j  a  va 2s.c  o  m
 * @param _className  class name
 * @return mojo descriptor, or <code>null</code> if the given class is not a
 *         mojo
 * @throws InvalidPluginDescriptorException
 */
private MojoDescriptor scan(final ClassLoader _cl, final String _className)
        throws InvalidPluginDescriptorException {
    final MojoDescriptor mojoDescriptor;
    Class<?> c;
    try {
        c = _cl.loadClass(_className);
    } catch (final ClassNotFoundException e) {
        throw new InvalidPluginDescriptorException("Error scanning class " + _className, e);
    }

    final Goal goalAnno = c.getAnnotation(Goal.class);
    if (goalAnno == null) {
        getLogger().debug("  Not a mojo: " + c.getName());
        mojoDescriptor = null;
    } else {
        mojoDescriptor = new MojoDescriptor();
        mojoDescriptor.setRole(Mojo.ROLE);
        mojoDescriptor.setImplementation(c.getName());
        mojoDescriptor.setLanguage("java");
        mojoDescriptor.setInstantiationStrategy(goalAnno.instantiationStrategy());
        mojoDescriptor.setExecutionStrategy(goalAnno.executionStrategy());
        mojoDescriptor.setGoal(goalAnno.name());
        mojoDescriptor.setAggregator(goalAnno.aggregator());
        mojoDescriptor.setDependencyResolutionRequired(goalAnno.requiresDependencyResolutionScope());
        mojoDescriptor.setDirectInvocationOnly(goalAnno.requiresDirectInvocation());
        mojoDescriptor.setProjectRequired(goalAnno.requiresProject());
        mojoDescriptor.setOnlineRequired(goalAnno.requiresOnline());
        mojoDescriptor.setInheritedByDefault(goalAnno.inheritByDefault());

        if (!Phase.VOID.equals(goalAnno.defaultPhase())) {
            mojoDescriptor.setPhase(goalAnno.defaultPhase().key());
        }

        final Deprecated deprecatedAnno = c.getAnnotation(Deprecated.class);

        if (deprecatedAnno != null) {
            mojoDescriptor.setDeprecated("true");
        }

        final Execute executeAnno = c.getAnnotation(Execute.class);

        if (executeAnno != null) {
            final String lifecycle = nullify(executeAnno.lifecycle());
            mojoDescriptor.setExecuteLifecycle(lifecycle);

            if (Phase.VOID.equals(executeAnno.phase())) {
                mojoDescriptor.setExecutePhase(executeAnno.phase().key());
            }

            final String customPhase = executeAnno.customPhase();

            if (customPhase.length() > 0) {
                if (!Phase.VOID.equals(executeAnno.phase())) {
                    getLogger().warn("Custom phase is overriding \"phase\" field.");
                }
                if (lifecycle == null) {
                    getLogger().warn(
                            "Setting a custom phase without a lifecycle is prone to error. If the phase is not custom, set the \"phase\" field instead.");
                }
                mojoDescriptor.setExecutePhase(executeAnno.customPhase());
            }

            mojoDescriptor.setExecuteGoal(nullify(executeAnno.goal()));
        }

        Class<?> cur = c;
        while (!Object.class.equals(cur)) {
            attachFieldParameters(cur, mojoDescriptor);
            cur = cur.getSuperclass();
        }

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("  Component found: " + mojoDescriptor.getHumanReadableKey());
        }
    }

    return mojoDescriptor;
}

From source file:org.sonatype.tycho.m2e.felix.internal.MavenBundlePluginConfigurator.java

License:Open Source License

protected static MojoExecution amendMojoExecution(MavenProject mavenProject, MojoExecution execution,
        Map<String, String> instructions) {
    if ("bundle".equals(execution.getGoal())) {
        // do not generate complete bundle. this is both slow and can produce unexpected workspace changes
        // that will trigger unexpected/endless workspace build.
        // we rely on the fact that ManifestPlugin mojo extends BundlePlugin and does not introduce any
        // additional required parameters, so can run manifest goal in place of bundle goal.
        MojoDescriptor descriptor = execution.getMojoDescriptor().clone();
        descriptor.setGoal("manifest");
        descriptor.setImplementation("org.apache.felix.bundleplugin.ManifestPlugin");
        MojoExecution _execution = new MojoExecution(execution.getPlugin(), "manifest",
                "m2e-tycho:" + execution.getExecutionId() + ":manifest");
        _execution.setConfiguration(execution.getConfiguration());
        _execution.setMojoDescriptor(descriptor);
        _execution.setLifecyclePhase(execution.getLifecyclePhase());
        execution = _execution;/*from w  w w  . jav  a  2 s.com*/
    }

    Xpp3Dom configuration = new Xpp3Dom(execution.getConfiguration());
    if (VERSION_2_3_6.compareTo(new DefaultArtifactVersion(execution.getVersion())) <= 0) {
        setBoolean(configuration, "rebuildBundle", true);
    }

    if (isDeclerativeServices(mavenProject.getBasedir(), instructions)) {
        setBoolean(configuration, "unpackBundle", true);
    }

    execution.setConfiguration(configuration);

    return execution;
}