Java
Transparent javascript and css compression with Maven3 and Yuicompressor

There are a lot of ressources on how to use YUIcompressor on a Maven project, but several examples don’t work with Maven3, or don’t allow to replace javascript and css by their compressed version transparently.

The following example will compress files transparently when packaging the webapp in a WAR file, and keep original files for development.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.1.1</version>            
   <configuration>
      <warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
   </configuration>
</plugin>


<plugin>
   <groupId>net.alchim31.maven</groupId>
   <artifactId>yuicompressor-maven-plugin</artifactId>
   <executions>
      <execution>
         <id>compressyui</id>
         <phase>prepare-package</phase>
         <goals>
            <goal>compress</goal>
         </goals>
      </execution>

   </executions>
   <configuration>
      <nosuffix>true</nosuffix>
   </configuration>
</plugin>


  • Romain Gonord

    One thing missing is fusion of all files. Following this, it only minifies files.
    I’m using this plugin to do minify and fusion :
    com.samaxes.maven
    maven-minify-plugin
    1.3.5
    You can find info there : http://code.google.com/p/maven-samaxes-pluginIt's less handy to use as you have to tell explicitly which files you wan’t to insert, but hey, it does the trick =)

  • http://blog.richeton.com/ Nicolas Richeton

    Nice.

    Will try this plugin :)

    Does it have any known issue with eclipse/m2e/wtp ?

  • http://www.facebook.com/suresh.smurthy Suresh S Murthy

    I have been looking for this configuration for a couple of days now.. Thanks a ton for posting this one.. works like charm!