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