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
MavenPlugin  

Featured
Updated Feb 12, 2012 by alex.obj...@gmail.com

Introduction

The main purpose of wro4j is to provide a runtime solution for optimizing web resource. Still, if you need a build time solution, the wro4j-maven-plugin can help you to achieve this goal.

Eclipse IDE support

Not is possible to use the wro4j-maven-plugin with an eclipse plugin called m2e-wro4j. More details about how to use it can be found on this blog post.

Details

There is almost no difference regarding project configuration. Thus, if you have configured your project for a runtime solution, there is almost nothing else to change. A standard folder structure, which is used by wro4j maven plugin as default looks like this:

All you have to do, is to add the following plugin dependency to the pom.xml of your web project:

<plugins>
  <plugin>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-maven-plugin</artifactId>
    <version>${wro4j.version}</version>
  </plugin>
</plugins>

This is the minimum necessary to get started. At this point, you can already start using it by running the following in command line:

  mvn wro4j:run -Dminimize=true -DtargetGroups=all

This command will generate a minimized version of all resources contained in 'all' group, for both type of resource: js & css.

You can configure all the properties in pom.xml in order to avoid specifying parameters in command line. In this case, your plugin configuration would look like this:

<plugins>
  <plugin>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-maven-plugin</artifactId>
    <version>${wro4j.version}</version>
    <executions>
      <execution>
        <phase>compile</phase>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <targetGroups>all</targetGroups>
      <destinationFolder>${basedir}/src/main/webapp/wro/</destinationFolder>
      <contextFolder>${basedir}/src/main/webapp/</contextFolder>
    </configuration>
  </plugin>
</plugins>

This is the smallest configuration with most of the parameters using default values. Eventually, you'll want to have a more advanced control over configuration. Below, you can see an example of configuration with all possible parameters set:

<plugins>
  <plugin>
    <groupId>ro.isdc.wro4j</groupId>
    <artifactId>wro4j-maven-plugin</artifactId>
    <version>${wro4j.version}</version>
    <executions>
      <execution>
        <phase>compile</phase>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <targetGroups>all</targetGroups>
      <minimize>true</minimize>
      <destinationFolder>${basedir}/src/main/webapp/wro/</destinationFolder>
      <cssDestinationFolder>d:/static/css/</cssDestinationFolder>
      <jsDestinationFolder>d:/static/js/</jsDestinationFolder>
      <contextFolder>${basedir}/src/main/webapp/</contextFolder>
      <wroFile>${basedir}/src/main/webapp/WEB-INF/wro.xml</wroFile>
      <wroManagerFactory>com.mycompany.MyCustomWroManagerFactory</wroManagerFactory>
      <ignoreMissingResources>false</ignoreMissingResources>
    </configuration>
  </plugin>
</plugins>

Plugin Parameters

Below is the description of parameters you can provide for the plugin:

  • minimize - a flag used to turn minimization on or off. This parameter is optional and by default its value is true.

  • targetGroups - (optional) a comma separated list of groups you want to build. If you do not specify this parameter, a file for each defined group will be generated.
A common practice is to have a single group (called 'all') which contains all other existing group. Still, if you want to build a file containing merged resources for other groups, you can do it like this:
  -DtargetGroups=group1,group2,group3
  • wroManagerFactory - Optional attribute, used to specify a custom implementation of MavenContextAwareManagerFactory interface. When this tag is not specified, a default implementation is used. This attribute is useful if you want to configure other processors than default one.
Since 1.3.0 version, wro4j provides two new manager factories:
  • ro.isdc.wro.extensions.manager.standalone.GoogleStandaloneManagerFactory: uses Google closure compiler for js compression
  • ro.isdc.wro.extensions.manager.standalone.YUIStandaloneManagerFactory: uses YUI compressor for js compression
Since 1.4.0 version, the following manager is available:
  • ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory - simplify the processors configurations using a simple property file (wro.properties), located by default at the following location:

${basedir}/src/main/webapp/WEB-INF/wro.properties. The processors can be configured by adding the following to wro.properties file:

preProcessors=cssImport,jsMin,cssMin
postProcessors=lessCss,coffeeScript

You can notice that the pre processors are provided using preProcessors key and the value is a comma separated list of processors aliases. The same applies to post processors, the only difference is the key, which is postProcessors in this case.

There are also few more other parameters which can be specified inside plugin configuration in pom.xml only:

  • wroFile - the location of wro.xml file which defines how resources are grouped. By default its value is this: ${basedir}/src/main/webapp/WEB-INF/wro.xml . If you have a different project structure or a different location of wro.xml, then you should change the value of this parameter.
  • contextFolder - defines the location of web application context, useful for locating resources relative to servletContext. By default its value is: ${basedir}/src/main/webapp/
  • destinationFolder - folder where the result merged resources will be created.
  • cssDestinationFolder - folder where the css merged resources will be created
  • jsDestinationFolder - folder where the js merged resources will be created
  • ignoreMissingResources - if false, the build will fail if at least one resource is missing or cannot be accessed, otherwise only a warning will be displayed.
  • groupNameMappingFile - (available since 1.4.0) is an optional parameter. When provided, it should be the path to the properties file where the mapping between original group names and renamed(by NamingStrategy) are described. The generated file can be used to identify the name of aggregated bundle name.
  • extraConfigFile - (available since 1.4.0) is an optional parameter. It is used to override the location of wro.properties configuration file, which has the default location here: /WEB-INF/wro.properties.

