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