All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.tools.plugin.util.PluginUtils Maven / Gradle / Ivy

Go to download

The Maven Plugin Tools Extractor API provides an API to extract descriptor information from Maven Plugins.

There is a newer version: 4.0.0-beta-1
Show newest version
package org.apache.maven.tools.plugin.util;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

/**
 * Convenience methods to play with Maven plugins.
 *
 * @author jdcasey
 *
 */
public final class PluginUtils
{
    private PluginUtils()
    {
        // nop
    }

    /**
     * Expression associated with class types to recognize Maven objects (injected in fact as parameters by 
     * maven-core's PluginParameterExpressionEvaluator) like components ("real" components are injected by Plexus).
     * 
     * @deprecated wrong approach (fake components), documented parameter default values instead to learn people how to
     *             discover them
     */
    public static final Map MAVEN_COMPONENTS;
    static
    {
        Map mavenComponents = new HashMap<>();

        mavenComponents.put( "org.apache.maven.execution.MavenSession", "${session}" );
        mavenComponents.put( "org.apache.maven.project.MavenProject", "${project}" );
        mavenComponents.put( "org.apache.maven.plugin.MojoExecution", "${mojoExecution}" );
        mavenComponents.put( "org.apache.maven.plugin.descriptor.PluginDescriptor", "${plugin}" );
        mavenComponents.put( "org.apache.maven.settings.Settings", "${settings}" );
        
        MAVEN_COMPONENTS = Collections.unmodifiableMap( mavenComponents );
    }

    /**
     * @param basedir not null
     * @param include not null
     * @return list of included files with default SCM excluded files
     */
    public static String[] findSources( String basedir, String include )
    {
        return PluginUtils.findSources( basedir, include, null );
    }

    /**
     * @param basedir not null
     * @param include not null
     * @param exclude could be null
     * @return list of included files
     */
    public static String[] findSources( String basedir, String include, String exclude )
    {
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir( basedir );
        scanner.setIncludes( new String[] { include } );
        if ( !StringUtils.isEmpty( exclude ) )
        {
            scanner.setExcludes( new String[] { exclude, StringUtils.join( FileUtils.getDefaultExcludes(), "," ) } );
        }
        else
        {
            scanner.setExcludes( FileUtils.getDefaultExcludes() );
        }

        scanner.scan();

        return scanner.getIncludedFiles();
    }

    /**
     * Sorts the specified mojo descriptors by goal name.
     *
     * @param mojoDescriptors The mojo descriptors to sort, may be null.
     * @see MojoDescriptor#getGoal()
     */
    public static void sortMojos( List mojoDescriptors )
    {
        if ( mojoDescriptors != null )
        {
            Collections.sort( mojoDescriptors, new Comparator()
            {
                /** {@inheritDoc} */
                public int compare( MojoDescriptor mojo0, MojoDescriptor mojo1 )
                {
                    return mojo0.getGoal().compareToIgnoreCase( mojo1.getGoal() );
                }
            } );
        }
    }

    /**
     * Sorts the specified mojo parameters by name.
     *
     * @param parameters The mojo parameters to sort, may be null.
     * @see Parameter#getName()
     * @since 2.4.4
     */
    public static void sortMojoParameters( List parameters )
    {
        if ( parameters != null )
        {
            Collections.sort( parameters, new Comparator()
            {
                /** {@inheritDoc} */
                public int compare( Parameter parameter1, Parameter parameter2 )
                {
                    return parameter1.getName().compareToIgnoreCase( parameter2.getName() );
                }
            } );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy