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.io.File;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.net.URLClassLoader;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.List;
29  
30  import org.apache.maven.artifact.DependencyResolutionRequiredException;
31  import org.apache.maven.plugin.descriptor.MojoDescriptor;
32  import org.apache.maven.plugin.descriptor.Parameter;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.reporting.MavenReport;
35  import org.codehaus.plexus.util.DirectoryScanner;
36  import org.codehaus.plexus.util.FileUtils;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  /**
40   * Convenience methods to play with Maven plugins.
41   *
42   * @author jdcasey
43   *
44   */
45  public final class PluginUtils {
46      private PluginUtils() {
47          // nop
48      }
49  
50      /**
51       * @param basedir not null
52       * @param include not null
53       * @return list of included files with default SCM excluded files
54       */
55      public static String[] findSources(String basedir, String include) {
56          return PluginUtils.findSources(basedir, include, null);
57      }
58  
59      /**
60       * @param basedir not null
61       * @param include not null
62       * @param exclude could be null
63       * @return list of included files
64       */
65      public static String[] findSources(String basedir, String include, String exclude) {
66          DirectoryScanner scanner = new DirectoryScanner();
67          scanner.setBasedir(basedir);
68          scanner.setIncludes(new String[] {include});
69          if (!(exclude == null || exclude.isEmpty())) {
70              scanner.setExcludes(new String[] {exclude, StringUtils.join(FileUtils.getDefaultExcludes(), ",")});
71          } else {
72              scanner.setExcludes(FileUtils.getDefaultExcludes());
73          }
74  
75          scanner.scan();
76  
77          return scanner.getIncludedFiles();
78      }
79  
80      /**
81       * Sorts the specified mojo descriptors by goal name.
82       *
83       * @param mojoDescriptors The mojo descriptors to sort, may be <code>null</code>.
84       * @see MojoDescriptor#getGoal()
85       */
86      public static void sortMojos(List<MojoDescriptor> mojoDescriptors) {
87          if (mojoDescriptors != null) {
88              Collections.sort(mojoDescriptors, new Comparator<MojoDescriptor>() {
89                  /** {@inheritDoc} */
90                  @Override
91                  public int compare(MojoDescriptor mojo0, MojoDescriptor mojo1) {
92                      return mojo0.getGoal().compareToIgnoreCase(mojo1.getGoal());
93                  }
94              });
95          }
96      }
97  
98      /**
99       * Sorts the specified mojo parameters by name.
100      *
101      * @param parameters The mojo parameters to sort, may be <code>null</code>.
102      * @see Parameter#getName()
103      * @since 2.4.4
104      */
105     public static void sortMojoParameters(List<Parameter> parameters) {
106         if (parameters != null) {
107             Collections.sort(parameters, new Comparator<Parameter>() {
108                 /** {@inheritDoc} */
109                 @Override
110                 public int compare(Parameter parameter1, Parameter parameter2) {
111                     return parameter1.getName().compareToIgnoreCase(parameter2.getName());
112                 }
113             });
114         }
115     }
116 
117     /**
118      * @param mojoClassName a fully qualified Mojo implementation class name, not null
119      * @param project a MavenProject instance, could be null
120      * @return <code>true</code> if the Mojo class implements <code>MavenReport</code>,
121      * <code>false</code> otherwise.
122      * @throws IllegalArgumentException if any
123      * @since 3.10.0
124      */
125     public static boolean isMavenReport(String mojoClassName, MavenProject project) throws IllegalArgumentException {
126         if (mojoClassName == null) {
127             throw new IllegalArgumentException("mojo implementation should be declared");
128         }
129 
130         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
131         if (project != null) {
132             List<String> classPathStrings;
133             try {
134                 classPathStrings = project.getCompileClasspathElements();
135                 if (project.getExecutionProject() != null) {
136                     classPathStrings.addAll(project.getExecutionProject().getCompileClasspathElements());
137                 }
138             } catch (DependencyResolutionRequiredException e) {
139                 throw new IllegalArgumentException(e);
140             }
141 
142             List<URL> urls = new ArrayList<>(classPathStrings.size());
143             for (String classPathString : classPathStrings) {
144                 try {
145                     urls.add(new File(classPathString).toURL());
146                 } catch (MalformedURLException e) {
147                     throw new IllegalArgumentException(e);
148                 }
149             }
150 
151             classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), classLoader);
152         }
153 
154         try {
155             Class<?> clazz = Class.forName(mojoClassName, false, classLoader);
156 
157             return MavenReport.class.isAssignableFrom(clazz);
158         } catch (ClassNotFoundException e) {
159             return false;
160         }
161     }
162 }