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

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

Introduction

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

Prototype

public MojoDescriptor() 

Source Link

Document

Default constructor.

Usage

From source file:com.github.shyiko.sme.ServersExtension.java

License:Apache License

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
    MojoExecution mojoExecution = new MojoExecution(new MojoDescriptor());
    ExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
    Properties userProperties = session.getUserProperties();

    boolean exportAsSysProp = isExtensionProperty(session, "servers.exportAsSysProp");

    Map<String, String> properties = new HashMap<String, String>();
    try {/*from   ww w. j  a v  a  2 s. c  o m*/
        for (Server server : session.getSettings().getServers()) {
            String serverId = server.getId();
            for (String field : FIELDS) {
                String[] aliases = getAliases(serverId, field);
                String fieldNameWithFirstLetterCapitalized = upperCaseFirstLetter(field);
                String fieldValue = (String) Server.class.getMethod("get" + fieldNameWithFirstLetterCapitalized)
                        .invoke(server);
                if (fieldValue != null) {
                    fieldValue = decryptInlinePasswords(fieldValue);
                }
                for (String alias : aliases) {
                    String userPropertyValue = userProperties.getProperty(alias);
                    if (userPropertyValue != null) {
                        fieldValue = userPropertyValue;
                        break;
                    }
                }
                String resolvedValue = (String) expressionEvaluator.evaluate(fieldValue);
                Server.class
                        .getMethod("set" + fieldNameWithFirstLetterCapitalized, new Class[] { String.class })
                        .invoke(server, resolvedValue);
                if (resolvedValue != null) {
                    for (String alias : aliases) {
                        properties.put(alias, resolvedValue);
                    }
                }
            }
        }

        if (exportAsSysProp) {
            System.getProperties().putAll(properties);
        } else {
            for (MavenProject project : session.getProjects()) {
                Properties projectProperties = project.getProperties();
                projectProperties.putAll(properties);
            }
        }
    } catch (Exception e) {
        throw new MavenExecutionException("Failed to expose settings.servers.*", e);
    }
}

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  .  ja va 2 s.  co 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);//from w  ww . j  ava2 s  . co  m
        descriptor.setPhase(phase);
        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.codehaus.gmaven.plugin.execute.ExpressionEvaluatorImpl.java

License:Apache License

public ExpressionEvaluatorImpl(final MavenSession context, final MavenProject project) {
    this.context = context;
    this.mojoExecution = new MojoExecution(new MojoDescriptor());
    this.pathTranslator = lookupPathTranslator();
    this.project = project;
    this.basedir = lookupBasedir();
}

From source file:org.codehaus.mojo.hibernate3.util.HibernateExpressionEvaluator.java

License:Apache License

public HibernateExpressionEvaluator(MavenSession session) {
    super(session, new MojoExecution(new MojoDescriptor()), new DefaultPathTranslator(), null,
            session.getCurrentProject(), session.getExecutionProperties());
}

From source file:org.dthume.maven.xpom.impl.XPOMUtil.java

License:Apache License

public static ExpressionEvaluator expressionEvaluatorForSession(final MavenSession session) {
    final MojoExecution execution = new MojoExecution(new MojoDescriptor());
    final PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session,
            execution);/* ww w. jav  a  2 s. co m*/
    return new DefaultExpressionEvaluator(evaluator);
}

From source file:org.dthume.maven.xpom.mojo.AbstractXPOMMojo.java

License:Apache License

protected final ExpressionEvaluator getExpressionEvaluator() {
    final MojoExecution execution = new MojoExecution(new MojoDescriptor());
    final PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session,
            execution);//from  www.ja  v a2 s . c  om
    return new DefaultExpressionEvaluator(evaluator);
}

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/*  w  w w  . java  2 s  .  c  om*/
 * @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.opencredo.maven.plugins.enforcer.EnforcerExpressionEvaluator.java

License:Apache License

/**
 * The Constructor.//from w  w  w.j a v  a  2 s .  c om
 *
 * @param theContext the the context
 * @param thePathTranslator the the path translator
 * @param theProject the the project
 */
public EnforcerExpressionEvaluator(MavenSession theContext, PathTranslator thePathTranslator,
        MavenProject theProject) {
    super(theContext, new MojoExecution(new MojoDescriptor()), thePathTranslator, null, theProject,
            theContext.getExecutionProperties());
}

From source file:org.sourcepit.maven.bootstrap.core.AbstractBootstrapper.java

License:Apache License

private void ensureDependenciesAreResolved(MavenSession session) {
    final MojoDescriptor mojoDescriptor = new MojoDescriptor();
    mojoDescriptor.setAggregator(false);
    // mojoDescriptor.setDependencyCollectionRequired(getDependencyResolutionRequired());
    mojoDescriptor.setDependencyResolutionRequired(getDependencyResolutionRequired());

    final MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);

    final DependencyContext dependencyContext = mojoExecutor.newDependencyContext(session,
            Collections.singletonList(mojoExecution));

    try {//from   w  ww .ja  v a  2s . co m
        mojoExecutor.ensureDependenciesAreResolved(mojoDescriptor, session, dependencyContext);
    } catch (LifecycleExecutionException e) {
        throw new IllegalStateException(e);
    }
}