001    package 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    
022    import java.util.Collections;
023    import java.util.Comparator;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.apache.maven.plugin.descriptor.MojoDescriptor;
029    import org.apache.maven.plugin.descriptor.Parameter;
030    import org.codehaus.plexus.util.DirectoryScanner;
031    import org.codehaus.plexus.util.FileUtils;
032    import org.codehaus.plexus.util.StringUtils;
033    
034    /**
035     * Convenience methods to play with Maven plugins.
036     *
037     * @author jdcasey
038     * @version $Id: PluginUtils.java 1343086 2012-05-27 20:21:15Z hboutemy $
039     */
040    public final class PluginUtils
041    {
042        private PluginUtils()
043        {
044            // nop
045        }
046    
047        /**
048         * Expression associated with class types to recognize Maven objects as components.
049         */
050        public static final Map<String, String> MAVEN_COMPONENTS;
051        static
052        {
053            Map<String, String> mavenComponents = new HashMap<String, String>();
054    
055            mavenComponents.put( "org.apache.maven.execution.MavenSession", "${session}" );
056            mavenComponents.put( "org.apache.maven.project.MavenProject", "${project}" );
057            mavenComponents.put( "org.apache.maven.plugin.MojoExecution", "${mojo}" );
058            mavenComponents.put( "org.apache.maven.plugin.descriptor.PluginDescriptor", "${plugin}" );
059            mavenComponents.put( "org.apache.maven.settings.Settings", "${settings}" );
060            
061            MAVEN_COMPONENTS = Collections.unmodifiableMap( mavenComponents );
062        }
063    
064        /**
065         * @param basedir not null
066         * @param include not null
067         * @return list of included files with default SCM excluded files
068         */
069        public static String[] findSources( String basedir, String include )
070        {
071            return PluginUtils.findSources( basedir, include, null );
072        }
073    
074        /**
075         * @param basedir not null
076         * @param include not null
077         * @param exclude could be null
078         * @return list of included files
079         */
080        public static String[] findSources( String basedir, String include, String exclude )
081        {
082            DirectoryScanner scanner = new DirectoryScanner();
083            scanner.setBasedir( basedir );
084            scanner.setIncludes( new String[] { include } );
085            if ( !StringUtils.isEmpty( exclude ) )
086            {
087                scanner.setExcludes( new String[] { exclude, StringUtils.join( FileUtils.getDefaultExcludes(), "," ) } );
088            }
089            else
090            {
091                scanner.setExcludes( FileUtils.getDefaultExcludes() );
092            }
093    
094            scanner.scan();
095    
096            return scanner.getIncludedFiles();
097        }
098    
099        /**
100         * Sorts the specified mojo descriptors by goal name.
101         *
102         * @param mojoDescriptors The mojo descriptors to sort, may be <code>null</code>.
103         * @see MojoDescriptor#getGoal()
104         */
105        public static void sortMojos( List<MojoDescriptor> mojoDescriptors )
106        {
107            if ( mojoDescriptors != null )
108            {
109                Collections.sort( mojoDescriptors, new Comparator<MojoDescriptor>()
110                {
111                    /** {@inheritDoc} */
112                    public int compare( MojoDescriptor mojo0, MojoDescriptor mojo1 )
113                    {
114                        return mojo0.getGoal().compareToIgnoreCase( mojo1.getGoal() );
115                    }
116                } );
117            }
118        }
119    
120        /**
121         * Sorts the specified mojo parameters by name.
122         *
123         * @param parameters The mojo parameters to sort, may be <code>null</code>.
124         * @see Parameter#getName()
125         * @since 2.4.4
126         */
127        public static void sortMojoParameters( List<Parameter> parameters )
128        {
129            if ( parameters != null )
130            {
131                Collections.sort( parameters, new Comparator<Parameter>()
132                {
133                    /** {@inheritDoc} */
134                    public int compare( Parameter parameter1, Parameter parameter2 )
135                    {
136                        return parameter1.getName().compareToIgnoreCase( parameter2.getName() );
137                    }
138                } );
139            }
140        }
141    }