Easy Javascript / CSS Compression with phing
Posted on 04/05/2008 at 12:08 PM
Hi Just thought I would blog a new phing task I just created that minifies css or javascript using YUI Compressor
This would be placed in build/extended/tasks/kpMinTask.php, also place the yuicompressor in build/tools/yuicompressor.jar
- <?php
- /**
- * Uses the Phing Task
- */
- require_once 'phing/Task.php';
- /**
- * Task to compress files using YUI Compressor.
- *
- * @author Keith Pope
- */
- class kpMinTask extends Task
- {
- /**
- * path to YuiCompressor
- *
- * @var string
- */
- protected $yuiPath;
- /**
- * the source files
- *
- * @var FileSet
- */
- /**
- * Whether the build should fail, if
- * errors occured
- *
- * @var boolean
- */
- protected $failonerror = false;
- /**
- * directory to put minified javascript files into
- *
- * @var string
- */
- protected $targetDir;
- /**
- * sets the path where JSmin can be found
- *
- * @param string $yuiPath
- */
- public function setYuiPath( $yuiPath )
- {
- $this->yuiPath = $yuiPath;
- }
- /**
- * Nested creator, adds a set of files (nested fileset attribute).
- */
- public function createFileSet()
- {
- return $this->filesets[$num - 1];
- }
- /**
- * Whether the build should fail, if an error occured.
- *
- * @param boolean $value
- */
- public function setFailonerror( $value )
- {
- $this->failonerror = $value;
- }
- /**
- * sets the directory compressor traget dir
- *
- * @param string $targetDir
- */
- public function setTargetDir( $targetDir )
- {
- $this->targetDir = $targetDir;
- }
- /**
- * The init method: Do init steps.
- */
- public function init()
- {
- return true;
- }
- /**
- * The main entry point method.
- */
- public function main()
- {
- $command = 'java -jar {yuipath} {src} -o {target}';
- foreach( $this->filesets as $fs )
- {
- try
- {
- $files = $fs->getDirectoryScanner( $this->project )->getIncludedFiles();
- foreach( $files as $file )
- {
- $this->log( 'Minifying file ' . $file );
- {
- }
- $return = null;
- foreach( $output as $line )
- {
- $this->log( $line, Project::MSG_VERBOSE );
- }
- if( $return != 0 )
- {
- throw new BuildException( "Task exited with code $return" );
- }
- }
- }
- catch( BuildException $be )
- {
- // directory doesn't exist or is not readable
- if ($this->failonerror)
- {
- throw $be;
- }
- else
- {
- $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
- }
- }
- }
- }
- }
- ?>
Then in your phing build.xml
- <taskdef name="minify" classname="extended.tasks.kpMinTask" />
- <target name="minify-js">
- <echo>--------------------------------</echo>
- <echo>| Minify javascript to release |</echo>
- <echo>--------------------------------</echo>
- <minify targetDir="../www/resources/js/min"
- yuiPath="tools/yuicompressor.jar">
- <fileset dir="../www/resources/js/">
- <include name="*.js"/>
- </fileset>
- </minify>
- </target>
- <target name="minify-css">
- <echo>--------------------------------</echo>
- <echo>| Minify CSS to release |</echo>
- <echo>--------------------------------</echo>
- <minify targetDir="../www/resources/v2_css/min"
- yuiPath="tools/yuicompressor.jar">
- <fileset dir="../www/resources/v2_css/">
- <include name="*.css"/>
- </fileset>
- </minify>
- </target>
Now we can can compress easily by running phing minify-js, sweet...