1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.alchim31.maven.yuicompressor;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.util.List;
26
27 import javax.inject.Inject;
28
29 import org.apache.maven.model.Resource;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.project.MavenProject;
35 import org.codehaus.plexus.build.BuildContext;
36 import org.codehaus.plexus.util.DirectoryScanner;
37 import org.codehaus.plexus.util.Scanner;
38
39
40
41
42 public abstract class MojoSupport extends AbstractMojo {
43
44
45 private static final String[] EMPTY_STRING_ARRAY = {};
46
47
48
49
50 @Parameter(defaultValue = "${project.basedir}/src/main/js")
51 private File sourceDirectory;
52
53
54
55
56 @Parameter(defaultValue = "${project.basedir}/src/main/webapp")
57 private File warSourceDirectory;
58
59
60
61
62 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}")
63 private File webappDirectory;
64
65
66
67
68 @Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
69 private File outputDirectory;
70
71
72
73
74 @Parameter(defaultValue = "${project.resources}", required = true, readonly = true)
75 private List<Resource> resources;
76
77
78 @Parameter
79 private List<String> excludes;
80
81
82 @Parameter(defaultValue = "false")
83 private boolean useProcessedResources;
84
85
86 @Parameter
87 private List<String> includes;
88
89
90 @Parameter
91 private boolean excludeWarSourceDirectory;
92
93
94
95
96 @Parameter(defaultValue = "false")
97 private boolean excludeResources;
98
99
100 @Parameter(defaultValue = "${project}", readonly = true, required = true)
101 protected MavenProject project;
102
103
104 @Parameter(defaultValue = "true", property = "maven.yuicompressor.jswarn")
105 protected boolean jswarn;
106
107
108
109
110 @Parameter(defaultValue = "false", property = "maven.yuicompressor.skip")
111 private boolean skip;
112
113
114
115
116 @Parameter(defaultValue = "false", property = "maven.yuicompressor.failOnWarning")
117 protected boolean failOnWarning;
118
119
120
121
122 @Inject
123 protected BuildContext buildContext;
124
125
126 protected ErrorReporter4Mojo jsErrorReporter;
127
128 @Override
129 public void execute() throws MojoExecutionException, MojoFailureException {
130 if (skip) {
131 getLog().debug("run of yuicompressor-maven-plugin skipped");
132 return;
133 }
134
135 if (failOnWarning) {
136 jswarn = true;
137 }
138
139 jsErrorReporter = new ErrorReporter4Mojo(getLog(), jswarn, buildContext);
140
141 try {
142 beforeProcess();
143 processDir(sourceDirectory, outputDirectory, null, useProcessedResources);
144 if (!excludeResources) {
145 for (Resource resource : resources) {
146 File destRoot = outputDirectory;
147 if (resource.getTargetPath() != null) {
148 destRoot = outputDirectory.toPath().resolve(resource.getTargetPath()).toFile();
149 }
150 processDir(Path.of(resource.getDirectory()).toFile(), destRoot, resource.getExcludes(),
151 useProcessedResources);
152 }
153 }
154 if (!excludeWarSourceDirectory) {
155 processDir(warSourceDirectory, webappDirectory, null, useProcessedResources);
156 }
157 afterProcess();
158 } catch (Exception e) {
159 throw new MojoExecutionException("wrap: " + e.getMessage(), e);
160 }
161
162 getLog().info(String.format("nb warnings: %d, nb errors: %d", jsErrorReporter.getWarningCnt(),
163 jsErrorReporter.getErrorCnt()));
164 if (failOnWarning && jsErrorReporter.getWarningCnt() > 0) {
165 throw new MojoFailureException("warnings on " + this.getClass().getSimpleName() + "=> failure ! (see log)");
166 }
167 }
168
169
170
171
172
173
174 protected abstract String[] getDefaultIncludes();
175
176
177
178
179
180
181
182 protected abstract void beforeProcess() throws IOException;
183
184
185
186
187
188
189
190 protected abstract void afterProcess() throws IOException;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 private void processDir(File srcRoot, File destRoot, List<String> srcExcludes, boolean destAsSource)
213 throws IOException, MojoFailureException, MojoExecutionException {
214 if (srcRoot == null) {
215 return;
216 }
217 if (!srcRoot.exists()) {
218 buildContext.addMessage(srcRoot, 0, 0, "Directory " + srcRoot.getPath() + " does not exist",
219 BuildContext.SEVERITY_WARNING, null);
220 getLog().info("Directory " + srcRoot.getPath() + " does not exist");
221 return;
222 }
223 if (destRoot == null) {
224 throw new MojoFailureException("destination directory for " + srcRoot + " is null");
225 }
226 Scanner scanner;
227 if (!buildContext.isIncremental()) {
228 DirectoryScanner dScanner = new DirectoryScanner();
229 dScanner.setBasedir(srcRoot);
230 scanner = dScanner;
231 } else {
232 scanner = buildContext.newScanner(srcRoot);
233 }
234
235 if (includes == null) {
236 scanner.setIncludes(getDefaultIncludes());
237 } else {
238 scanner.setIncludes(includes.toArray(new String[0]));
239 }
240
241 if (srcExcludes != null && !srcExcludes.isEmpty()) {
242 scanner.setExcludes(srcExcludes.toArray(EMPTY_STRING_ARRAY));
243 }
244 if (excludes != null && !excludes.isEmpty()) {
245 scanner.setExcludes(excludes.toArray(EMPTY_STRING_ARRAY));
246 }
247 scanner.addDefaultExcludes();
248
249 scanner.scan();
250
251 String[] includedFiles = scanner.getIncludedFiles();
252 if (includedFiles == null || includedFiles.length == 0) {
253 if (buildContext.isIncremental()) {
254 getLog().info("No files have changed, so skipping the processing");
255 } else {
256 getLog().info("No files to be processed");
257 }
258 return;
259 }
260 for (String name : includedFiles) {
261 SourceFile src = new SourceFile(srcRoot, destRoot, name, destAsSource);
262 jsErrorReporter.setDefaultFileName("..."
263 + src.toFile().getCanonicalPath().substring(src.toFile().getCanonicalPath().lastIndexOf('/') + 1));
264 jsErrorReporter.setFile(src.toFile());
265 processFile(src);
266 }
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280 protected abstract void processFile(SourceFile src) throws IOException, MojoExecutionException;
281 }