com.pyx4me.maven.j2me.ProGuardMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2me-maven-plugin Show documentation
Show all versions of j2me-maven-plugin Show documentation
Maven 2 Plugin to make j2me project
/**
* Pyx4me framework
* Copyright (C) 2006-2007 pyx4.com.
*
* @author vlads
* @version $Id: ProGuardMojo.java 107 2007-04-21 03:10:01Z 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 standalone obfuscation task
*
*
* @goal obfuscate
* @phase package
* @description Create JAD files
*
*/
public class ProGuardMojo extends WtkJadMojo {
/**
* Recursively reads configuration options from the given file filename
*
* @parameter default-value="${basedir}/proguard.conf"
*/
private File proguardInclude;
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), proguardInclude, obfuscate, this, false, 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 proguardInclude, boolean obfuscate,
AbstractJadWtkMojo mojo, boolean isTest, File proguardTestInclude) throws MojoExecutionException,
MojoFailureException {
ArrayList args = new ArrayList();
File 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");
args.add(fileToString(baseFile));
args.add("-outjars");
args.add(fileToString(jarFile));
if (!obfuscate) {
args.add("-dontobfuscate");
}
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 mdip 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 dependancy = mojo.mavenProject.getCompileArtifacts();
for (Iterator i = dependancy.iterator(); i.hasNext();) {
Artifact artifact = (Artifact) i.next();
if ((Artifact.SCOPE_PROVIDED.equals(artifact.getScope()))
|| (Artifact.SCOPE_SYSTEM.equals(artifact.getScope()))) {
args.add("-libraryjars");
args.add(fileToString(artifact.getFile()));
}
}
}
// 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_map.txt").getAbsoluteFile())));
args.add("-printseeds");
args.add(fileToString((new File(mojo.outputDirectory, "proguard_seeds.txt").getAbsoluteFile())));
if (mojo.getLog().isDebugEnabled()) {
args.add("-verbose");
}
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;
// This should be solved in Maven 2.1
for(Iterator i = mojo.pluginArtifacts.iterator(); i.hasNext(); ) {
Artifact artifact = (Artifact)i.next();
if ("proguard".equals(artifact.getArtifactId())) {
if (isVersionGrate(artifact, proguardArtifact)) {
proguardArtifact = artifact;
}
}
mojo.getLog().debug("pluginArtifact: " + artifact.getFile());
}
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 + ")");
}
}
}