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.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
41
42
43
44
45 public final class PluginUtils {
46 private PluginUtils() {
47
48 }
49
50
51
52
53
54
55 public static String[] findSources(String basedir, String include) {
56 return PluginUtils.findSources(basedir, include, null);
57 }
58
59
60
61
62
63
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
82
83
84
85
86 public static void sortMojos(List<MojoDescriptor> mojoDescriptors) {
87 if (mojoDescriptors != null) {
88 Collections.sort(mojoDescriptors, new Comparator<MojoDescriptor>() {
89
90 @Override
91 public int compare(MojoDescriptor mojo0, MojoDescriptor mojo1) {
92 return mojo0.getGoal().compareToIgnoreCase(mojo1.getGoal());
93 }
94 });
95 }
96 }
97
98
99
100
101
102
103
104
105 public static void sortMojoParameters(List<Parameter> parameters) {
106 if (parameters != null) {
107 Collections.sort(parameters, new Comparator<Parameter>() {
108
109 @Override
110 public int compare(Parameter parameter1, Parameter parameter2) {
111 return parameter1.getName().compareToIgnoreCase(parameter2.getName());
112 }
113 });
114 }
115 }
116
117
118
119
120
121
122
123
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 }