com.jayway.maven.plugins.android.AbstractEmulatorMojo Maven / Gradle / Ivy
/*
* Copyright (C) 2009, 2010 Jayway AB
* Copyright (C) 2007-2008 JVending Masa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.maven.plugins.android;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* AbstractEmulatorMojo contains all code related to the interaction with the Android emulator. At this stage that is
* starting and stopping the emulator.
*
* @author Manfred Moser
* @see com.jayway.maven.plugins.android.Emulator
* @see com.jayway.maven.plugins.android.standalonemojos.EmulatorStartMojo
* @see com.jayway.maven.plugins.android.standalonemojos.EmulatorStopMojo
*/
public abstract class AbstractEmulatorMojo extends AbstractAndroidMojo {
/** operating system name. */
public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
/**
* The Android emulator configuration to use. All values are optional.
* <emulator>
* <avd>Default</avd>
* <wait>20000</wait>
* <options>-no-skin</options>
* </emulator>
*
* @parameter
*/
private Emulator emulator;
/**
* Name of the Android Virtual Device (emulatorAvd) that will be started by the emulator. Default value is "Default"
* @see Emulator#avd
* @parameter expression="${android.emulator.avd}"
* @readonly
*/
private String emulatorAvd;
/**
* Wait time for the emulator start up.
* @see Emulator#wait
* @parameter expression="${android.emulator.wait}"
* @readonly
*/
private String emulatorWait;
/**
* Additional command line options for the emulator start up. This option can be used to pass any additional
* options desired to the invocation of the emulator. Use emulator -help for more details. An example would be
* "-no-skin".
* @see Emulator#options
* @parameter expression="${android.emulator.options}"
* @readonly
*/
private String emulatorOptions;
/** parsed value for avd that will be used for the invocation. */
private String parsedAvd;
/** parsed value for options that will be used for the invocation. */
private String parsedOptions;
/** parsed value for wait that will be used for the invocation. */
private String parsedWait;
private static final String STOP_EMULATOR_MSG = "Stopping android emulator with pid: ";
private static final String START_EMULATOR_MSG = "Starting android emulator with script: ";
private static final String START_EMULATOR_WAIT_MSG = "Waiting for emulator start:";
private static final String NO_EMULATOR_RUNNING = "unknown";
private static final String NO_DEMON_RUNNING_MACOSX = "* daemon not running";
/** Folder that contains the startup script and the pid file. */
private static final String scriptFolder = System.getProperty("java.io.tmpdir");
/** file name for the pid file. */
private static final String pidFileName = scriptFolder + "/maven-android-plugin-emulator.pid";
/**
* Are we running on a flavour of Windows.
* @return
*/
private boolean isWindows() {
boolean result;
if (OS_NAME.toLowerCase().contains("windows")) {
result = true;
}
else
{
result = false;
}
getLog().debug("isWindows: " + result);
return result;
}
/**
* Start the Android Emulator with the specified options.
* @see #emulatorAvd
* @see #emulatorWait
* @see #emulatorOptions
* @throws org.apache.maven.plugin.MojoExecutionException
*/
protected void startAndroidEmulator() throws MojoExecutionException
{
parseParameters();
CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
executor.setLogger(this.getLog());
try {
String filename;
if (isWindows())
{
filename = writeEmulatorStartScriptWindows();
}
else
{
filename = writeEmulatorStartScriptUnix();
}
String emulatorName = getRunningEmulatorName();
// normally only #NO_EMULATOR_RUNNING is returned
// however when starting emulator within intellij on macosx with launchd configuration the first time
// #NO_DEMON_RUNNING_MACOSX is the start of the first line but needs to be treated the same
if (emulatorName.equals(NO_EMULATOR_RUNNING) || emulatorName.startsWith(NO_DEMON_RUNNING_MACOSX))
{
getLog().info(START_EMULATOR_MSG + filename);
executor.executeCommand(filename, null);
getLog().info(START_EMULATOR_WAIT_MSG + parsedWait);
// wait for the emulator to start up
Thread.sleep(new Long(parsedWait));
}
else
{
getLog().info("Emulator " + emulatorName + " already running. Skipping start and wait.");
}
} catch (Exception e) {
throw new MojoExecutionException("", e);
}
}
/**
* Get the name of the running emulator.
* @return emulator name or "unknown" if none found running with adb tool.
* @throws MojoExecutionException
* @throws ExecutionException
* @see #NO_EMULATOR_RUNNING
*/
private String getRunningEmulatorName() throws MojoExecutionException, ExecutionException {
CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
executor.setLogger(this.getLog());
List© 2015 - 2024 Weber Informatics LLC | Privacy Policy