net.java.truelicense.maven.plugin.ObfuscateMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truelicense-maven-plugin Show documentation
Show all versions of truelicense-maven-plugin Show documentation
Provides a Maven plugin with goals for the obfuscation of constant
string values in Java class and test-class files (byte code).
/*
* Copyright (C) 2005-2013 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.maven.plugin;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TVFS;
import java.io.*;
import static java.io.File.*;
import java.util.Formatter;
import javax.annotation.concurrent.NotThreadSafe;
import net.java.truelicense.maven.plugin.obfuscation.Processor;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
/**
* A basic MOJO for the obfuscation of constant string values in Java class
* files (byte code).
*
* @author Christian Schlichtherle
*/
@NotThreadSafe
public abstract class ObfuscateMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true)
private File directory;
/**
* Whether or not the class files shall get backed up to a JAR file before
* processing them.
*/
@Parameter(property = "truelicense.obfuscate.backup", defaultValue = "false")
private boolean backup;
/**
* The prefix of the backup JAR file with the original class files.
*/
@Parameter(property = "truelicense.obfuscate.backupPrefix", defaultValue = "classes")
private String backupPrefix;
/**
* The extension of the backup directory with the original class files:
* Use "zip"
to create a ZIP file as the backup directory.
*/
@Parameter(property = "truelicense.obfuscate.backupExtension", defaultValue = "zip")
private String backupExtension;
/** The maximum allowed size of a class file in bytes. */
@Parameter(property = "truelicense.obfuscate.maxBytes", defaultValue = "" + Processor.DEFAULT_MAX_BYTES)
private int maxBytes;
/**
* The scope of constant string value obfuscation:
* One of "none"
, "annotated"
or
* "all"
.
*
* - Use
"none"
to skip obfuscation.
* - Use
"annotated"
to obfuscate only constant string
* values of fields which have been annotated with @Obfuscate.
* - Use
"all"
to obfuscate all constant string values.
* This may result in quite some code size and runtime overhead,
* so you should not use this in general.
*
*/
@Parameter(property = "truelicense.obfuscate.scope", defaultValue = "annotated")
private Scope scope;
/**
* The methodName for synthesized method names.
* This a methodName string for the class {@link Formatter}.
* It's first parameter is a string identifier for the obfuscation stage
* and its second parameter is an integer index for the synthesized method.
*/
@Parameter( property = "truelicense.obfuscate.methodNameFormat",
defaultValue = Processor.DEFAULT_METHOD_NAME_FORMAT)
private String methodNameFormat;
/**
* Whether or not a call to java.lang.String.intern()
* shall get added when computing the original constant string values again.
* Use this to preserve the identity relation of constant string values if
* required.
*/
@Parameter( property = "truelicense.obfuscate.intern",
defaultValue = "true")
private boolean intern;
private CheckedLog checkedLog;
@Override
public void setLog(final Log log) {
super.setLog(log);
this.checkedLog = null;
}
private CheckedLog checkedLog() {
final CheckedLog checkedLog = this.checkedLog;
return null != checkedLog
? checkedLog
: (this.checkedLog = new CheckedLog(super.getLog()));
}
abstract TFile outputDirectory();
@Override
public void execute()
throws MojoExecutionException, MojoFailureException {
try {
if (scope == Scope.none) {
checkedLog().warn("Skipping constant string value obfuscation.");
} else {
try {
try {
backup();
obfuscate();
} finally {
TVFS.umount();
}
} catch (final RuntimeException ex) {
throw new MojoExecutionException(ex.toString(), ex);
} catch (final IOException ex) {
throw new MojoFailureException(ex.toString(), ex);
}
}
} catch (final MojoExecutionException ex) {
checkedLog().error(ex.getLocalizedMessage(), ex);
throw ex;
} catch (final MojoFailureException ex) {
checkedLog().error(ex.getLocalizedMessage(), ex);
throw ex;
}
checkedLog().check();
}
private void backup() throws IOException {
if (!backup) return;
final TFile backupDir = new TFile(createTempFile(
backupPrefix + ".",
"." + backupExtension,
directory));
checkedLog().info(backupDir + ": Creating class file backup directory.");
outputDirectory().cp_rp(backupDir.rm_r());
}
private void obfuscate() {
new Processor(
new MojoLogger(checkedLog()),
outputDirectory(),
maxBytes,
scope == Scope.all,
methodNameFormat,
intern).run();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy