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