Hi% username%,
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 - plugin for Maven versions 2-3.
YUI Compressor - js / css obfuscation.
Simple XML - XML ​​mapping and (de) serialization.
Content:
1. Use as Maven plugin.
2. Use as a console application.
3. Creating configuration files (XML).')
Cactus can be used as a Maven plugin in java-projects, as well as a console utility.
Use as maven plugin.It is obvious that to use Maven plug-in, we need Maven version 2 or 3 installed
(download provided by cactus-plugin.jar for Maven version 3, in order to get * .jar for version 2 it is enough to send SMS to compile it from sources with that version, what you need) .
1. To build Cactus from source, the following steps are required:
- Download and go to the downloaded directory:
git clone git@github.com:nbabinski/Cactus.git cd Cactus
- Installation:
mvn install
2. 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.1
Where “DOWNLOAD_FOLDER” is the path to cactus-plugin.jar
If the installation was successful, we see approximately the following picture:
[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
Congratulations, Cactus is successfully installed. It remains to configure your project's pom.xml:
<build> <plugins> ... <plugin> <groupId>com.cactus</groupId> <artifactId>cactus-plugin</artifactId> <version>0.1</version> <configuration> <jsBaseDirectory>src/main/webapp/js</jsBaseDirectory> <outputDirectory>cache</outputDirectory> </configuration> <executions> <execution> <goals> <goal>obfuscate</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build>
Where:
baseDirectory - (optional) project directory. The default is $ {project.basedir}.
jsBaseDirectory - (required) Relative path from baseDirectory to the directory with JavaScript / CSS code.
confingDirectory - (optional) path to the folder with the configuration file cactus.xml, the default is the same as jsBaseDirectory.
outputDirectory - (optional) relative path from jsBaseDirectory to the directory with processed / obfuscated files. The directory must exist.
mode - (optional) There are two modes of operation - PRODUCTION / DEBUG. The default mode is set to PRODUCTION, that is, the files are glued together and obfustsiruyutsya. In DEBUG mode, files are only glued together, but not obfuscated.
After connecting to pom.xml, you can try the
mvn package to build a project. The Cactus plugin can also be used without a project, all we need is the presence of the cactus.xml file. Going to the folder with cactus.xml, just type in the console:
mvn com.cactus:cactus-plugin:obfuscate
Use 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 the cactus-tool:
java -jar cactus-tool.jar
By default, the cactus-tool requires only one parameter —c — the path to the folder containing cactus.xml.
There are also the following parameters:
-d - relative path to js files from the directory specified in -c
-o - relative auth to the directory with the results of obfuscation. If not specified, the results will be placed in the -d directory.
-m - the operation mode PRODUCTION / DEBUG. Described above.
Example of use:
java -jar cactus-tool.jar -d ../web/js -o cache
Creating configuration files (XML)Sample configuration file:
<?xml version="1.0" encoding="UTF-8"?> <cactus> <js> <needles> <needle> <output>ext-3.1.0.js</output> <files> <file>https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core-debug.js</file> </files> </needle> <needle> <output>jquery-1.5.2.js</output> <files> <file>https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js</file> </files> </needle> </needles> </js> <css> <needles> <needle> <output>test.css</output> <files> <file>YOUR_CSS_FILE_HERE</file> </files> </needle> </needles> </css> </cactus>
The example shows a configuration file with three output files (needles, in translation, needles because of a cactus, strictly speaking): ext-3.1.0.js, jquery-1.5.2.js and test.css
After launch:
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
- We get the result of 2 js file.
Tag file allows you to enter URLs, paths, file names. All folders must be inside the BASE directory (-c). For example for the structure:
|-root
|--batch
| | - cactus-tool.jar
|--webapp
|----js
| |--cactus.xml
| |--boo.js
| |--foo.js
| |--utils
| | |--array.js
| | |--string.js
| | |--parser
| | | |--xmlparser.js
| |--cache
The following cactus.xml is needed:
<?xml version="1.0" encoding="UTF-8"?> <cactus> <js> <needles> <needle> <output>test.js</output> <files> <file>boo.js</file> <file>utils</file> <file>utils/parser/xmlparser.js</file> </files> </needle> </needles> </js> </cactus>
Next, run the cactus-tool from the root / batch directory:
java -jar cactus-tool.jar -c ../webapp/js -d ../webapp/js -o cache
In this example, cactus.xml is located in the js directory, so the -c and -d parameters are the same.
As a result, we get the test.js file in the root / webappjs / cache folder, which is the result of the merging and obfuscation of the boo.js file, all the 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 together as a result.
UPDIn order to stab a huge amount of unnecessary controversy on the vine, 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 compressionAfter compression