001package org.apache.maven.tools.plugin.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collections;
023import java.util.Comparator;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.maven.plugin.descriptor.MojoDescriptor;
029import org.apache.maven.plugin.descriptor.Parameter;
030import org.codehaus.plexus.util.DirectoryScanner;
031import org.codehaus.plexus.util.FileUtils;
032import org.codehaus.plexus.util.StringUtils;
033
034/**
035 * Convenience methods to play with Maven plugins.
036 *
037 * @author jdcasey
038 * @version $Id: PluginUtils.html 907055 2014-04-27 15:36:06Z hboutemy $
039 */
040public final class PluginUtils
041{
042    private PluginUtils()
043    {
044        // nop
045    }
046
047    /**
048     * Expression associated with class types to recognize Maven objects (injected in fact as parameters by <a
049     * href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
050     * maven-core's PluginParameterExpressionEvaluator</a>) like components ("real" components are injected by Plexus).
051     * 
052     * @deprecated wrong approach (fake components), documented parameter default values instead to learn people how to
053     *             discover them
054     */
055    public static final Map<String, String> MAVEN_COMPONENTS;
056    static
057    {
058        Map<String, String> mavenComponents = new HashMap<String, String>();
059
060        mavenComponents.put( "org.apache.maven.execution.MavenSession", "${session}" );
061        mavenComponents.put( "org.apache.maven.project.MavenProject", "${project}" );
062        mavenComponents.put( "org.apache.maven.plugin.MojoExecution", "${mojoExecution}" );
063        mavenComponents.put( "org.apache.maven.plugin.descriptor.PluginDescriptor", "${plugin}" );
064        mavenComponents.put( "org.apache.maven.settings.Settings", "${settings}" );
065        
066        MAVEN_COMPONENTS = Collections.unmodifiableMap( mavenComponents );
067    }
068
069    /**
070     * @param basedir not null
071     * @param include not null
072     * @return list of included files with default SCM excluded files
073     */
074    public static String[] findSources( String basedir, String include )
075    {
076        return PluginUtils.findSources( basedir, include, null );
077    }
078
079    /**
080     * @param basedir not null
081     * @param include not null
082     * @param exclude could be null
083     * @return list of included files
084     */
085    public static String[] findSources( String basedir, String include, String exclude )
086    {
087        DirectoryScanner scanner = new DirectoryScanner();
088        scanner.setBasedir( basedir );
089        scanner.setIncludes( new String[] { include } );
090        if ( !StringUtils.isEmpty( exclude ) )
091        {
092            scanner.setExcludes( new String[] { exclude, StringUtils.join( FileUtils.getDefaultExcludes(), "," ) } );
093        }
094        else
095        {
096            scanner.setExcludes( FileUtils.getDefaultExcludes() );
097        }
098
099        scanner.scan();
100
101        return scanner.getIncludedFiles();
102    }
103
104    /**
105     * Sorts the specified mojo descriptors by goal name.
106     *
107     * @param mojoDescriptors The mojo descriptors to sort, may be <code>null</code>.
108     * @see MojoDescriptor#getGoal()
109     */
110    public static void sortMojos( List<MojoDescriptor> mojoDescriptors )
111    {
112        if ( mojoDescriptors != null )
113        {
114            Collections.sort( mojoDescriptors, new Comparator<MojoDescriptor>()
115            {
116                /** {@inheritDoc} */
117                public int compare( MojoDescriptor mojo0, MojoDescriptor mojo1 )
118                {
119                    return mojo0.getGoal().compareToIgnoreCase( mojo1.getGoal() );
120                }
121            } );
122        }
123    }
124
125    /**
126     * Sorts the specified mojo parameters by name.
127     *
128     * @param parameters The mojo parameters to sort, may be <code>null</code>.
129     * @see Parameter#getName()
130     * @since 2.4.4
131     */
132    public static void sortMojoParameters( List<Parameter> parameters )
133    {
134        if ( parameters != null )
135        {
136            Collections.sort( parameters, new Comparator<Parameter>()
137            {
138                /** {@inheritDoc} */
139                public int compare( Parameter parameter1, Parameter parameter2 )
140                {
141                    return parameter1.getName().compareToIgnoreCase( parameter2.getName() );
142                }
143            } );
144        }
145    }
146}