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