com.pyx4me.maven.j2me.ProGuardMojo Maven / Gradle / Ivy
/**
* Pyx4me framework
* Copyright (C) 2006-2007 pyx4.com.
*
* @author vlads
* @version $Id: ProGuardMojo.java 1529 2007-10-19 17:51:37Z vlads $
*/
package com.pyx4me.maven.j2me;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Java;
import de.pleumann.antenna.misc.Utility;
/**
*
*
* The Obfuscate task provides a stand-alone obfuscation task
*
*
* @goal obfuscate
* @phase package
* @description Create JAD files
*
*/
public class ProGuardMojo extends WtkJadMojo {
/**
* Use ProGuard preverification instead of WTK. (ProGuard option
* -microedition)
*
* @parameter default-value="false"
*/
private boolean proguardPreverify = false;
/**
* ProGuard configuration options
*
* @parameter
*/
private ProguardOptions proguardOptions;
/**
* Recursively reads configuration options from the given file filename
*
* @parameter default-value="${basedir}/proguard.conf"
*/
private File proguardInclude;
/**
* Apply ProGuard classpathentry Filters to input jar. e.g.
* !**.gif,!**/awt/**,!**/tests/**'
*
* @parameter
*/
protected String inFilter;
static final String proguardMainClass = "proguard.ProGuard";
/**
* Specifies not to obfuscate the input class files.
*
* @parameter default-value="true"
*/
private boolean obfuscate;
public void execute() throws MojoExecutionException, MojoFailureException {
executeProGuard(getJarFile(classifier), null, proguardInclude, obfuscate, proguardPreverify, this, false, null,
inFilter, proguardOptions, null);
super.execute();
}
/**
* ProGuard docs: Names with special characters like spaces and parentheses
* must be quoted with single or double quotes.
*/
private static String fileNameToString(String fileName) {
return "'" + fileName + "'";
}
private static String fileToString(File file) {
return fileNameToString(file.toString());
}
public static void executeProGuard(File jarFile, File outFile, File proguardInclude, boolean obfuscate,
boolean proguardPreverify, AbstractJadWtkMojo mojo, boolean isTest, File proguardTestInclude,
String inFilter, ProguardOptions options, ProguardOptions optionsTest) throws MojoExecutionException,
MojoFailureException {
ArrayList args = new ArrayList();
if (outFile == null) {
outFile = jarFile;
}
File baseFile;
if (jarFile.isDirectory()) {
baseFile = jarFile;
} else {
baseFile = new File(jarFile.toString() + "_proguard_base.jar");
if (baseFile.exists()) {
if (!baseFile.delete()) {
throw new MojoFailureException("Can't delete " + baseFile);
}
}
if (!jarFile.renameTo(baseFile)) {
throw new MojoFailureException("Can't rename " + jarFile);
}
}
args.add("-injars");
StringBuffer filter = new StringBuffer(fileToString(baseFile));
filter.append("(");
filter.append("!META-INF/maven/**");
if (inFilter != null) {
filter.append(",");
filter.append(inFilter);
}
filter.append(")");
args.add(filter.toString());
args.add("-outjars");
args.add(fileToString(outFile));
if (!obfuscate) {
args.add("-dontobfuscate");
}
if (proguardPreverify) {
args.add("-microedition");
}
if (proguardInclude != null) {
if (proguardInclude.exists()) {
args.add("-include");
args.add(fileToString(proguardInclude));
mojo.getLog().debug("proguardInclude " + proguardInclude);
} else {
mojo.getLog().debug("proguardInclude config does not exists " + proguardInclude);
}
}
if (isTest && proguardTestInclude != null) {
// Can't pass this safle as cms line args beacuse of *
// args.add("-keep class * extends junit.framework.TestCase");
if (proguardTestInclude.exists()) {
args.add("-include");
args.add(fileToString(proguardTestInclude));
mojo.getLog().debug("proguardTestInclude " + proguardTestInclude);
} else {
mojo.getLog().debug("proguardTestInclude config does not exists " + proguardTestInclude);
}
}
if (mojo.useWtkLibs) {
// Find MIDP ClassPath from WTK
Project antProject = mojo.getAntProject();
Task dummyTaks = new Task() {
};
dummyTaks.setTaskName("proguard");
dummyTaks.setProject(antProject);
Utility utility = Utility.getInstance(antProject, dummyTaks);
String mdipClassPath = utility.getMidpApi();
args.add("-libraryjars");
args.add(mdipClassPath);
} else {
if (mojo.libs != null) {
for (Iterator i = mojo.libs.iterator(); i.hasNext();) {
Object lib = i.next();
args.add("-libraryjars");
args.add(fileNameToString(lib.toString()));
}
}
List dependencies = mojo.mavenProject.getCompileArtifacts();
for (Iterator i = dependencies.iterator(); i.hasNext();) {
Artifact artifact = (Artifact) i.next();
if (!mojo.isUseDependency(artifact)) {
continue;
}
Dependency dependencyConfig = mojo.getDependencyConfig(artifact);
String libraryjar = fileToString(getClasspathElement(artifact, mojo.mavenProject));
if (dependencyConfig != null) {
if (dependencyConfig.filter != null) {
StringBuffer libraryFilter = new StringBuffer(libraryjar);
libraryFilter.append("(");
// libraryFilter.append("!META-INF/maven/**");
libraryFilter.append(dependencyConfig.filter);
libraryFilter.append(")");
libraryjar = libraryFilter.toString();
}
}
args.add("-libraryjars");
args.add(libraryjar);
}
}
// Seeds main classes
if (mojo.midlets != null) {
for (int i = 0; i < mojo.midlets.length; i++) {
MIDlet mp = mojo.midlets[i];
args.add("-keep public class " + mp.cls);
}
}
args.add("-printmapping");
args.add(fileToString((new File(mojo.outputDirectory, "proguard" + (isTest ? "_test" : "") + "_map.txt")
.getAbsoluteFile())));
args.add("-printseeds");
args.add(fileToString((new File(mojo.outputDirectory, "proguard" + (isTest ? "_test" : "") + "_seeds.txt")
.getAbsoluteFile())));
if (mojo.getLog().isDebugEnabled()) {
args.add("-verbose");
}
if ((options != null) && (options.options != null)) {
for (int i = 0; i < options.options.length; i++) {
args.add(options.options[i]);
}
}
if ((isTest) && (optionsTest != null) && (optionsTest.options != null)) {
for (int i = 0; i < optionsTest.options.length; i++) {
args.add(optionsTest.options[i]);
}
}
mojo.getLog().info("execute ProGuard " + args.toString());
proguardMain(getProguardJar(mojo), args, mojo);
}
static boolean isVersionGrate(Artifact artifact1, Artifact artifact2) {
if ((artifact2 == null) || (artifact2.getVersion() == null)) {
return true;
}
if ((artifact1 == null) || (artifact1.getVersion() == null)) {
return false;
}
// Just very simple
return (artifact1.getVersion().compareTo(artifact2.getVersion()) > 0);
}
private static File getProguardJar(AbstractJadWtkMojo mojo) throws MojoExecutionException {
Artifact proguardArtifact = null;
int proguardArtifactDistance = -1;
// This should be solved in Maven 2.1
for (Iterator i = mojo.pluginArtifacts.iterator(); i.hasNext();) {
Artifact artifact = (Artifact) i.next();
mojo.getLog().debug("pluginArtifact: " + artifact.getFile());
if ("proguard".equals(artifact.getArtifactId())) {
int distance = artifact.getDependencyTrail().size();
mojo.getLog().debug("proguard DependencyTrail: " + distance);
if (proguardArtifactDistance == -1) {
proguardArtifact = artifact;
} else if (distance < proguardArtifactDistance) {
proguardArtifact = artifact;
proguardArtifactDistance = distance;
}
// if (isVersionGrate(artifact, proguardArtifact)) {
// proguardArtifact = artifact;
// break;
// }
}
}
if (proguardArtifact != null) {
mojo.getLog().debug("proguardArtifact: " + proguardArtifact.getFile());
return proguardArtifact.getFile().getAbsoluteFile();
}
mojo.getLog().info("proguard jar not found in pluginArtifacts");
ClassLoader cl;
cl = mojo.getClass().getClassLoader();
// cl = Thread.currentThread().getContextClassLoader();
String classResource = "/" + proguardMainClass.replace('.', '/') + ".class";
URL url = cl.getResource(classResource);
if (url == null) {
throw new MojoExecutionException("Obfuscation failed ProGuard (" + proguardMainClass
+ ") not found in classpath");
}
String proguardJar = url.toExternalForm();
if (proguardJar.startsWith("jar:file:")) {
proguardJar = proguardJar.substring("jar:file:".length());
proguardJar = proguardJar.substring(0, proguardJar.indexOf('!'));
} else {
throw new MojoExecutionException("Unrecognized location (" + proguardJar + ") in classpath");
}
return new File(proguardJar);
}
private static void proguardMain(File proguardJar, ArrayList argsList, AbstractJadWtkMojo mojo)
throws MojoExecutionException {
Java java = new Java();
java.setProject(mojo.getAntProject());
java.setTaskName("proguard");
mojo.getLog().info("proguard jar: " + proguardJar);
java.createClasspath().setLocation(proguardJar);
// java.createClasspath().setPath(System.getProperty("java.class.path"));
java.setClassname(proguardMainClass);
java.setFailonerror(true);
java.setFork(true);
for (Iterator i = argsList.iterator(); i.hasNext();) {
String line = i.next().toString();
java.createArg().setValue(line);
mojo.getLog().debug("[" + line + "]");
}
int result = java.executeJava();
if (result != 0) {
throw new MojoExecutionException("Obfuscation failed (result=" + result + ")");
}
}
}