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 *
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 <a
49 * href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
50 * maven-core's PluginParameterExpressionEvaluator</a>) like components ("real" components are injected by Plexus).
51 *
52 * @deprecated wrong approach (fake components), documented parameter default values instead to learn people how to
53 * discover them
54 */
55 @Deprecated
56 public static final Map<String, String> MAVEN_COMPONENTS;
57 static
58 {
59 Map<String, String> mavenComponents = new HashMap<>();
60
61 mavenComponents.put( "org.apache.maven.execution.MavenSession", "${session}" );
62 mavenComponents.put( "org.apache.maven.project.MavenProject", "${project}" );
63 mavenComponents.put( "org.apache.maven.plugin.MojoExecution", "${mojoExecution}" );
64 mavenComponents.put( "org.apache.maven.plugin.descriptor.PluginDescriptor", "${plugin}" );
65 mavenComponents.put( "org.apache.maven.settings.Settings", "${settings}" );
66
67 MAVEN_COMPONENTS = Collections.unmodifiableMap( mavenComponents );
68 }
69
70 /**
71 * @param basedir not null
72 * @param include not null
73 * @return list of included files with default SCM excluded files
74 */
75 public static String[] findSources( String basedir, String include )
76 {
77 return PluginUtils.findSources( basedir, include, null );
78 }
79
80 /**
81 * @param basedir not null
82 * @param include not null
83 * @param exclude could be null
84 * @return list of included files
85 */
86 public static String[] findSources( String basedir, String include, String exclude )
87 {
88 DirectoryScanner scanner = new DirectoryScanner();
89 scanner.setBasedir( basedir );
90 scanner.setIncludes( new String[] { include } );
91 if ( !StringUtils.isEmpty( exclude ) )
92 {
93 scanner.setExcludes( new String[] { exclude, StringUtils.join( FileUtils.getDefaultExcludes(), "," ) } );
94 }
95 else
96 {
97 scanner.setExcludes( FileUtils.getDefaultExcludes() );
98 }
99
100 scanner.scan();
101
102 return scanner.getIncludedFiles();
103 }
104
105 /**
106 * Sorts the specified mojo descriptors by goal name.
107 *
108 * @param mojoDescriptors The mojo descriptors to sort, may be <code>null</code>.
109 * @see MojoDescriptor#getGoal()
110 */
111 public static void sortMojos( List<MojoDescriptor> mojoDescriptors )
112 {
113 if ( mojoDescriptors != null )
114 {
115 Collections.sort( mojoDescriptors, new Comparator<MojoDescriptor>()
116 {
117 /** {@inheritDoc} */
118 @Override
119 public int compare( MojoDescriptor mojo0, MojoDescriptor mojo1 )
120 {
121 return mojo0.getGoal().compareToIgnoreCase( mojo1.getGoal() );
122 }
123 } );
124 }
125 }
126
127 /**
128 * Sorts the specified mojo parameters by name.
129 *
130 * @param parameters The mojo parameters to sort, may be <code>null</code>.
131 * @see Parameter#getName()
132 * @since 2.4.4
133 */
134 public static void sortMojoParameters( List<Parameter> parameters )
135 {
136 if ( parameters != null )
137 {
138 Collections.sort( parameters, new Comparator<Parameter>()
139 {
140 /** {@inheritDoc} */
141 @Override
142 public int compare( Parameter parameter1, Parameter parameter2 )
143 {
144 return parameter1.getName().compareToIgnoreCase( parameter2.getName() );
145 }
146 } );
147 }
148 }
149 }