Calling Zelix KlassMaster from Apache Ant
Zelix KlassMaster exposes a generic Java API that allows it to be called by a build tool.
A simple Ant task for the Zelix KlassMaster obfuscator is built on this API and is already included in the ZKM.jar file.
The task is called ZKMTask .
A simple example build.xml with a corresponding ZKM Script and the source code of the Ant task ZKMTask appear below.
The attributes to the ZKMTask task correspond to the basic command line options.
The ZKM Script referred to in the build XML typically makes use of System Variables that have been set in the build XML.
You can also set Special Configuration Options by setting System properties.
<?xml version="1.0"?>
<project name="ZKMTask" default="main" basedir=".">
<property name="LOGS_DIR" value="logs"/>
<property name="OPEN_CLASSES" value="\Projects\MyJar.jar"/>
<property name="SAVE_DIR" value="\projects\obfuscated"/>
<path id="ZKM_classpath">
<fileset dir="C:\JDK\j2sdkee1.4\lib" includes="j2ee.jar"/>
<fileset dir="C:\JDK\WTK21\wtklib\" includes="kenv.zip"/>
<fileset dir="D:\Java\apache-ant-1.6.5\lib\" includes="ant.jar" />
</path>
<property name="myClasspath" refid="ZKM_classpath"/>
<taskdef name="ZKM" classname="ZKMTask" classpath="ZKM.jar"/>
<target name="main">
<!-- Attributes correspond to Zelix KlassMaster command line options.
scriptFileName is only mandatory attribute -->
<ZKM scriptFileName="myZKMScript.txt"
logFileName="ZKM_log.txt"
trimLogFileName="ZKM_TrimLog.txt"
defaultExcludeFileName="defaultExclude.txt"
defaultTrimExcludeFileName="defaultTrimExclude.txt"
defaultDirectoryName="."
isParseOnly="false"
isVerbose="true"
>
<!-- the sysproperty tag provides an easy way of setting a System property -->
<sysproperty key="ZKM_NEW_CHANGE_LOG_ENCODING" value="UTF-16" />
</ZKM>
</target>
</project>
//"myClasspath" is a property passed in from the build XML
classpath "%myClasspath%";
//"OPEN_CLASSES" is a property passed in from the build XML
open "%OPEN_CLASSES%";
trimExclude *.*^ public static main(java.lang.String[]);
trim;
exclude *.^*^ public static main(java.lang.String[]);
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
obfuscateFlow=aggressive
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate
preverify=true
;
//"SAVE_DIR" is a property passed in from the build XML
saveAll "%SAVE_DIR%";
ZKMTask is a simple Ant task definition for the Zelix KlassMaster obfuscator.
It passes in the Ant Project properties by default. It can be modified to pass in Properties of your own choosing.
The compiled version of this class is already included in the ZKM.jar file.
/*
* Copyright (c) 2003-2024 Zelix Pty Ltd (ACN 078 740 093). All Rights Reserved.
*
* You are free to use this software with or without modifications
* subject to the following conditions.
*
* THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF ANY KIND.
* ZELIX PTY LTD MAKES NO WARRANTIES, EITHER EXPRESS OR IMPLIED, WITH
* RESPECT TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO ANY WARRANTY
* OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE.
* ZELIX PTY LTD DOES NOT WARRANT THAT THE OPERATION OF THE SOFTWARE WILL
* BE UNINTERRUPTED OR ERROR-FREE, OR THAT DEFECTS IN THE SOFTWARE WILL
* BE CORRECTED. YOU THE USER ARE SOLELY RESPONSIBLE FOR DETERMINING THE
* APPROPRIATENESS OF THE SOFTWARE FOR YOUR USE AND ACCEPT FULL RESPONSIBILITY
* FOR ALL RISKS ASSOCIATED WITH ITS USE. ZELIX PTY LTD IS NOT AND WILL
* NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR INCIDENTAL DAMAGES
* (INCLUDING LOSS OF PROFITS OR INTERRUPTION OF BUSINESS) HOWEVER CAUSED
* EVEN IF ZELIX PTY LTD HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
import org.apache.tools.ant.*;
import java.util.*;
public class ZKMTask extends Task {
private static final String TRUE = "true";
private static final String YES = "yes";
private String scriptFileName;
private String logFileName;
private String trimLogFileName;
private String defaultExcludeFileName;
private String defaultTrimExcludeFileName;
private String defaultDirectoryName;
private boolean isVerbose = false;
private boolean isParseOnly = false;
/**
* A ZKM Script file name must be specified.
*/
public void setScriptFileName(String scriptFileName) {
this.scriptFileName = scriptFileName;
}
/**
* Set the log file name. The file name can be relative or absolute. Optional.
* The log file name will default to "ZKM_log.txt".
*/
public void setLogFileName(String logFileName) {
this.logFileName = logFileName;
}
/**
* Set the trim log file name. The file name can be relative or absolute. Optional.
* The trim log file name will default to "ZKM_TrimLog.txt".
*/
public void setTrimLogFileName(String trimLogFileName) {
this.trimLogFileName = trimLogFileName;
}
/**
* Set the 'default exclusions' file name. The file name can be relative or absolute. Optional.
* By default Zelix KlassMaster will look for a file named "defaultExclude.txt".
*/
public void setDefaultExcludeFileName(String defaultExcludeFileName) {
this.defaultExcludeFileName = defaultExcludeFileName;
}
/**
* Set the 'default trim exclusions' file name. The file name can be relative or absolute. Optional.
* By default Zelix KlassMaster will look for a file named "defaultTrimExclude.txt".
*/
public void setDefaultTrimExcludeFileName(String defaultTrimExcludeFileName) {
this.defaultTrimExcludeFileName = defaultTrimExcludeFileName;
}
/**
* Set the 'default method parameter changes exclusions' file name. The file name can be relative or absolute. Optional.
* By default Zelix KlassMaster will look for a file named "defaultMethodParameterChangesExclude.txt".
*/
public void setDefaultMethodParameterChangesExcludeFileName(String defaultMethodParameterChangesExcludeFileName) {
this.defaultMethodParameterChangesExcludeFileName = defaultMethodParameterChangesExcludeFileName;
}
/**
* Set the 'default method parameter obfuscation exclusions' file name. The file name can be relative or absolute. Optional.
* By default Zelix KlassMaster will look for a file named "defaultMethodParameterObfuscationExclude.txt".
*/
public void setDefaultMethodParameterObfuscationExcludeFileName(String defaultMethodParameterObfuscationExcludeFileName) {
this.defaultMethodParameterObfuscationExcludeFileName = defaultMethodParameterObfuscationExcludeFileName;
}
/**
* Set the name of the default directory . The directory name can be relative or absolute.
* If any of the log file, trim log file, 'default exclusion' file and the 'default trim exclusions' file names are relative then
* they will be relative to this miscellaneous files directory. Optional.
* The directory name will default to that of the current working directory.
*/
public void setDefaultDirectoryName(String defaultDirectoryName) {
this.defaultDirectoryName = defaultDirectoryName;
}
/**
* Optional. Defaults to false.
* If true then Zelix KlassMaster will parse the ZKM Script but not execute it.
* @param isParseOnlyString Should be "true" or "false"
*/
public void setIsVerbose(String isVerboseString) {
if (isVerboseString != null) {
if (isVerboseString.equalsIgnoreCase(TRUE) ||
isVerboseString.equalsIgnoreCase(YES))
{
isVerbose = true;
}
}
}
/**
* Optional. Defaults to false.
* @param isParseOnlyString Should be "true" or "false"
*/
public void setIsParseOnly(String isParseOnlyString) {
if (isParseOnlyString != null) {
if (isParseOnlyString.equalsIgnoreCase(TRUE) ||
isParseOnlyString.equalsIgnoreCase(YES))
{
isParseOnly = true;
}
}
}
public void execute() throws BuildException {
if (scriptFileName == null || scriptFileName.length() == 0) {
throw new BuildException("Missing or empty ZKM Script file name");
}
if (sysproperties != null) {
Properties systemProperties = System.getProperties();
for (Iterator it=sysproperties.iterator(); it.hasNext(); ) {
Sysproperty sysproperty = (Sysproperty)it.next();
systemProperties.put(sysproperty.getKey(), sysproperty.getValue());
}
}
try {
com.zelix.ZKM.run(scriptFileName, logFileName, trimLogFileName, defaultExcludeFileName, defaultTrimExcludeFileName,
defaultDirectoryName, isVerbose, isParseOnly, project.getProperties());
}
catch(Exception ex) {
ex.printStackTrace();
throw new BuildException(ex.toString());
}
}
ArrayList sysproperties = new ArrayList();
public Sysproperty createSysproperty() {
Sysproperty sysproperty = new Sysproperty();
sysproperties.add(sysproperty);
return sysproperty;
}
public class Sysproperty {
private String key;
private String value;
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
|