Here you will find several use-cases and implementation examples.
Use a Custom Resource Processor
If you have a use-case, where you need to run a single custom processor (called MyCustomPostProcessor) on all resources found in the model, you have to follow these steps:
- Create a custom WroManagerFactory
This code applies for the wro4j versions >= 1.3.6
public class MyCustomWroManagerFactory
extends DefaultStandaloneContextAwareManagerFactory{
@Override
protected ProcessorsFactory newProcessorsFactory() {
SimpleProcessorsFactory factory = (SimpleProcessorsFactory) super.newProcessorsFactory();
factory.addPostProcessor(new MyCustomPostProcessor());
}
}Note: If you are using 1.2.x branch, then instead of DefaultStandaloneContextAwareManagerFactory use DefaultMavenContextAwareManagerFactory.
As you can see, we just override the method responsible for processors configuration. Here you can add any processors you want (custom or existing ones).
- Update pom.xml configuration
<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>com.mycompany.MyCustomWroManagerFactory</wroManagerFactory>
</configuration>
</plugin>Alternatively, you can set the wroManagerFactory when explicitly running plugin:
mvn wro4j:run -DwroManagerFactory=com.mycompany.MyCustomWroManagerFactory
There are several existing implementations of manager factories you can use for maven plugin are:
- ro.isdc.wro.extensions.manager.standalone.GoogleStandaloneManagerFactory
- ro.isdc.wro.extensions.manager.standalone.YUIStandaloneManagerFactory
Example
This example will show how you can easily use google closure with advanced optimization mode (the existing factory GoogleStandaloneManagerFactory uses simple optimization mode).
Based on documentation about the customization, you can easily provide a custom processor in the wro4j maven plugin flow. Integrating google closure with advanced optimization is simple as:
Create custom wro manager factory
public class GoogleClosureAdvancedWroManagerFactory
extends DefaultStandaloneContextAwareManagerFactory{
@Override
protected ProcessorsFactory newProcessorsFactory() {
SimpleProcessorsFactory factory = (SimpleProcessorsFactory) super.newProcessorsFactory();
factory.addPostProcessor(new GoogleClosureCompressorProcessor(CompilationLevel.ADVANCED_OPTIMIZATIONS));
}
}Update wro4j maven plugin configuration
<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>com.mycompany.GoogleClosureAdvancedWroManagerFactory</wroManagerFactory>
</configuration>
</plugin>Alternatively, you can set the wroManagerFactory when explicitly running plugin:
mvn wro4j:run -DwroManagerFactory=com.mycompany.GoogleClosureAdvancedWroManagerFactory
Where do I put the class for my Custom Resource Processor? In the project? Or somewhere else in the file system?
It should be available in the classpath of the project where you are using wro4j maven plugin. So, yes, in the project is ok.
Thanks for the quick response.
Ok, it is finding my class when I put it in src/main/java, but what about DefaultStandaloneContextAwareManagerFactory?? It's not in my class path.
DefaultStandaloneContextAwareManagerFactory? is part of wro4j-core.jar Since you are using wro4j, this class is in your classpath.
Got it. After that was found, I was missing javax.servlet.ServletContext?. I found a random jar that had that class in it (what should I be using?). Then also, a minor detail, I had to return factory in the newProcessorsFactory() method. Thanks!
ServletContext? is a class from servlet-api.jar. Probably you are not using maven for dependencies management. In that case you have to manually add required jar's to your classpath.
Did you manage to return a factory in newProcessorsFactory() method? There are several examples in wiki. Also, you can take a look on existing implementations from the source code.
Hi!
What about a use case where I need to run a custom processor AND some other processors? I mean, is it possible to use the ConfigurableWroManagerFactory and specify a custom Processor in the configuration? If so, where/how do I register my custom processor?
Thanks!
Just extend the ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory and override the protected Map<String, ResourcePostProcessor?> createPostProcessorsMap() or protected Map<String, ResourcePreProcessor?> createPreProcessorsMap()
method. The returned value should contain alias with the corresponding processor instances. If you have other question, you can use the mailing list.
Ok, it works now. I was extending the wrong class (ro.isdc.wro.manager.factory.ConfigurableWroManagerFactory).
Thanks.