1 package net_alchim31_maven_yuicompressor;
2
3 import java.io.File;
4 import java.util.List;
5
6 import org.apache.maven.model.Resource;
7 import org.apache.maven.plugin.AbstractMojo;
8 import org.apache.maven.plugin.MojoExecutionException;
9 import org.apache.maven.plugin.MojoFailureException;
10 import org.apache.maven.project.MavenProject;
11 import org.codehaus.plexus.util.DirectoryScanner;
12
13
14
15
16
17
18
19
20 public abstract class MojoSupport extends AbstractMojo {
21 private static final String[] EMPTY_STRING_ARRAY = {};
22
23
24
25
26
27
28
29 private File sourceDirectory;
30
31
32
33
34
35
36 private File warSourceDirectory;
37
38
39
40
41
42
43 private File webappDirectory;
44
45
46
47
48
49
50 private File outputDirectory;
51
52
53
54
55
56
57 private List<Resource> resources;
58
59
60
61
62
63
64 private List<String> excludes;
65
66
67
68
69
70
71 private List<String> includes;
72
73
74
75
76
77
78 private boolean excludeWarSourceDirectory = false;
79
80
81
82
83
84
85 private boolean excludeResources = false;
86
87
88
89
90
91
92 protected MavenProject project;
93
94
95
96
97
98
99 protected boolean jswarn;
100
101
102
103
104
105
106 private boolean skip;
107
108
109
110
111
112
113 protected boolean failOnWarning;
114 protected ErrorReporter4Mojo jsErrorReporter_;
115
116 @SuppressWarnings("unchecked")
117 public void execute() throws MojoExecutionException, MojoFailureException {
118 try {
119 if (skip) {
120 getLog().debug("run of yuicompressor-maven-plugin skipped");
121 return;
122 }
123 if (failOnWarning) {
124 jswarn = true;
125 }
126 jsErrorReporter_ = new ErrorReporter4Mojo(getLog(), jswarn);
127 beforeProcess();
128 processDir(sourceDirectory, outputDirectory, null, true);
129 if (!excludeResources) {
130 for (Resource resource : resources){
131 File destRoot = outputDirectory;
132 if (resource.getTargetPath() != null) {
133 destRoot = new File(outputDirectory, resource.getTargetPath());
134 }
135 processDir(new File( resource.getDirectory() ), destRoot, resource.getExcludes(), true);
136 }
137 }
138 if (!excludeWarSourceDirectory) {
139 processDir(warSourceDirectory, webappDirectory, null, false);
140 }
141 afterProcess();
142 getLog().info(String.format("nb warnings: %d, nb errors: %d", jsErrorReporter_.getWarningCnt(), jsErrorReporter_.getErrorCnt()));
143 if (failOnWarning && (jsErrorReporter_.getWarningCnt() > 0)) {
144 throw new MojoFailureException("warnings on "+ this.getClass().getSimpleName() + "=> failure ! (see log)");
145 }
146 } catch (RuntimeException exc) {
147 throw exc;
148 } catch (MojoFailureException exc) {
149 throw exc;
150 } catch (MojoExecutionException exc) {
151 throw exc;
152 } catch (Exception exc) {
153 throw new MojoExecutionException("wrap: " + exc.getMessage(), exc);
154 }
155 }
156
157 protected abstract String[] getDefaultIncludes() throws Exception;
158 protected abstract void beforeProcess() throws Exception;
159 protected abstract void afterProcess() throws Exception;
160
161
162
163
164
165 protected void processDir(File srcRoot, File destRoot, List<String> srcExcludes, boolean destAsSource) throws Exception {
166 if ((srcRoot == null) || ( !srcRoot.exists() )) {
167 return;
168 }
169 if (destRoot == null) {
170 throw new MojoFailureException("destination directory for " + srcRoot + " is null");
171 }
172 DirectoryScanner scanner = new DirectoryScanner();
173 scanner.setBasedir(srcRoot);
174
175 if (includes == null) {
176 scanner.setIncludes(getDefaultIncludes());
177 } else {
178 scanner.setIncludes(includes.toArray(new String[0]));
179 }
180
181 if ( (srcExcludes != null) && !srcExcludes.isEmpty() ) {
182 scanner.setExcludes( srcExcludes.toArray( EMPTY_STRING_ARRAY ) );
183 }
184 if ((excludes != null) && !excludes.isEmpty()) {
185 scanner.setExcludes( excludes.toArray( EMPTY_STRING_ARRAY ) );
186 }
187 scanner.addDefaultExcludes();
188 scanner.scan();
189 for(String name :scanner.getIncludedFiles() ) {
190 SourceFile src = new SourceFile(srcRoot, destRoot, name, destAsSource);
191 jsErrorReporter_.setDefaultFileName("..." + src.toFile().getAbsolutePath().substring(project.getBasedir().getAbsolutePath().length()));
192 processFile(src);
193 }
194 }
195
196 protected abstract void processFile(SourceFile src) throws Exception;
197 }