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 1405318 2012-11-03 10:57:36Z 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 (injected in fact as parameters by
049 * <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">
050 * maven-core's PluginParameterExpressionEvaluator</a>)
051 * like components ("real" components are injected by Plexus).
052 */
053 public static final Map<String, String> MAVEN_COMPONENTS;
054 static
055 {
056 Map<String, String> mavenComponents = new HashMap<String, String>();
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 MAVEN_COMPONENTS = Collections.unmodifiableMap( mavenComponents );
065 }
066
067 /**
068 * @param basedir not null
069 * @param include not null
070 * @return list of included files with default SCM excluded files
071 */
072 public static String[] findSources( String basedir, String include )
073 {
074 return PluginUtils.findSources( basedir, include, null );
075 }
076
077 /**
078 * @param basedir not null
079 * @param include not null
080 * @param exclude could be null
081 * @return list of included files
082 */
083 public static String[] findSources( String basedir, String include, String exclude )
084 {
085 DirectoryScanner scanner = new DirectoryScanner();
086 scanner.setBasedir( basedir );
087 scanner.setIncludes( new String[] { include } );
088 if ( !StringUtils.isEmpty( exclude ) )
089 {
090 scanner.setExcludes( new String[] { exclude, StringUtils.join( FileUtils.getDefaultExcludes(), "," ) } );
091 }
092 else
093 {
094 scanner.setExcludes( FileUtils.getDefaultExcludes() );
095 }
096
097 scanner.scan();
098
099 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 }