Spring Boot configuration of wro4j
How to configure wro4j (Web Resource Optimizer for Java) with Spring Boot using runtime mode and an HTTP filter, without any third-party Spring Boot starters.
What is wro4j
wro4j (Web Resource Optimizer for Java) allows you to optimize your web resources. When developing rich web applications with multiple CSS/JS frameworks alongside custom code, bundling resources into fewer HTTP requests while minimizing and compressing content improves performance — particularly for mobile users.
wro4j operates in two modes: runtime and compile time. Runtime setup with an HTTP filter was chosen here for development convenience, avoiding the complexity of compile-time configuration and the need for additional Maven repositories.
Spring Boot Setup
wro4j lacks native Spring Boot support. Rather than adding third-party dependencies, manual configuration was implemented.
Maven Dependencies
<!-- wro4j -->
<dependency>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-core</artifactId>
<version>1.7.8</version>
</dependency>
<dependency>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-extensions</artifactId>
<version>1.7.8</version>
</dependency>Configuration Class
Register a FilterRegistrationBean that reads wro4j properties from application.properties using the wro. prefix. The filter maps to /wro/* paths.
@Bean
FilterRegistrationBean webResourceOptimizer(Environment env) {
FilterRegistrationBean fr = new FilterRegistrationBean();
ConfigurableWroFilter filter = new ConfigurableWroFilter();
filter.setProperties(buildWroProperties(env));
fr.setFilter(filter);
fr.addUrlPatterns("/wro/*");
return fr;
}
private static final String[] OTHER_WRO_PROP = new String[] {
ConfigurableProcessorsFactory.PARAM_PRE_PROCESSORS,
ConfigurableProcessorsFactory.PARAM_POST_PROCESSORS
};
private Properties buildWroProperties(Environment env) {
Properties prop = new Properties();
for (ConfigConstants c : ConfigConstants.values()) {
addProperty(env, prop, c.name());
}
for (String name : OTHER_WRO_PROP) {
addProperty(env, prop, name);
}
log.debug("Wro4J properties {}", prop);
return prop;
}
private void addProperty(Environment env, Properties to, String name) {
String value = env.getProperty("wro." + name);
if (value != null) {
to.put(name, value);
}
}wro.xml Configuration
Place this file in the WEB-INF directory. It specifies resource groups that wro4j will bundle together:
<?xml version="1.0" encoding="UTF-8"?>
<groups xmlns="http://www.isdc.ro/wro">
<group name="other">
<js>classpath:static/jquery/jquery-1.10.1.min.js</js>
<js>classpath:static/timeago/jquery.timeago.1.4.1.js</js>
<css>classpath:static/slider/slider.css</css>
</group>
</groups>application.properties
# wro4j config
wro.preProcessors=cssUrlRewriting,cssImport,semicolonAppender,lessCss
wro.postProcessors=cssVariables,cssMinJawr,jsMin
# set to 1 second to check for resource changes
wro.resourceWatcherUpdatePeriod = 0
wro.ignoreMissingResources = falseUpdate
For non-WAR deployments, you need to override where wro.xml comes from. A sample project demonstrating the alternative wro.xml configuration approach for non-WAR deployments is available on GitHub.