View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.tools.plugin.util;
20  
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.apache.maven.plugin.descriptor.Parameter;
29  import org.codehaus.plexus.util.DirectoryScanner;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * Convenience methods to play with Maven plugins.
35   *
36   * @author jdcasey
37   *
38   */
39  public final class PluginUtils {
40      private PluginUtils() {
41          // nop
42      }
43  
44      /**
45       * Expression associated with class types to recognize Maven objects (injected in fact as parameters by <a
46       * href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
47       * maven-core's PluginParameterExpressionEvaluator</a>) like components ("real" components are injected by Plexus).
48       *
49       * @deprecated wrong approach (fake components), documented parameter default values instead to learn people how to
50       *             discover them
51       */
52      @Deprecated
53      public static final Map<String, String> MAVEN_COMPONENTS;
54  
55      static {
56          Map<String, String> mavenComponents = new HashMap<>();
57  
58          mavenComponents.put("org.apache.maven.execution.MavenSession", "${session}");
59          mavenComponents.put("org.apache.maven.project.MavenProject", "${project}");
60          mavenComponents.put("org.apache.maven.plugin.MojoExecution", "${mojoExecution}");
61          mavenComponents.put("org.apache.maven.plugin.descriptor.PluginDescriptor", "${plugin}");
62          mavenComponents.put("org.apache.maven.settings.Settings", "${settings}");
63  
64          mavenComponents.put("org.apache.maven.api.Session", "${session}");
65          mavenComponents.put("org.apache.maven.api.Project", "${project}");
66          mavenComponents.put("org.apache.maven.api.MojoExecution", "${mojoExecution}");
67          // TODO: apiv4: add PluginDescriptor to the api ?
68          // mavenComponents.put( "org.apache.maven.api.descriptor.PluginDescriptor", "${plugin}" );
69          mavenComponents.put("org.apache.maven.api.settings.Settings", "${settings}");
70  
71          MAVEN_COMPONENTS = Collections.unmodifiableMap(mavenComponents);
72      }
73  
74      /**
75       * @param basedir not null
76       * @param include not null
77       * @return list of included files with default SCM excluded files
78       */
79      public static String[] findSources(String basedir, String include) {
80          return PluginUtils.findSources(basedir, include, null);
81      }
82  
83      /**
84       * @param basedir not null
85       * @param include not null
86       * @param exclude could be null
87       * @return list of included files
88       */
89      public static String[] findSources(String basedir, String include, String exclude) {
90          DirectoryScanner scanner = new DirectoryScanner();
91          scanner.setBasedir(basedir);
92          scanner.setIncludes(new String[] {include});
93          if (!(exclude == null || exclude.isEmpty())) {
94              scanner.setExcludes(new String[] {exclude, StringUtils.join(FileUtils.getDefaultExcludes(), ",")});
95          } else {
96              scanner.setExcludes(FileUtils.getDefaultExcludes());
97          }
98  
99          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 }