The parameters: destinationFolder, cssDestinationFolder, jsDestinationFolder are optional. When providing one of the following cssDestinationFolder or jsDestinationFolder, you have a more granular control over where to put the merged resources (useful when you want you css files to be located in different folder than the JavaScript).

In other words, if aggregated resources of both types should be located in the same folder, use destinationFolder parameter. However, if you want css resources to be located in a different folder than js resource, than cssDestinationFolder & jsDestinationFolder are your friends.

Note that if you are building a war file, you can specify the target directory by prefixing the destination directory path with '${project.build.directory}/${project.build.finalName}'.

Comment by pete.set...@gmail.com, Jun 23, 2011

How is the maven plugin supposed to interact with the cssUrlRewriting. I've managed to make things work to some degree by maintaining a parallel tree of static resources and disabling the cssUrlRewriting, but feel that I may be missing something important.

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

You can control the processors chain, find out how to do that here: http://code.google.com/p/wro4j/wiki/ProcessorsManagement

Comment by artur.or...@gmail.com, Jul 11, 2011

Hi,

I want to use several wro4j files and process them differently (e.g. different source files, different destination folders, different compression).

I created the wro.xml files in 4 different directories and would use all 4 for generating 4 different all-targets in different destination directories.

How can I specify 4 different configurations and 4 different compression algorithms (e.g. cssMin, beautifyJs, ...) in the pom.xml?

Thanks for your help

Artur

Comment by artur.or...@gmail.com, Jul 11, 2011

Sorry for the inconvinience,

I did find the solution by moving the configuration into the <execution> section and using different <id/>s for each section.

Cheers

Artur

Comment by project member alex.obj...@gmail.com, Jul 13, 2011

Hi Artur, you've found the solution I was about to suggest. Just one thing: there is no need to have multiple wro.xml files. You can create different groups and reuse the same wro.xml file for all processing variation you need.

Comment by artur.or...@gmail.com, Jul 22, 2011

Hi Alex,

thanks for your suggestion. I was using different wro4j configuration files because I wanted to generate the same files for two different devices, e.g. all.css for iphone and all.css for desktop. Both are using different input css files and the output css files should be stored in different directories. This is now working nicely with the different execution sections.

The only issue I've got at the moment is the wish to use the WRO4J maven plugin as simple as the WRO4J-Runner on command line. The reason is to use a simple way to specify the preprocessors by our web developr as propertty in the pom.xml like it works for WRO4J runner (-m -c BeautifyJs?). Especially because we want to use different compression algorithms for the "devel" profile and for the "prod" profile.

On "devel" we would like to use "cssLint" for css, "BeautifyJs?" for javascript, but on the prod profile perhaps "yuiCssMin" for CSS and "GoogleClosureAdvanced?" for javascript.

The easiest way would be the possibility to define it like the WRO4J URI filter in the <filter/> section using the ConfigurableWroManagerFactory and only specifying the <preprocessors> in the <configuration> instead of creating standalone factories for all algorithms available in WRO4J.

Thanks for your help

Artur

Comment by project member alex.obj...@gmail.com, Jul 22, 2011

I am thinking about creating somekind of DSL for specifying processors, which could be used by maven plugin. Not sure how this should look like yet.

This feature isn't hard to implement, but I don't want to add it unless I have a good and simple way to use it. Once a feature is added, it should be supported in future releases.

You could try to implement ProcessorsFactory? interface which would read a property file where you would define the processors you would like to be used during processors. Your implementation could be a good starting point for this feature, because you are the first one who had the idea for this use-case.

Comment by project member alex.obj...@gmail.com, Jul 26, 2011

The issue containing this feature has been created: http://code.google.com/p/wro4j/issues/detail?id=254

Comment by artur.or...@gmail.com, Jul 26, 2011

Hi,

I would keep it as consistent as possible with the <filter/> version of WRO4J. It would be enough to have the possibility to use <preprocessors>CssImportPre? lessCss dataUri yuiCssMin</preprocessors> as chain for one <configuration> section in the plugin.

Would it be too complicated to reuse the same code as in the <filter> configuration?

What do you think?

Thanks a lot for your help and your great work!

Artur

Comment by project member alex.obj...@gmail.com, Jul 26, 2011

Actually I would rather keep this kind of configuration out of web.xml. The preferred way would be a property file or something similar. Check out the updated documentation: http://code.google.com/p/wro4j/wiki/Installation

This approach would keep the web.xml clean and allow you to change configuration programatically based on your environment or other preferences.

Comment by d...@maucher-online.de, Oct 24, 2011

