Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Minified JavaScript/CSS Builds

DZone's Guide to

Minified JavaScript/CSS Builds

·
Free Resource

How to include minified JavaScript and CSS files in web application builds In NetBeans IDE? Here's a solution.

I have adapted the work done by Philippe Mouawad and Andrew Gomilko using the YUI Compression tool as Ant Task so that it can be used with NetBeans 6.1 web application projects.

To get automated JavaScript and CSS files into your WAR files during the NetBeans build process do the following:

 

These two files need to go in your NetBeans ant\lib folder, for my Windows/NetBeans 6.1 installation that is:

C:\Program Files\NetBeans 6.1\java2\ant\lib

For web applications that you want to build with the compressed js/css files, add the following XML to the build.xml for the project, overriding the "-pre-dist" Ant target, which is not currently configured by default in NetBeans.

<target name="-pre-dist">    
<echo level="info" message="Compressing JavaScript and CSS files...." />
<path id="yuicompressor.classpath">
<fileset dir="${ant.home}/lib">
<include name="yuicompressor-2.3.5.jar"/>
</fileset>
</path>
<taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
<classpath>
<path refid="yuicompressor.classpath"/>
</classpath>
</taskdef>
<yuicompress linebreak="8000" warn="false" munge="no" preserveallsemicolons="true"
outputfolder="${basedir}/${build.web.dir}" >
<fileset dir="${basedir}/web" excludes="/ext-2.1/**/*.js" >
<include name="**/*.js" />
<include name="**/*.css" />
</fileset>
</yuicompress>
<echo level="info" message="Compression Complete" />
</target>

 


This will compress and copy the JavaScript and CSS files to the build directory before the WAR file is built and will thus be included in your WAR file.

Note there is a line in my example with an "exclude" for my ext-2.1 directory, the files there are already compressed so I have excluded them, just add/remove to suit your application.

All we need now is for someone to code a NetBeans plugin that configures this all for us and makes it configurable from a wizard maybe, any takers?

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}