@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { EnvVars env = build.getEnvironment(listener); FilePath workDir = build.getModuleRoot(); ArgumentListBuilder cmdLine = buildMavenCmdLine(build, listener, env); StringBuilder javaPathBuilder = new StringBuilder(); JDK configuredJdk = build.getProject().getJDK(); if (configuredJdk != null) { javaPathBuilder .append(build.getProject().getJDK().getBinDir().getCanonicalPath()) .append(File.separator); } javaPathBuilder.append("java"); if (!launcher.isUnix()) { javaPathBuilder.append(".exe"); } String[] cmds = cmdLine.toCommandArray(); try { // listener.getLogger().println("Executing: " + cmdLine.toStringWithQuote()); int exitValue = launcher .launch() .cmds(new File(javaPathBuilder.toString()), cmds) .envs(env) .stdout(listener) .pwd(workDir) .join(); boolean success = (exitValue == 0); build.setResult(success ? Result.SUCCESS : Result.FAILURE); return success; } catch (IOException e) { Util.displayIOException(e, listener); e.printStackTrace(listener.fatalError("command execution failed")); build.setResult(Result.FAILURE); return false; } }
@Override public BuildWrapper.Environment setUp( AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { final PrintStream logger = listener.getLogger(); final DeviceFarmApi api = new DeviceFarmApiImpl(); long start = System.currentTimeMillis(); try { EnvVars environment = build.getEnvironment(listener); String expendedTag = environment.expand(tag); log(logger, Messages.TRYING_TO_CONNECT_API_SERVER(deviceApiUrl, expendedTag)); api.connectApiServer( logger, deviceApiUrl, expendedTag, build.getProject().getAbsoluteUrl() + build.getNumber()); final RemoteDevice reserved = api.waitApiResponse( logger, DEVICE_WAIT_TIMEOUT_IN_MILLIS, DEVICE_READY_CHECK_INTERVAL_IN_MS); log( logger, Messages.DEVICE_IS_READY(passedSeconds(start), reserved.ip, reserved.port, reserved.url)); if (descriptor == null) { descriptor = Hudson.getInstance().getDescriptorByType(DescriptorImpl.class); } // Substitute environment and build variables into config final String androidHome = discoverAndroidSdkHome(build, launcher, listener); log(logger, Messages.USING_SDK(androidHome)); AndroidSdk sdk = new AndroidSdk(androidHome, androidHome); final AndroidDeviceContext device = new AndroidDeviceContext(build, launcher, listener, sdk, reserved.ip, reserved.port); // disconnect first to workaround previous error device.disconnect(); // connect device with adb device.connect(DEVICE_CONNECT_TIMEOUT_IN_MILLIS); device.waitDeviceReady(logger, DEVICE_CONNECT_TIMEOUT_IN_MILLIS, 1000); // check availability device.devices(); // unlock screen device.unlockScreen(); // Start dumping logcat to temporary file final LogcatCollector logcatCollector = new LogcatCollector(build, device); logcatCollector.start(); return new BuildWrapper.Environment() { @Override public void buildEnvVars(Map<String, String> env) { env.put("ANDROID_IP", device.ip()); env.put("ANDROID_HOME", androidHome); env.put("ANDROID_SDK_HOME", androidHome); env.put("ANDROID_PORT", Integer.toString(device.port())); env.put("ANDROID_SERIAL", device.serial()); } @Override public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException { cleanUp(build, device, api, logcatCollector); return true; } }; } catch (FailedToConnectApiServerException e) { log(logger, Messages.FAILED_TO_CONNECT_API_SERVER()); } catch (MalformedResponseException e) { log(logger, Messages.FAILED_TO_PARSE_DEVICE_FARM_RESPONSE()); } catch (TimeoutException e) { log(logger, Messages.DEVICE_WAIT_TIMEOUT(passedSeconds(start))); } catch (NoDeviceAvailableException e) { log(logger, Messages.NO_SUCH_DEVICE()); } build.setResult(Result.NOT_BUILT); cleanUp(null, null, api, null); return null; }