GitHub Repository – with Maven project example
EDIT: wro4j is a plugin that makes use of the Closure compiler however it only allows for SIMPLE_CUSTOMIZATIONS level obfuscation.
Firstly, you need to include the Exec-Maven. This plugin allows you to execute Java applications during your build process. Include this plugin in your maven pom.xml like so:
Notice the configuration of the plugin. Firstly we are targeting the goal ‘java’. I shall discuss this later. Secondly, we define a main class. This is the Java application the plugin will run. Finally we have the arguments, these are the arguments that will be passed to the ‘public static void main(String args)’ method. In the example above we pass the input file and the location/name of the output file.
The class *must* have at least two arguments – the input and output file – however you can pass further arguments which allow you to customise the Closure compiler obfuscation.
- args = Input file (required)
- args = Output file (required)
- args = Compilation level (default ADVANCED_OPTIMIZATIONS)
- args = Output format (default test)
You can of course customise this class to make it perform as you like
Next notice the use of the ‘ClosureCompiler’ class, this class communicates with the Closure API
It fetches the obfuscated result and saves it to the specified output file
That just about completes the blog, the last thing is to show how your would invoke this. Thats a simple as:
You can of course customise the goal of the plugin so that it obfuscates as and when you need it to.
I would also welcome any comments or further customisations on this