Cactus Obfuscator Obfuscator JavaScript / CSS
Today, I would like to introduce to the community a utility written by my friend Nikolai Babinsky - Cactus Obfuscator .
Cactus Obfuscator is a three-module application designed to obfuscate JavaScript and CSS code.
Cactus Obfuscator uses:
Apache Maven - a plugin for Maven versions 2-3.
YUI Compressor - js / css obfuscation.
Simple XML - XML mapping and (de) serialization.
Content:
1. Use as a Maven plugin.
2. Use as a console application.
3. Creation of configuration files (XML).
Cactuscan be used as a Maven plugin in java projects, as well as a console utility.
Use as a Maven plugin.
It is obvious that in order to use the Maven plugin, we need installed Maven version 2 or 3 (cactus-plugin.jar for version 3 of Maven is provided for downloading, to get * .jar for version 2 you just need to
1. To build Cactus from source you need to perform the following steps:
- Download and go to the downloaded directory:
git clone [email protected]:nbabinski/Cactus.git
cd Cactus- Installation:
mvn install2. Installing the compiled cactus-plugin.jar package.
Open the terminal and write the following:
mvn install:install-file -Dfile=DOWNLOAD_FOLDER/cactus-plugin.jar -DgroupId=com.cactus -DartifactId=cactus-plugin -Dpackaging=maven-plugin -Dversion=0.1Where is “DOWNLOAD_FOLDER” the path to cactus-plugin.jar If the installation was successful, we see approximately the following picture: Congratulations, Cactus has been successfully installed. It remains to configure pom.xml of your project:
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.702s
[INFO] Finished at: Tue Apr 19 01:36:31 EEST 2011
[INFO] Final Memory: 2M/48M
...
com.cactus cactus-plugin 0.1 src/main/webapp/js cache obfuscate
...
Where:
baseDirectory - (optional) project directory. The default is $ {project.basedir}.
jsBaseDirectory - (required) Relative path from baseDirectory to a directory with JavaScript / CSS code.
confingDirectory - (optional) path to the folder with the cactus.xml configuration file, by default the same as jsBaseDirectory.
outputDirectory - (optional) relative path from jsBaseDirectory to the directory with processed / obfuscated files. A directory must exist.
mode - (optional) There are two operating modes - PRODUCTION / DEBUG. By default, the mode is set to PRODUCTION, that is, files are glued and obfuscated. In DEBUG mode - files are only glued together, but not obfuscated.
After connecting in pom.xml, you can try the mvn package to build the project. Cactus plugin can also be used without a project, all we need is the presence of a cactus.xml file. Having entered the folder with cactus.xml it is enough to type in the console:
mvn com.cactus:cactus-plugin:obfuscateUse as a console application.
Cactus can be used without Maven, just from the console. All we need is a JVM installed on the machine.
How to use:
1. Create a cactus.xml configuration file.
2. Run cactus-tool:
java -jar cactus-tool.jarBy default, cactus-tool requires only one -c option - the path to the folder containing cactus.xml.
There are also the following options:
-d - relative path to js files from the directory specified in -c
-o- the relative essence to the directory with the results of obfuscation. If not specified, the results will be placed in the -d
-m directory - PRODUCTION / DEBUG operation mode. Described above.
Usage example:
java -jar cactus-tool.jar -d ../web/js -o cacheCreating configuration files (XML)
Example configuration file:
https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core-debug.js https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js YOUR_CSS_FILE_HERE The example shows a configuration file with three files to exit (needles, in translation - needles, therefore, and cactus, in fact): ext-3.1.0.js, jquery-1.5.2.js and test.css
After launch: - We get how result of 2 js file. The file tag allows you to enter URLs, paths, file names. All folders must be inside the BASE directory (-c). For example, for the structure: The following cactus.xml is needed:
mkdir cactus-test
cd cactus-test
//copy cactus.xml and cactus-tool.jar in cactus-test folder
java -jar cactus-tool.jar -c PATH_TO_CACTUS_CONFIG_FOLDER|-root
|--batch
| | - cactus-tool.jar
|--webapp
|----js
| |--cactus.xml
| |--boo.js
| |--foo.js
| |--utils
| | |--array.js
| | |--string.js
| | |--parser
| | | |--xmlparser.js
| |--cacheboo.js utils utils/parser/xmlparser.js Next, run the cactus-tool from the root / batch directory:
java -jar cactus-tool.jar -c ../webapp/js -d ../webapp/js -o cacheIn this example, cactus.xml is in the js directory so the -c and -d options are the same.
As a result, we get the test.js file in the root / webappjs / cache folder, which is the result of merging and obfuscating the boo.js file, all files from the utils directory and utils / parser / xmlparser.js.
The sequence of file tags is important, it reflects in what order the files will be glued as a result.
UPD
In order to cut to the bud a huge amount of unnecessary controversy, I will write again that YUI Compressor is used for obfuscation .
By itself, Cactus is written to simplify the assembly and compression of js / css files.
Examples:
Before compression
After compression