My favorites | Sign in
Project Hosting will be READ-ONLY Thursday at 3:00pm UTC for up to 3 hours for network maintenance.
Project Home Downloads Wiki Issues Source
Search
for
MavenPluginWithYuiOrGoogleClosure  
Describes how wro4j maven plugin can be used to use google closure compiler or YUI compressor
Updated Jul 19, 2011 by alex.obj...@gmail.com

Introduction

Since 1.3.x branch release, wro4j provides some fixes which allows easily customize the way your resources are compressed.

Prerequisites: GettingStarted & MavenPlugin

Wro4j Maven Plugin with Google Closure

Google closure compiler is one of the best compressors out there. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls.

Note: the wro4j factory invokes Google Closure Compiler with SIMPLE_OPTIMIZATIONS.

In order to make it work with wro4j maven plugin you have to do the following:

  1. Add wro4j-extensions library to the classpath. It is recommended to use maven for adding this dependency, because other transitive dependencies will be resolved by maven.
  2.     <dependency>
          <groupId>ro.isdc.wro4j</groupId>
          <artifactId>wro4j-extensions</artifactId>
          <version>${wro4j.version}</version>
        </dependency>
  1. Configure wro4j maven plugin in pom.xml
  2.       <plugin>
            <groupId>ro.isdc.wro4j</groupId>
            <artifactId>wro4j-maven-plugin</artifactId>
            <version>${wro4j.version}</version>
            <executions>
              <execution>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <wroManagerFactory>ro.isdc.wro.extensions.manager.standalone.GoogleStandaloneManagerFactory</wroManagerFactory>
            </configuration>
          </plugin>

Alternatively you can configure the managerFactory from the command line when invoking manually the plugin:

mvn wro4j:run -DwroManagerFactory=ro.isdc.wro.extensions.manager.standalone.GoogleStandaloneManagerFactory

Wro4j Maven Plugin with YUICompressor

YUICompressor is also very popular tool. Tests on the YUI Library have shown savings of over 20% compared to JSMin (becoming 10% after HTTP compression). It does also compress CSS files, not only javascripts.

In order to make it work with wro4j maven plugin, you have to follow similar steps as with google closure compiler:

  1. Add wro4j-extensions library to the classpath. It is recommended to use maven for adding this dependency, because other transitive dependencies will be resolved by maven.
  2.     <dependency>
          <groupId>ro.isdc.wro4j</groupId>
          <artifactId>wro4j-extensions</artifactId>
          <version>${wro4j.version}</version>
        </dependency>
  3. Configure wro4j maven plugin in pom.xml
  4.       <plugin>
            <groupId>ro.isdc.wro4j</groupId>
            <artifactId>wro4j-maven-plugin</artifactId>
            <version>${wro4j.version}</version>
            <executions>
              <execution>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <wroManagerFactory>ro.isdc.wro.extensions.manager.standalone.YUIStandaloneManagerFactory</wroManagerFactory>
            </configuration>
          </plugin>

Alternatively you can configure the managerFactory from the command line when invoking manually the plugin:

mvn wro4j:run -DwroManagerFactory=ro.isdc.wro.extensions.manager.standalone.YUIStandaloneManagerFactory
Comment by garretdw...@gmail.com, Jan 28, 2011

Can you provide a more extensive explanation of what this configuration does? Perhaps you could additionally provide an example that does the following:

Input files: src/main/javascript/com/example/js/foo.js src/main/javascript/com/example/js/bar.js

Output files: target/classes/com/example/js/foo-min.js target/classes/com/example/js/bar-min.js

Thanks.

Comment by project member alex.obj...@gmail.com, Jan 28, 2011

When using wro4j, you don't have to manually add files to compress and the output file location. All you have to do, is to define wro.xml file which describes how resources are grouped. This is described here: http://code.google.com/p/wro4j/wiki/GettingStarted and http://code.google.com/p/wro4j/wiki/MavenPlugin

Comment by ahaer...@gmail.com, Apr 14, 2011

Is it possible to set the Closure Compiler optimization level to ADVANCED_OPTIMIZATIONS?

Thanks

Comment by project member alex.obj...@gmail.com, Apr 14, 2011

Yes, it is. Create a custom implementation of manager factory, similar to GoogleStandaloneManagerFactory?

Example:

public class GoogleStandaloneManagerFactory? extends DefaultStandaloneContextAwareManagerFactory? {

@Override protected ProcessorsFactory? newProcessorsFactory() {
final SimpleProcessorsFactory? factory = new SimpleProcessorsFactory?(); factory.addPreProcessor(new BomStripperPreProcessor?()); factory.addPreProcessor(new CssImportPreProcessor?()); factory.addPreProcessor(new CssUrlRewritingProcessor?()); factory.addPreProcessor(new SemicolonAppenderPreProcessor?()); factory.addPreProcessor(new GoogleClosureCompressorProcessor?(CompilationLevel?.ADVANCED_OPTIMIZATIONS)); factory.addPreProcessor(new JawrCssMinifierProcessor?());

factory.addPostProcessor(new CssVariablesProcessor?()); return factory;
}

}

and use this class when setting the wroManagerFactory property value

Comment by monkeybo...@gmail.com, May 6, 2011

Hi, is it possible to configure closure compiler to strip out a specified regex pattern from your javascript files?

thanks Jon

Comment by project member alex.obj...@gmail.com, May 6, 2011

That is actually a question for google closure compiler developers. If it is possible, than you can create a custom implementation of GoogleClosureCompressorProcessor? which does exactly what you need and add that custom processor to the processorsFactory.

Comment by anton.ne...@gmail.com, Jun 9, 2011

Alex, could you add ADVANCED_OPTIMIZATIONS version of a Factory to your standard distribution?

Comment by project member alex.obj...@gmail.com, Jun 14, 2011

I will add a new factory which will use ADVANCED_OPTIMIZATION.

Comment by anton.ne...@gmail.com, Jun 16, 2011

Alex, thanks a lot! Waiting for new release!

Comment by jvers...@gmail.com, Oct 31, 2011

ADVANCED_OPTIMIZATION is available:

<plugin>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-maven-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>run</goal> 
            </goals> 
        </execution> 
    </executions>
    <configuration>
        <targetGroups>all</targetGroups>
        <destinationFolder>${basedir}/src/main/webapp/dst/</destinationFolder>
        <contextFolder>${basedir}/src/main/webapp/</contextFolder>
        <wroManagerFactory>ro.isdc.wro.extensions.manager.standalone.GoogleAdvancedStandaloneManagerFactory</wroManagerFactory> 
    </configuration>` 
</plugin> 
Comment by project member alex.obj...@gmail.com, Feb 14, 2012

Do you mean disable minimization? If yes, use the minimize flag and set it to false. We can continue this subject on the mailing list (which is more appropriate for this kind of discussion).


Sign in to add a comment
Powered by Google Project Hosting