View Javadoc

1   package org.apache.maven.tools.plugin.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.plugin.descriptor.MojoDescriptor;
29  import org.apache.maven.plugin.descriptor.Parameter;
30  import org.codehaus.plexus.util.DirectoryScanner;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * Convenience methods to play with Maven plugins.
36   *
37   * @author jdcasey
38   * @version $Id: PluginUtils.java 1353228 2012-06-24 08:16:01Z hboutemy $
39   */
40  public final class PluginUtils
41  {
42      private PluginUtils()
43      {
44          // nop
45      }
46  
47      /**
48       * Expression associated with class types to recognize Maven objects (injected  in fact as parameters by
49       * <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
50       * maven-core's PluginParameterExpressionEvaluator</a>)
51       * like components ("real" components are injected by Plexus).
52       */
53      public static final Map<String, String> MAVEN_COMPONENTS;
54      static
55      {
56          Map<String, String> mavenComponents = new HashMap<String, String>();
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", "${mojo}" );
61          mavenComponents.put( "org.apache.maven.plugin.descriptor.PluginDescriptor", "${plugin}" );
62          mavenComponents.put( "org.apache.maven.settings.Settings", "${settings}" );
63          
64          MAVEN_COMPONENTS = Collections.unmodifiableMap( mavenComponents );
65      }
66  
67      /**
68       * @param basedir not null
69       * @param include not null
70       * @return list of included files with default SCM excluded files
71       */
72      public static String[] findSources( String basedir, String include )
73      {
74          return PluginUtils.findSources( basedir, include, null );
75      }
76  
77      /**
78       * @param basedir not null
79       * @param include not null
80       * @param exclude could be null
81       * @return list of included files
82       */
83      public static String[] findSources( String basedir, String include, String exclude )
84      {
85          DirectoryScanner scanner = new DirectoryScanner();
86          scanner.setBasedir( basedir );
87          scanner.setIncludes( new String[] { include } );
88          if ( !StringUtils.isEmpty( exclude ) )
89          {
90              scanner.setExcludes( new String[] { exclude, StringUtils.join( FileUtils.getDefaultExcludes(), "," ) } );
91          }
92          else
93          {
94              scanner.setExcludes( FileUtils.getDefaultExcludes() );
95          }
96  
97          scanner.scan();
98  
99          return scanner.getIncludedFiles();
100     }
101 
102     /**
103      * Sorts the specified mojo descriptors by goal name.
104      *
105      * @param mojoDescriptors The mojo descriptors to sort, may be <code>null</code>.
106      * @see MojoDescriptor#getGoal()
107      */
108     public static void sortMojos( List<MojoDescriptor> mojoDescriptors )
109     {
110         if ( mojoDescriptors != null )
111         {
112             Collections.sort( mojoDescriptors, new Comparator<MojoDescriptor>()
113             {
114                 /** {@inheritDoc} */
115                 public int compare( MojoDescriptor mojo0, MojoDescriptor mojo1 )
116                 {
117                     return mojo0.getGoal().compareToIgnoreCase( mojo1.getGoal() );
118                 }
119             } );
120         }
121     }
122 
123     /**
124      * Sorts the specified mojo parameters by name.
125      *
126      * @param parameters The mojo parameters to sort, may be <code>null</code>.
127      * @see Parameter#getName()
128      * @since 2.4.4
129      */
130     public static void sortMojoParameters( List<Parameter> parameters )
131     {
132         if ( parameters != null )
133         {
134             Collections.sort( parameters, new Comparator<Parameter>()
135             {
136                 /** {@inheritDoc} */
137                 public int compare( Parameter parameter1, Parameter parameter2 )
138                 {
139                     return parameter1.getName().compareToIgnoreCase( parameter2.getName() );
140                 }
141             } );
142         }
143     }
144 }