Skip to content

Commit

Permalink
Merge remote branch 'mosabua/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
hugojosefson committed Jun 1, 2010
2 parents 65df284 + 4cadb3c commit c4cea3e
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 40 deletions.
Expand Up @@ -46,6 +46,11 @@
*/
public abstract class AbstractAndroidMojo extends AbstractMojo {

/**
* The file extension used for the android package file.
*/
protected static final String ANDROID_PACKAGE_EXTENSTION = ".apk";

/**
* The maven project.
*
Expand Down Expand Up @@ -305,6 +310,7 @@ public abstract class AbstractAndroidMojo extends AbstractMojo {
*/
protected boolean attachSources;


/**
* Accessor for the local repository.
* @return The local repository.
Expand Down
Expand Up @@ -200,7 +200,7 @@ private String writeEmulatorStartScriptWindows() throws IOException, MojoExecuti
// and others.
String command = assembleStartCommandLine();
String uniqueWindowTitle = "MavenAndroidPlugin-AVD" + parsedAvd;
writer.print("START " + uniqueWindowTitle + " " + command);
writer.print("START \"" + uniqueWindowTitle + "\" " + command);
writer.println();
writer.println("FOR /F \"tokens=2\" %%I in ('TASKLIST /NH /FI \"WINDOWTITLE eq " + uniqueWindowTitle + "\"' ) DO SET PID=%%I");
writer.println("ECHO %PID% > " + pidFileName);
Expand Down Expand Up @@ -281,8 +281,7 @@ protected void stopAndroidEmulator() throws MojoExecutionException {
private void stopEmulatorWindows(CommandExecutor executor, String pid) throws ExecutionException {
String stopCommand = "TASKKILL"; // this assumes that the command is on the path
List<String> commands = new ArrayList<String>();
commands.add("/PID");
commands.add(pid);
commands.add("/PID " + pid); // combined together into on so that apostrophes do not get mixed up
getLog().info(STOP_EMULATOR_MSG + pid);
executor.executeCommand(stopCommand, commands);
}
Expand Down
Expand Up @@ -94,7 +94,8 @@ protected void deployDependencies() throws MojoExecutionException {
}

protected void deployBuiltApk() throws MojoExecutionException {
File apkFile = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".apk");
File apkFile = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName()
+ ANDROID_PACKAGE_EXTENSTION);
deployApk(apkFile);
}

Expand Down
Expand Up @@ -2,26 +2,37 @@

import org.apache.maven.plugin.MojoExecutionException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Implementation for the zipaplign goal. Implements parsing parameters from pom or command line arguments and sets
* useful defaults as well.
*
* @author Manfred Moser
* @author Manfred Moser <manfred@simnpligility.com>
*/
public abstract class AbstractZipalignMojo extends AbstractAndroidMojo {

/**
* The zipalign command configuration to use. As soon as a zipalign goal is invoked the command will be executed
* unless the skip parameter is set. By default the input file is the apk produced by the build in target. The
* outputApk will use the postfix -aligned.apk.
* &lt;zipalign&gt;
* &lt;skip&gt;false&lt;/skip&gt;
* &lt;verbose&gt;true&lt;/verbose&gt;
* &lt;/emulator&gt;
* </pre>
* @parameter
*/
private Zipalign zipalign;

/**
* @see Zipalign#enabled
* @parameter expression="${android.zipalign.enabled}"
* @see Zipalign#skip
* @parameter expression="${android.zipalign.skip}"
* @readonly
*/
private Boolean zipalignEnabled;
private Boolean zipalignSkip;

/**
* @see Zipalign#verbose
Expand All @@ -44,16 +55,25 @@ public abstract class AbstractZipalignMojo extends AbstractAndroidMojo {
*/
private String zipalignOutputApk;

private Boolean parsedEnabled;
private Boolean parsedSkip;
private Boolean parsedVerbose;
private String parsedInputApk;
private String parsedOutputApk;
private String parsedOutputApk;


/** the apk file to be zipaligned. */
private File apkFile;
/** the output apk file for the zipalign process. */
private File alignedApkFile;

/**
* actually do the zipalign
* @throws MojoExecutionException
*/
protected void zipalign() throws MojoExecutionException {
parseParameters();
if (parsedEnabled)
{
if (parsedSkip) {
getLog().info("Skipping zipalign");
} else {
CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
executor.setLogger(this.getLog());

Expand All @@ -70,6 +90,8 @@ protected void zipalign() throws MojoExecutionException {
parameters.add(parsedOutputApk);

try {
getLog().info("Running command: " + command);
getLog().info("with parameters: " + parameters);
executor.executeCommand(command, parameters);
} catch (ExecutionException e) {
throw new MojoExecutionException("", e);
Expand All @@ -78,19 +100,21 @@ protected void zipalign() throws MojoExecutionException {
}

private void parseParameters() {
getLog().debug("Parsing parameters");
// <zipalign> exist in pom file
if (zipalign != null)
{
// <zipalign><enabled> exists in pom file
if (zipalign.isEnabled() != null)
// <zipalign><skip> exists in pom file
if (zipalign.isSkip() != null)
{
parsedEnabled= zipalign.isEnabled();
parsedSkip = zipalign.isSkip();
}
else
{
parsedEnabled = determineEnabled();
parsedSkip = determineSkip();
}
// <zipalign><options> exists in pom file

// <zipalign><verbose> exists in pom file
if (zipalign.isVerbose() != null)
{
parsedVerbose = zipalign.isVerbose();
Expand All @@ -99,6 +123,7 @@ private void parseParameters() {
{
parsedVerbose = determineVerbose();
}

// <zipalign><inputApk> exists in pom file
if (zipalign.getInputApk() != null)
{
Expand All @@ -108,6 +133,8 @@ private void parseParameters() {
{
parsedInputApk = determineInputApk();
}


// <zipalign><outputApk> exists in pom file
if (zipalign.getOutputApk() != null)
{
Expand All @@ -117,47 +144,84 @@ private void parseParameters() {
{
parsedOutputApk = determineOutputApk();
}

}
// commandline options
// command line options
else
{
parsedEnabled = determineEnabled();
parsedSkip = determineSkip();
parsedVerbose = determineVerbose();
parsedInputApk = determineInputApk();
parsedOutputApk = determineOutputApk();
}

getLog().debug("skip:" + parsedSkip);
getLog().debug("verbose:" + parsedVerbose);
getLog().debug("inputApk:" + parsedInputApk);
getLog().debug("outputApk:" + parsedOutputApk);
}

/**
* Get wait value for zipalign from command line option.
* @return if available return command line value otherwise return default value (5000).
* Get skip value for zipalign from command line option.
* @return if available return command line value otherwise return default false.
*/
private Boolean determineEnabled() {
private Boolean determineSkip() {
Boolean enabled;
if (zipalignEnabled != null)
if (zipalignSkip != null)
{
enabled = zipalignEnabled;
enabled = zipalignSkip;
}
else
{
getLog().debug("Using default for zipalign.skip=false");
enabled = Boolean.FALSE;
}
return enabled;
}

/**
* Get verbose value for zipalign from command line option.
* @return if available return command line value otherwise return default false.
*/
private Boolean determineVerbose() {
Boolean enabled;
if (zipalignEnabled != null)
if (zipalignVerbose != null)
{
enabled = zipalignEnabled;
enabled = zipalignVerbose;
}
else
{
getLog().debug("Using default for zipalign.verbose=false");
enabled = Boolean.FALSE;
}
return enabled;
}

/**
* Gets the apk file location from basedir/target/finalname.apk
* @return absolute path.
*/
private String getApkLocation() {
if (apkFile == null) apkFile = new File(project.getBuild().getDirectory(),
project.getBuild().getFinalName() + ANDROID_PACKAGE_EXTENSTION);
return apkFile.getAbsolutePath();
}

/**
* Gets the apk file location from basedir/target/finalname-aligned.apk. "-aligned" is the inserted string for the
* output file.
* @return absolute path.
*/
private String getAlignedApkLocation() {
if (alignedApkFile == null) alignedApkFile = new File(project.getBuild().getDirectory(),
project.getBuild().getFinalName() + "-aligned" + ANDROID_PACKAGE_EXTENSTION);
return alignedApkFile.getAbsolutePath();
}

/**
* Get inputApk value for zipalign from command line option.
* @return if available return command line value otherwise return default.
*/
private String determineInputApk() {
String inputApk;
if (zipalignInputApk != null)
Expand All @@ -166,11 +230,17 @@ private String determineInputApk() {
}
else
{
inputApk = ""; // what should we default to? pom.build.finalname or something?
String inputPath = getApkLocation();
getLog().debug("Using default for zipalign.inputApk: " + inputPath);
inputApk = inputPath;
}
return inputApk;
}

/**
* Get outputApk value for zipalign from command line option.
* @return if available return command line value otherwise return default.
*/
private String determineOutputApk() {
String outputApk;
if (zipalignOutputApk != null)
Expand All @@ -179,7 +249,9 @@ private String determineOutputApk() {
}
else
{
outputApk = ""; // what should we default to? pom.build.finalname or something? or some as input if possible
String outputPath = getAlignedApkLocation();
getLog().debug("Using default for zipalign.outputApk: " + outputPath);
outputApk = outputPath;
}
return outputApk;
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/jayway/maven/plugins/android/AndroidSdk.java
Expand Up @@ -33,14 +33,21 @@ public class AndroidSdk {
private final String platform;
private static final String PARAMETER_MESSAGE = "Please provide a proper Android SDK directory path as configuration parameter <sdk><path>...</path></sdk> in the plugin <configuration/>. As an alternative, you may add the parameter to commandline: -Dandroid.sdk.path=... or set environment variable " + AbstractAndroidMojo.ENV_ANDROID_HOME + ".";

private static final String SOURCE_PROPERTIES_FILENAME = "source.properties";

/** should really use multimap from google collections but for this one use case not worth including.. */
/**
* Maps from Platform to API Level and reverse. E.g. 2.2. to 8 and 8 to 2.2.
* should really use multimap from google collections but for this one use case not worth including..
*/
public static HashMap<String, String> installedPlatforms2ApiLevels;
public static HashMap<String, String> installedApiLevels2Platforms;

/** property file in each platform folder with details about platform. */
private static final String SOURCE_PROPERTIES_FILENAME = "source.properties";
/** property name for platform version in sdk source.properties file. */
private static final String PLATFORM_VERSION_PROPERTY = "Platform.Version";
/** property name for api level version in sdk source.properties file. */
private static final String API_LEVEL_PROPERTY = "AndroidVersion.ApiLevel";

/** folder name for the sdk sub folder that contains the different platform versions. */
private static final String PLATFORMS_FOLDER_NAME = "platforms";

public AndroidSdk(File path, String platformOrApiLevel) {
Expand All @@ -52,13 +59,13 @@ public AndroidSdk(File path, String platformOrApiLevel) {
// letting this through to preserve compatibility for now
} else if (!(installedApiLevels2Platforms.containsKey(platformOrApiLevel)
|| installedPlatforms2ApiLevels.containsKey(platformOrApiLevel))) {
throw new InvalidSdkException("Platform/API level " + platformOrApiLevel + " not available") ;
throw new InvalidSdkException("Invalid SDK: Platform/API level " + platformOrApiLevel + " not available.") ;
} else if (installedApiLevels2Platforms.containsKey(platformOrApiLevel)) {
this.platform = platformOrApiLevel;
} else if (installedPlatforms2ApiLevels.containsKey(platformOrApiLevel)) {
this.platform = installedPlatforms2ApiLevels.get(platformOrApiLevel);
} else {
throw new InvalidSdkException("Invalid platform: " + platformOrApiLevel);
throw new InvalidSdkException("Invalid SDK: Platform/API level " + platformOrApiLevel + " not available.");
}
}

Expand Down
Expand Up @@ -4,7 +4,7 @@
* Configuration for the Android Emulator. This class is only the definition of the parameters that are shadowed in
* {@link com.jayway.maven.plugins.android.AbstractAndroidMojo} and used there.
*
* @author Manfred Moser
* @author Manfred Moser <manfred@simnpligility.com>
*/
public class Emulator {

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/jayway/maven/plugins/android/Zipalign.java
Expand Up @@ -4,36 +4,42 @@
* Configuration for the zipalign command. This class is only the definition of the parameters that are shadowed in
* {@link com.jayway.maven.plugins.android.AbstractZipalignMojo} and used there.
*
* @author Manfred Moser
* @author Manfred Moser <manfred@simnpligility.com>
*/
public class Zipalign {

/**
* Skip the zipalign command if desired. Similar to test.skip for surefire plugin.
* @parameter
* @default=false
*/
private Boolean enabled;
private Boolean skip;

/**
* Activate verbose output of the zipalign command.
* @parameter
* @default=true
*/
private Boolean verbose;


/**
* The apk file to be zipaligned. Per default the file is taken from build directory (target normally) using the
* build final name as file name and apk as extension.
* @parameter
*/
private String inputApk;

/**
* The apk file produced by the zipalign process. Per default the file is placed into the build directory (target
* normally) using the build final name appended with "-aligned" as file name and apk as extension.
* @parameter
*/
private String outputApk;


public Boolean isEnabled() {
return enabled;
public Boolean isSkip() {
return skip;
}

public Boolean isVerbose() {
Expand Down

0 comments on commit c4cea3e

Please sign in to comment.