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