View Javadoc
1   /*
2    *    Copyright 2011-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * Compress XML files.
28   */
29  @Mojo(name = "xml", defaultPhase = LifecyclePhase.COMPILE, requiresProject = false, threadSafe = true)
30  public class XmlCompressorMojo extends AbstractMojo {
31  
32      /**
33       * File extensions to be processed.
34       *
35       * @deprecated use fileExtensions
36       */
37      @Deprecated
38      @Parameter(property = "htmlcompressor.fileExt")
39      private String[] fileExt;
40  
41      /** file extensions to be processed. */
42      @Parameter(property = "htmlcompressor.fileExtensions")
43      private String[] fileExtensions;
44  
45      /** if false all compression is off (default is true). */
46      @Parameter(property = "htmlcompressor.enabled", defaultValue = "true")
47      private boolean enabled = true;
48  
49      /** Skip run of plugin. */
50      @Parameter(defaultValue = "false", alias = "skip", property = "skip")
51      private boolean skip;
52  
53      /** if false keeps XML comments (default is true). */
54      @Parameter(property = "htmlcompressor.removeComments", defaultValue = "true")
55      private boolean removeComments = true;
56  
57      /** removes iter-tag whitespace characters (default is true). */
58      @Parameter(property = "htmlcompressor.removeIntertagSpaces", defaultValue = "true")
59      private boolean removeIntertagSpaces = true;
60  
61      /**
62       * source folder where xml files are located.
63       */
64      @Parameter(property = "htmlcompressor.srcFolder", defaultValue = "${project.basedir}/src/main/resources")
65      private String srcFolder = "src/main/resources";
66  
67      /**
68       * target folder where compressed xml files will be placed.
69       */
70      @Parameter(property = "htmlcompressor.targetFolder", defaultValue = "${project.build.directory}/classes")
71      private String targetFolder = "target/classes";
72  
73      /** Charset encoding for files to read and create. */
74      @Parameter(property = "htmlcompressor.encoding", defaultValue = "UTF-8")
75      private String encoding = "UTF-8";
76  
77      @Override
78      public void execute() throws MojoExecutionException {
79          // Check if plugin run should be skipped
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          // Deprecated
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      * Gets the file ext.
117      *
118      * @return the file ext
119      *
120      * @deprecated use getFileExtensions
121      */
122     @Deprecated
123     public String[] getFileExt() {
124         return fileExt;
125     }
126 
127     /**
128      * Sets the file ext.
129      *
130      * @param fileExt
131      *            the new file ext
132      *
133      * @deprecated use setFileExtensions
134      */
135     @Deprecated
136     public void setFileExt(String[] fileExt) {
137         this.fileExt = fileExt;
138     }
139 
140     /**
141      * Gets the file extensions.
142      *
143      * @return the file extensions
144      */
145     public String[] getFileExtensions() {
146         return fileExtensions;
147     }
148 
149     /**
150      * Sets the file ext.
151      *
152      * @param fileExtensions
153      *            the new file extensions
154      */
155     public void setFileExtensions(String[] fileExtensions) {
156         this.fileExtensions = fileExtensions;
157     }
158 
159     /**
160      * Gets the enabled.
161      *
162      * @return the enabled
163      */
164     public Boolean getEnabled() {
165         return enabled;
166     }
167 
168     /**
169      * Sets the enabled.
170      *
171      * @param enabled
172      *            the new enabled
173      */
174     public void setEnabled(Boolean enabled) {
175         this.enabled = enabled;
176     }
177 
178     /**
179      * Gets the removes the comments.
180      *
181      * @return the removes the comments
182      */
183     public Boolean getRemoveComments() {
184         return removeComments;
185     }
186 
187     /**
188      * Sets the removes the comments.
189      *
190      * @param removeComments
191      *            the new removes the comments
192      */
193     public void setRemoveComments(Boolean removeComments) {
194         this.removeComments = removeComments;
195     }
196 
197     /**
198      * Gets the removes the intertag spaces.
199      *
200      * @return the removes the intertag spaces
201      */
202     public Boolean getRemoveIntertagSpaces() {
203         return removeIntertagSpaces;
204     }
205 
206     /**
207      * Sets the removes the intertag spaces.
208      *
209      * @param removeIntertagSpaces
210      *            the new removes the intertag spaces
211      */
212     public void setRemoveIntertagSpaces(Boolean removeIntertagSpaces) {
213         this.removeIntertagSpaces = removeIntertagSpaces;
214     }
215 
216     /**
217      * Gets the src folder.
218      *
219      * @return the src folder
220      */
221     public String getSrcFolder() {
222         return srcFolder;
223     }
224 
225     /**
226      * Sets the src folder.
227      *
228      * @param srcFolder
229      *            the new src folder
230      */
231     public void setSrcFolder(String srcFolder) {
232         this.srcFolder = srcFolder;
233     }
234 
235     /**
236      * Gets the target folder.
237      *
238      * @return the target folder
239      */
240     public String getTargetFolder() {
241         return targetFolder;
242     }
243 
244     /**
245      * Sets the target folder.
246      *
247      * @param targetFolder
248      *            the new target folder
249      */
250     public void setTargetFolder(String targetFolder) {
251         this.targetFolder = targetFolder;
252     }
253 }