I’ve spent some time recently writing ant scripts to generate documentation, combine and minimise multiple javascript files into a single download. I thought I’d share what I have, in case others find it useful or can suggest better ways of doing what I’m trying to accomplish.
Combining multiple JS files into a single file
Here’s a simple ant task that concatenates several files into a single file. The version.txt is a file that simple contains a version number in it i.e. ‘0.5’.
-
<target name="combine">
-
<echo message="Concatenating Files" />
-
<concat destfile="./dist/uncompressed/mydistribution-${VERSION}.js">
-
<fileset dir="." includes="file1.js" />
-
<fileset dir="." includes="file2.js" />
-
<fileset dir="." includes="file3.js" />
-
<fileset dir="." includes="file4.js" />
-
<fileset dir="." includes="file5.js" />
-
</concat>
-
</target>
Minimising using YUI Compressor
You’ll need to download the latest version of the YUI Compressor. All I’ve provided is a simple ant wrapper around it, and example of how to use it:
-
<property name="LIB_DIR" value="./lib"/>
-
<property name="YUI" value="${LIB_DIR}/yui-compressor/yuicompressor-2.4.2.jar" />
-
<target name="minimiseJSFile">
-
<java jar="${YUI}" fork="true" failonerror="true">
-
<arg line="–type js" />
-
<arg line="-o ${outputFile}" />
-
<arg value="${inputFile}" />
-
</java>
-
</target>
-
<!– using the above –>
-
<target name="minimise">
-
<antcall target="minimiseJSFile">
-
<param name="inputFile" value="./dist/uncompressed/mydistribution-${VERSION}.js" />
-
<param name="outputFile" value="./dist/minimised/mydistribution.min-${VERSION}.js" />
-
</antcall>
-
</target>
-
It’s worth noting that by default the YUI Compressor both minimises and obfuscates code, this is because the process of obfuscation also significantly reduces the size of the script since it substitutes your nice variable names with single letter variables. If you do not want this behaviour then you can add the ‘–nomunge’ directive as an arg line above .
Generating JS Documention
For this to work you’ll need to download the latest version of JSDOC Toolkit. In the example below im enumerating each file I want documentation generated for, you could just as easily point it at a directory.
-
<target name="doc" description="generates documentation for core rdfQuery">
-
<!– jsdoc-toolkit ant taks is currently broken, so we directly run –>
-
<echo message="Generating Documentation:"/>
-
<java jar="${JSDOC_TOOLKIT_DIR}/jsrun.jar" fork="true" failonerror="true">
-
<arg value="${JSDOC_TOOLKIT_DIR}/app/run.js"/>
-
<arg value="-t=${JSDOC_TOOLKIT_DIR}/templates/jsdoc"/>
-
<arg value="-d=./dist/documentation/"/>
-
<arg value="file1.js"/>
-
<arg value="file2.js"/>
-
<arg value="file3.js"/>
-
</java>
-
</target>
-
Packaging a distribution
Here we want to simply create a single, easily downloadable zip file which contains the combined javascripts, a minimised version of this, and all the api documentation.
-
-
<target name="dist">
-
<zip destfile="./dist/mydistribution-${VERSION}.zip">
-
<zipfileset dir="./dist/uncompressed/" includes="*.js" prefix="./dist/uncompressed/"/>
-
<zipfileset dir="./dist/minimised/" includes="*.js" prefix="./dist/minimised/"/>
-
<zipfileset dir="./dist/documentation/" includes="**/**" prefix="./dist/documentation/"/>
-
</zip>
-
</target>
-
Putting it altogether
Here’s a real example of how you can combine the above together. I’ve copied the build.xml that I added to the rdfQuery project below.:
-
<?xml version="1.0"?>
-
<project name="rdfquery" basedir="." default="all">
-
<loadfile property="VERSION" srcfile="version.txt" description="Version to build" >
-
<filterchain>
-
<striplinebreaks/>
-
</filterchain>
-
</loadfile>
-
-
<property name="DOCS_DIR" value="./docs" description="API documentation"/>
-
<property name="DIST_DIR" value="./dist"/>
-
<property name="LIB_DIR" value="./lib"/>
-
<property name="JSDOC_TOOLKIT_DIR" value="${LIB_DIR}/jsdoc-toolkit/"/>
-
<property name="YUI" value="${LIB_DIR}/yui-compressor/yuicompressor-2.4.2.jar" />
-
<!– Names for output –>
-
<property name="JS" value="${DIST_DIR}/js/" />
-
<property name="JS_MIN" value="${DIST_DIR}/minimised/" />
-
-
<target name="all" depends="init, doc, dist"/>
-
-
<target name="doc" description="generates documentation for core rdfQuery">
-
<!– jsdoc-toolkit ant taks is currently broken, so we directly run –>
-
<echo message="Generating Documentation:"/>
-
<java jar="${JSDOC_TOOLKIT_DIR}/jsrun.jar" fork="true" failonerror="true">
-
<arg value="${JSDOC_TOOLKIT_DIR}/app/run.js"/>
-
<arg value="-t=${JSDOC_TOOLKIT_DIR}/templates/jsdoc"/>
-
<arg value="-d=${DOCS_DIR}"/>
-
<arg value="jquery.uri.js"/>
-
<arg value="jquery.xmlns.js"/>
-
<arg value="jquery.datatype.js"/>
-
<arg value="jquery.curie.js"/>
-
<arg value="jquery.rdf.js"/>
-
<arg value="jquery.rdfa.js"/>
-
<arg value="jquery.rules.js"/>
-
</java>
-
</target>
-
-
<target name="dist">
-
<antcall target="combine" />
-
<antcall target="minimise" />
-
<zip destfile="${DIST_DIR}/jquery.rdfquery-${VERSION}.zip">
-
<zipfileset dir="${JS}" includes="*.js" prefix="${JS}"/>
-
<zipfileset dir="${JS_MIN}" includes="*.js" prefix="${JS_MIN}"/>
-
<zipfileset dir="${DOCS_DIR}" includes="**/**" prefix="${DOCS_DIR}"/>
-
</zip>
-
</target>
-
-
<target name="combine" description="combines js files into three different files representing the three different packages for distribution">
-
<echo message="Building rdfQuery Core Distribution" />
-
<concat destfile="${JS}/jquery.rdfquery.core-${VERSION}.js">
-
<fileset dir="." includes="jquery.uri.js" />
-
<fileset dir="." includes="jquery.xmlns.js" />
-
<fileset dir="." includes="jquery.datatype.js" />
-
<fileset dir="." includes="jquery.curie.js" />
-
<fileset dir="." includes="jquery.rdf.js" />
-
</concat>
-
-
<echo message="Building rdfQuery RDFa Distribution" />
-
<concat destfile="${JS}/jquery.rdfquery.rdfa-${VERSION}.js">
-
<fileset dir="${JS}/" includes="jquery.rdfquery.core-${VERSION}.js" />
-
<fileset dir="." includes="jquery.rdfa.js" />
-
</concat>
-
-
<echo message="Building rdfQuery Rules Distribution" />
-
<concat destfile="${JS}/jquery.rdfquery.rules-${VERSION}.js">
-
<fileset dir="${JS}/" includes="jquery.rdfquery.rdfa-${VERSION}.js" />
-
<fileset dir="." includes="jquery.rules.js" />
-
</concat>
-
</target>
-
-
<target name="minimise">
-
<echo message="Minimising rdfQuery Core Distribution" />
-
<echo message="Minimising rdfQuery RDFa Distribution" />
-
<echo message="Minimising rdfQuery Rules Distribution" />
-
-
<antcall target="minimiseJSFile">
-
<param name="inputFile" value="${JS}/jquery.rdfquery.core-${VERSION}.js" />
-
<param name="outputFile" value="${JS_MIN}/jquery.rdfquery.core.min-${VERSION}.js" />
-
</antcall>
-
<antcall target="minimiseJSFile">
-
<param name="inputFile" value="${JS}/jquery.rdfquery.rdfa-${VERSION}.js" />
-
<param name="outputFile" value="${JS_MIN}/jquery.rdfquery.rdfa.min-${VERSION}.js" />
-
</antcall>
-
<antcall target="minimiseJSFile">
-
<param name="inputFile" value="${JS}/jquery.rdfquery.rules-${VERSION}.js" />
-
<param name="outputFile" value="${JS_MIN}/jquery.rdfquery.rules.min-${VERSION}.js" />
-
</antcall>
-
</target>
-
-
<target name="minimiseJSFile">
-
<java jar="${YUI}" fork="true" failonerror="true">
-
<arg line="–type js" />
-
<arg line="-o ${outputFile}" />
-
<arg value="${inputFile}" />
-
</java>
-
</target>
-
-
<target name="clean" description="">
-
<echo message="Deleting distribution and API documentation"/>
-
<delete dir="${DIST_DIR}"/>
-
<delete dir="${DOCS_DIR}"/>
-
</target>
-
-
<target name="init" depends="clean">
-
<mkdir dir="${DIST_DIR}" />
-
<mkdir dir="${DIST_DIR}/js" />
-
<mkdir dir="${DIST_DIR}/minimised" />
-
<mkdir dir="${DOCS_DIR}" />
-
</target>
-
</project>
Summary
I hope others find this useful. There are a number of obivious improvements that can be made but I hope it serves to illustrate the general principles. Let me know you all think