Skip to content
Ron Bieber edited this page Sep 2, 2015 · 26 revisions

This is a version of an HTML Compressor I found on Google code that has been modified to work with Java Server Pages.

This version also includes an ANT task that can be used during an automated build process to optimize JSP files to remove the incessant “blank lines” that you get from JSP tag processing.

Removing these extra lines in these files reduces the number of bytes that needs to be compressed and downloaded to the client.

The original version of HTMLCompressor was written by Sergiy Kovalchuk and can be found on Google code. Special thanks to Sergiy for releasing this under the Apache license and giving me a foundation to solve my problem without re-inventing the wheel.

View the current Change Log.

Ant Task Usage

A sample of using the Ant task can be found in the build.xml file:

 <target name="test" depends="build.jar">
       <path id="compressor-classpath">
            <pathelement location="${lib.dir}/yuicompressor-2.4.3.jar"/>
            <pathelement location="${build.dir}/jspcompressor-0.1.jar"/>
        </path>    
        <taskdef name="jspcompressor" classname="com.googlecode.jspcompressor.ant.task.CompressJspTask" 
        classpathref="compressor-classpath"/>
        <jspcompressor destdir="./tmp" compressjs="true" removecomments="true" removejspcomments="true" debug="false" 
        skipstrutsformcomments="true">
            <fileset dir="testsrc" includes="**/*.jsp"/>
        </jspcompressor>
    </target> 

The lib directory for this package actually includes all of the jar files necessary to build and run. These should be added to the classpath as part of your task. The ${build.dir} directory contains the final build product of the ant task and compressor and is added as well.

The task is used to let Ant know that you have a new task to add, where it is (classpathref) and the class name that implements the task.

The code for the task can be found in the src/com/googlecode/jspcompressor/ant/task directory. Its nothing fancy, and only exposes a few of the properties at this point for the jspcompressor. More will come over time – but at this point the ant task chooses “intelligent” defaults for the unexposed ones.

Currently the attributes exposed for the task are:

Attribute Value Description
enabled true or false Whether the task actually does anything or not. If its false, its basically a no-op. If its true, compression will be performed
destdir file path Path to deposit newly processed files
compressjs true or false If true, turns on YUI compression for inline Javascript
removecomments true or false Tells the compressor to remove html comments
removejspcomments true or false Tells the compressor to remove JSP comments (<%— —%> )
debug true or false Display debug messages (mostly used for JS Compression debugging right now)
failonerror true or false Throws an exception to stop the build if Javascript compression fails, with failing code as the message
skipstrutsformcomments true or false Tells the compressor to leave JSP comments (<%— —%>) that contain references to the <html:form> struts element

The last element is apparently a work around that some code uses to initialize struts <html:form> elements without actually using them that was used in Struts 1.0 applications. For some applications, removal of these comments cause forms not to initialize properly.

Finally, the task takes one or more filesets that specify the files to work on. These are your standard fileset constructs. See the ant documentation for details on their use.

A Note on Javascript Compression

Some java applications render javascript based on the state of the Java application serving the page and therefore have JSP (like <logic:present>) custom tags included in them. These cause the YUI compressor to fail due to invalid syntax in Javascript. Remember, YUI actually parses the javascript – so it has to be syntactically correct. I’ve tried to handle most of these conditions – but some conditions not present in the current codebase I am working with may fail. HTML parsing is hard with regular expressions.

Bottom line is, if you have syntactically incorrect Javascript, the YUICompressor parser will fail and the failing code will be used uncompressed.

If you are receiving “string index out of bounds” errors while trying to compress javascript, ensure that you do not have a version of Rhino in your CLASSPATH. While the YUI compressor docs say that it can run side by side with other versions of Rhino, in my experience this isn’t the case. YUI compressor includes its own version of Rhino within the jar file and these conflict.

Once I removed Rhino from my CLASSPATH, my errors went away.

Building JavaDoc
I have removed the JavaDoc documentation from the repository. To build it, execute the following command:

ant build.javadoc

This will build and deposit the JavaDoc documentation in the doc/ directory.

You may get warnings about certain Ant related classes not being available. Set the ant.dir property in the build.properties file to the location of ant.jar to fix these warnings

Submitting Issues
If you find any issues while using this application feel free to submit them on the issues tracker.

Final Note
The code for this has dramatically changed from the original. To minimize confusion, the project and its related classes will now be called jspcompressor rather than HTMLCompressor. While the name has changed and the functionality has been enhanced, it should still work on plain ol’ html files as well as ASP and JSP files.

Clone this wiki locally