Java Code Examples for org.apache.maven.plugin.descriptor.MojoDescriptor#getParameters()

The following examples show how to use org.apache.maven.plugin.descriptor.MojoDescriptor#getParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginExtractor.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the subset of the given configuration containing only the values accepted by the plugin/goal. The
 * configuration is modified in-place. The the extraction fail the configuration stays unchanged.
 *
 * @param mojo          the Wisdom mojo
 * @param plugin        the plugin object
 * @param goal          the goal / mojo
 * @param configuration the global configuration
 */
public static void extractEligibleConfigurationForGoal(AbstractWisdomMojo mojo,
                                                       Plugin plugin, String goal, Xpp3Dom configuration) {
    try {
        MojoDescriptor descriptor = mojo.pluginManager.getMojoDescriptor(plugin, goal,
                mojo.remoteRepos, mojo.repoSession);
        final List<Parameter> parameters = descriptor.getParameters();
        Xpp3Dom[] children = configuration.getChildren();
        if (children != null) {
            for (int i = children.length - 1; i >= 0; i--) {
                Xpp3Dom child = children[i];
                if (!contains(parameters, child.getName())) {
                    configuration.removeChild(i);
                }
            }
        }
    } catch (Exception e) {
        mojo.getLog().warn("Cannot extract the eligible configuration for goal " + goal + " from the " +
                "configuration");
        mojo.getLog().debug(e);
        // The configuration is not changed.
    }

}
 
Example 2
Source File: MojoExtension.java    From helm-maven-plugin with MIT License 4 votes vote down vote up
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) throws ParameterResolutionException {
    try {

        // get descriptor

        Class<? extends AbstractHelmMojo> mojoType = (Class<AbstractHelmMojo>) parameterContext.getParameter().getType();
        MojoDescriptor descriptor = mojoDescriptors.get(mojoType);
        assertNotNull(descriptor, "Plugin descriptor for " + mojoType.getSimpleName() + " not found, run 'maven-plugin-plugin:descriptor'.");

        // create mojo with default values

        AbstractHelmMojo mojo = spy(mojoType);
        for (Parameter parameter : descriptor.getParameters()) {
            if (parameter.getDefaultValue() == null || !parameter.isEditable() || parameter.getType().equals("boolean")) {
                continue;
            }
            getField(mojoType, parameter.getName()).set(mojo, resolve(context, parameter.getDefaultValue()));
        }

        // read mojo values from annotations

        MojoProperty[] mojoProperties = ArrayUtils.addAll(
                context.getRequiredTestClass().getAnnotationsByType(MojoProperty.class),
                context.getRequiredTestMethod().getAnnotationsByType(MojoProperty.class));
        for (MojoProperty mojoProperty : mojoProperties) {
            getField(mojoType, mojoProperty.name()).set(mojo, resolve(context, mojoProperty.value()));
        }

        // settings

        getField(mojoType, "settings").set(mojo, new Settings());
        
        // plexus SecDispatcher

        SecDispatcher secDispatcher = spy(DefaultSecDispatcher.class);
        FieldSetter.setField(secDispatcher, DefaultSecDispatcher.class.getDeclaredField("_cipher"), new DefaultPlexusCipher());
        getField(mojoType, "securityDispatcher").set(mojo, secDispatcher);

        // validate that every parameter is set

        for (Parameter paramter : descriptor.getParameters()) {
            if (paramter.isRequired()) {
                assertNotNull(
                        getField(mojoType, paramter.getName()).get(mojo),
                        "Parameter '" + paramter.getName() + "' not set for mojo '" + mojoType.getSimpleName() + "'.");
            }
        }

        return mojo;
    } catch (ReflectiveOperationException | PlexusCipherException e) {
        throw new ParameterResolutionException("Failed to setup mockito.", e);
    }
}