1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41
42
43
44 public final class PluginUtils {
45 private PluginUtils() {
46
47 }
48
49
50
51
52
53
54 public static String[] findSources(String basedir, String include) {
55 return PluginUtils.findSources(basedir, include, null);
56 }
57
58
59
60
61
62
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
81
82
83
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
93
94
95
96
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
106
107
108
109
110
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 }