WRO4J (Web Resource Optimizer For Java) is an awesome open source resource optimizer. I recently implemented it in my application. So here I am providing steps I followed to implement it and issues I faced.
Tools used: Maven
1. Add maven dependency for the WRO4J in you pom.xml as follows:
2. Add a filter in web.xml as follows:This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<dependency> <groupId>ro.isdc.wro4j</groupId> <artifactId>wro4j-core</artifactId> <version>1.5.0</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>ro.isdc.wro4j</groupId> <artifactId>wro4j-extensions</artifactId> <version>1.5.0</version> <type>jar</type> <scope>compile</scope> </dependency>
3. Under WEB-INF create a folder wro.xml with content as follows:This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<filter> <filter-name>WebResourceOptimizer</filter-name> <filter-class>ro.isdc.wro.http.WroFilter</filter-class> </filter> <filter-mapping> <filter-name>WebResourceOptimizer</filter-name> <url-pattern>/resources/*</url-pattern> </filter-mapping>
This will create a js and css at runtime by combining all js and css under the group all and return and all.js and all.css respectively.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> <groups xmlns="http://www.isdc.ro/wro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.isdc.ro/wro wro.xsd"> <group name="all"> <js>/js/app.js</js> <js>/js/app1.js</js> <css>/css/main.css</css> <css>/css/main1.css</css> </group> </groups>
4. Under same folder WEB-INF create another file wro.properties with content as follows:
debug=true
disableCache=true
gzipResources=true
jmxEnabled=false
preProcessors=semicolonAppender
postProcessors=jsMin,cssCompressor,cssMin
5. Open any new existing JSP page and add a js call as follows:
Thats it!! Now start your server and open your page.and for css add the call as:
I faced one issue while implementing wro4j and that is due to a dependency of wro4j jars. This version of wro4j requires commons-io.2.1 but because some other dependency older version of common-io got loaded. I did not get any error but was getting empty results when calling /wro/all.js and /wro/all.css. So, be careful.
~Manish
Thanks for the tutorial.
ReplyDelete