hi together,

I have a problem with the point that artur was asking: how to use multiple processings within one pom.xml? We did it like it is described here, but it is not working. the plugin is searching for the wro.xml instead of the specified wro-vcl.xml.

any ideas?

this is very importing for us.

regards dirk

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>myartifact</artifactId>
    <groupId>mytool</groupId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>${project.artifactId}.${project.packaging} - ${project.version}</name>
    <parent>
        <groupId>myartifactfather</groupId>
        <artifactId>myartifacttool</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../base-pom.xml</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.ibm.wps</groupId>
            <artifactId>portletapi</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <distributionManagement>
        <snapshotRepository>
            <id>nexus</id>
            <name>Repository</name>
            <url>myurl/</url>
        </snapshotRepository>
    </distributionManagement>
    <properties>
        <minimize>true</minimize>
    </properties>
    <build>
        <plugins>
            <!--  Optimizing css & js files -->
            <plugin>
                <groupId>ro.isdc.wro4j</groupId>
                <artifactId>wro4j-maven-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <!-- VCL css & js -->
                    <execution>
                        <id>vcl</id>
                        <configuration>
                            <targetGroups>vcl</targetGroups>
                            <wroFile>${basedir}/src/main/webapp/WEB-INF/wro-vcl.xml</wroFile>
                            <destinationFolder>${basedir}/src/main/webapp/vcl/</destinationFolder>
                            <extraConfigFile>${basedir}/src/main/webapp/WEB-INF/wro-vcl.properties</extraConfigFile>
                        </configuration>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <!-- js -->
                    <execution>
                        <id>js</id>
                        <configuration>
                            <targetGroups>all-js</targetGroups>
                            <wroFile>${basedir}/src/main/webapp/WEB-INF/wro-js.xml</wroFile>
                            <destinationFolder>${basedir}/src/main/webapp/js</destinationFolder>
                            <extraConfigFile>${basedir}/src/main/webapp/WEB-INF/wro-js.properties</extraConfigFile>
                        </configuration>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <contextFolder>${basedir}/src/main/webapp/</contextFolder>
                    <minimize>${minimize}</minimize>
                    <ignoreMissingResources>false</ignoreMissingResources>
                    <wroManagerFactory>ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory</wroManagerFactory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <SCM-Revision>${buildNumber}</SCM-Revision>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Comment by project member alex.obj...@gmail.com, Oct 24, 2011

Hi,

Can you show the content of the wro-vc.properties & wro-js.properties files?

Also, could we move this discussion to mailing list? (I don't want to pollute this wiki page with too much comments).

Thanks, Alex

Comment by project member alex.obj...@gmail.com, Oct 24, 2011

I have tried locally, and as long as all required files exist, everything works as expected.

Comment by d...@maucher-online.de, Oct 24, 2011

wro-vcl.properties:

preProcessors=cssUrlRewriting,yuiCssMin,yuiJsMinAdvanced
postProcessors=

but the problem is that the files are not loaded.

i start maven like this: mvn wro4j:run and this is the result:

D:\home\prjct\dsar\src\new-arch\common\fwk.theme.portal>set java_home=C:\Program Files (x86)\Java\jre6 
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building fwk.theme.war.war - 1.0.0-SNAPSHOT 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- wro4j-maven-plugin:1.4.1:run (default-cli) @ fwk.theme.war ---
[INFO] Executing the mojo: 
[INFO] Wro4j Model path: D:\home\prjct\src\new-arch\common\fwk.theme.portal\src\main\webapp\WEB-INF\wro.xml
[INFO] targetGroups: null
[INFO] minimize: true
[INFO] ignoreMissingResources: false
[INFO] wroManagerFactory: ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory
[INFO] destinationFolder: D:\home\prjct\src\new-arch\common\fwk.theme.portal\target\wro
[INFO] jsDestinationFolder: null
[INFO] cssDestinationFolder: null
[INFO] groupNameMappingFile: null
[INFO] wroManagerFactory class: ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.579s
[INFO] Finished at: Mon Oct 24 14:44:19 CEST 2011
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal ro.isdc.wro4j:wro4j-maven-plugin:1.4.1:run (default-cli) on project retacc.fwk.theme.war: Exception occured while processing: Exception while loading properties file from D:\home\prjct\dsar\src\new-arch\common\fwk.theme.portal\src\main\webapp\WEB-INF\wro.properties: D:\home\prjct\dsar\src\new-arch\common\fwk.theme.portal\src\main\webapp\WEB-INF\wro.properties (Das System kann die angegebene Datei nicht finden) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Comment by project member alex.obj...@gmail.com, Oct 24, 2011

Try to run it using:

mvn clean package

From some reason, when running the wro4j:run goal directly, the options aren't picked-up. I think this is something related to how maven works when multiple executions are configured (not related to wro4j maven plugin)

Comment by d...@maucher-online.de, Oct 25, 2011

it works. thank you for your support!


Sign in to add a comment
Powered by Google Project Hosting