1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.tunyk.mvn.plugins.htmlcompressor;
17
18 import java.nio.charset.Charset;
19
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugins.annotations.LifecyclePhase;
23 import org.apache.maven.plugins.annotations.Mojo;
24 import org.apache.maven.plugins.annotations.Parameter;
25
26
27
28
29 @Mojo(name = "xml", defaultPhase = LifecyclePhase.COMPILE, requiresProject = false, threadSafe = true)
30 public class XmlCompressorMojo extends AbstractMojo {
31
32
33
34
35
36
37 @Deprecated
38 @Parameter(property = "htmlcompressor.fileExt")
39 private String[] fileExt;
40
41
42 @Parameter(property = "htmlcompressor.fileExtensions")
43 private String[] fileExtensions;
44
45
46 @Parameter(property = "htmlcompressor.enabled", defaultValue = "true")
47 private boolean enabled = true;
48
49
50 @Parameter(defaultValue = "false", alias = "skip", property = "skip")
51 private boolean skip;
52
53
54 @Parameter(property = "htmlcompressor.removeComments", defaultValue = "true")
55 private boolean removeComments = true;
56
57
58 @Parameter(property = "htmlcompressor.removeIntertagSpaces", defaultValue = "true")
59 private boolean removeIntertagSpaces = true;
60
61
62
63
64 @Parameter(property = "htmlcompressor.srcFolder", defaultValue = "${project.basedir}/src/main/resources")
65 private String srcFolder = "src/main/resources";
66
67
68
69
70 @Parameter(property = "htmlcompressor.targetFolder", defaultValue = "${project.build.directory}/classes")
71 private String targetFolder = "target/classes";
72
73
74 @Parameter(property = "htmlcompressor.encoding", defaultValue = "UTF-8")
75 private String encoding = "UTF-8";
76
77 @Override
78 public void execute() throws MojoExecutionException {
79
80 if (this.skip) {
81 getLog().info("XMLCompressor is skipped");
82 return;
83 }
84
85 if (!enabled) {
86 getLog().info("XML compression was turned off.");
87 return;
88 }
89
90
91 if (fileExt != null && fileExtensions == null) {
92 fileExtensions = fileExt;
93 }
94
95 getLog().info("Compressing " + srcFolder);
96 XmlCompressor xmlCompressor = new XmlCompressor(srcFolder, targetFolder);
97 xmlCompressor.setFileExtensions(fileExtensions);
98 xmlCompressor.setFileEncoding(Charset.forName(encoding));
99
100 com.googlecode.htmlcompressor.compressor.XmlCompressor xmlCompressorHandler = new com.googlecode.htmlcompressor.compressor.XmlCompressor();
101 xmlCompressorHandler.setEnabled(enabled);
102 xmlCompressorHandler.setRemoveComments(removeComments);
103 xmlCompressorHandler.setRemoveIntertagSpaces(removeIntertagSpaces);
104 xmlCompressor.setXmlCompressor(xmlCompressorHandler);
105
106 try {
107 xmlCompressor.compress();
108 } catch (Exception e) {
109 throw new MojoExecutionException(e.getMessage());
110 }
111
112 getLog().info("XML compression completed.");
113 }
114
115
116
117
118
119
120
121
122 @Deprecated
123 public String[] getFileExt() {
124 return fileExt;
125 }
126
127
128
129
130
131
132
133
134
135 @Deprecated
136 public void setFileExt(String[] fileExt) {
137 this.fileExt = fileExt;
138 }
139
140
141
142
143
144
145 public String[] getFileExtensions() {
146 return fileExtensions;
147 }
148
149
150
151
152
153
154
155 public void setFileExtensions(String[] fileExtensions) {
156 this.fileExtensions = fileExtensions;
157 }
158
159
160
161
162
163
164 public Boolean getEnabled() {
165 return enabled;
166 }
167
168
169
170
171
172
173
174 public void setEnabled(Boolean enabled) {
175 this.enabled = enabled;
176 }
177
178
179
180
181
182
183 public Boolean getRemoveComments() {
184 return removeComments;
185 }
186
187
188
189
190
191
192
193 public void setRemoveComments(Boolean removeComments) {
194 this.removeComments = removeComments;
195 }
196
197
198
199
200
201
202 public Boolean getRemoveIntertagSpaces() {
203 return removeIntertagSpaces;
204 }
205
206
207
208
209
210
211
212 public void setRemoveIntertagSpaces(Boolean removeIntertagSpaces) {
213 this.removeIntertagSpaces = removeIntertagSpaces;
214 }
215
216
217
218
219
220
221 public String getSrcFolder() {
222 return srcFolder;
223 }
224
225
226
227
228
229
230
231 public void setSrcFolder(String srcFolder) {
232 this.srcFolder = srcFolder;
233 }
234
235
236
237
238
239
240 public String getTargetFolder() {
241 return targetFolder;
242 }
243
244
245
246
247
248
249
250 public void setTargetFolder(String targetFolder) {
251 this.targetFolder = targetFolder;
252 }
253 }