@Override
  @NotNull
  public TaskResult execute(@NotNull CommonTaskContext commonTaskContext) throws TaskException {

    final TaskResultBuilder taskResultBuilder = TaskResultBuilder.newBuilder(commonTaskContext);
    final TaskContext taskContext = Narrow.to(commonTaskContext, TaskContext.class);
    final BuildLogger buildLogger = taskContext.getBuildLogger();

    // move this crazy resolution out of here.
    resolveContext(taskContext, commonTaskContext);

    final String rubyRuntimeLabel =
        RubyBuildConfigurationPlugin.getRubyRuntime(getBuildDefinition());

    try {

      if (rubyRuntimeLabel == null) {
        throw new RuntimeLocatorException(
            "A ruby runtime has not been chosen for this plan.  Please see the miscellaneous tab to choose a plan-wide ruby runtime.");
      }

      final RubyLabel rubyLabel = RubyLabel.fromString(rubyRuntimeLabel);

      final ConfigurationMap config = commonTaskContext.getConfigurationMap();
      Map<String, String> envVars = buildEnvironment(rubyLabel, config);

      List<String> commandsList = buildCommandList(rubyLabel, config);

      ExternalProcess externalProcess =
          getProcessService()
              .createExternalProcess(
                  commonTaskContext,
                  new ExternalProcessBuilder()
                      .env(envVars)
                      .command(commandsList)
                      .workingDirectory(commonTaskContext.getWorkingDirectory()));

      externalProcess.execute();

      taskResultBuilder.checkReturnCode(externalProcess, 0);
    } catch (IllegalArgumentException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    } catch (PathNotFoundException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    } catch (RuntimeLocatorException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    }

    return taskResultBuilder.build();
  }
  private void resolveContext(TaskContext taskContext, CommonTaskContext commonTaskContext) {

    // This is a hack to ultimately resolve the BuildDefinition.  Unfortunately this is not well
    // encapsulated and the lack of
    //      information provided to the deploy task makes it difficult to navigate back to.
    if (taskContext != null) {
      // taskContext is null for deploy tasks
      setBuildContext(taskContext.getBuildContext().getParentBuildContext());
      setBuildDefinition(getBuildContext().getBuildDefinition());
    } else {

      // this is a deploy task
      final DeploymentTaskContext deploymentTaskContext =
          Narrow.to(commonTaskContext, DeploymentTaskContext.class);
      long environmentId = deploymentTaskContext.getDeploymentContext().getEnvironmentId();

      DeploymentProject deploymentProject =
          getDeploymentProjectService().getDeploymentProjectForEnvironment(environmentId);
      Plan plan = getPlanManager().getPlanByKey(deploymentProject.getPlanKey());
      setBuildDefinition(plan.getBuildDefinition());
    }
  }
  @Override
  @NotNull
  public TaskResult execute(@NotNull TaskContext context) throws TaskException {
    BuildLogger logger = getBuildLogger(context);
    final ErrorMemorisingInterceptor errorLines = new ErrorMemorisingInterceptor();
    logger.getInterceptorStack().add(errorLines);
    Map<String, String> combinedMap = Maps.newHashMap();
    combinedMap.putAll(context.getConfigurationMap());
    combinedMap.putAll(context.getBuildContext().getBuildDefinition().getCustomConfiguration());
    IvyBuildContext buildContext = new IvyBuildContext(combinedMap);
    File rootDirectory = context.getRootDirectory();
    long serverId = buildContext.getArtifactoryServerId();
    try {
      ivyDependenciesDir = extractIvyDependencies(serverId, rootDirectory, buildContext);
      log.info(logger.addBuildLogEntry("Ivy dependency directory found at: " + ivyDependenciesDir));
    } catch (IOException ioe) {
      ivyDependenciesDir = null;
      logger.addBuildLogEntry(
          new ErrorLogEntry(
              "Error occurred while preparing Artifactory Ivy Runner dependencies. Build Info support is "
                  + "disabled: "
                  + ioe.getMessage()));
      log.error(
          "Error occurred while preparing Artifactory Ivy Runner dependencies. "
              + "Build Info support is disabled.",
          ioe);
    }
    if (ivyDependenciesDir == null) {
      String message = "Ivy dependency directory not found.";
      logger.addErrorLogEntry(message);
      log.error(message);
    }
    String executable = getExecutable(buildContext);
    if (StringUtils.isBlank(executable)) {
      log.error(logger.addErrorLogEntry("Cannot find ivy executable"));
      return TaskResultBuilder.create(context).failed().build();
    }
    Map<String, String> globalEnv = environmentVariableAccessor.getEnvironment();
    Map<String, String> environment = Maps.newHashMap(globalEnv);
    if (StringUtils.isNotBlank(ivyDependenciesDir)) {
      ArtifactoryBuildInfoPropertyHelper propertyHelper = new IvyPropertyHelper();
      propertyHelper.init(context.getBuildContext());
      buildInfoPropertiesFile =
          propertyHelper.createFileAndGetPath(
              buildContext,
              context.getBuildLogger(),
              environmentVariableAccessor.getEnvironment(context),
              globalEnv);
      if (StringUtils.isNotBlank(buildInfoPropertiesFile)) {
        activateBuildInfoRecording = true;
        environment.put(BuildInfoConfigProperties.PROP_PROPS_FILE, buildInfoPropertiesFile);
      }
    }
    List<String> command = Lists.newArrayList(executable);
    if (activateBuildInfoRecording) {
      command.add("-lib");
      command.add(Commandline.quoteArgument(ivyDependenciesDir));
      command.add("-listener");
      command.add(
          Commandline.quoteArgument("org.jfrog.build.extractor.listener.ArtifactoryBuildListener"));
    }
    String buildFile = buildContext.getBuildFile();
    if (StringUtils.isNotBlank(buildFile)) {
      command.addAll(Arrays.asList("-f", buildFile));
    }
    String targets = buildContext.getTargets();
    if (StringUtils.isNotBlank(targets)) {
      String[] targetTokens = StringUtils.split(targets, ' ');
      command.addAll(Arrays.asList(targetTokens));
    }

    String antOpts = buildContext.getAntOpts();
    if (StringUtils.isNotBlank(antOpts)) {
      environment.put("ANT_OPTS", antOpts);
    }
    if (StringUtils.isNotBlank(buildContext.getEnvironmentVariables())) {
      environment.putAll(
          environmentVariableAccessor.splitEnvironmentAssignments(
              buildContext.getEnvironmentVariables(), false));
    }
    String subDirectory = buildContext.getWorkingSubDirectory();
    if (StringUtils.isNotBlank(subDirectory)) {
      rootDirectory = new File(rootDirectory, subDirectory);
    }
    ExternalProcessBuilder processBuilder =
        new ExternalProcessBuilder()
            .workingDirectory(rootDirectory)
            .command(command)
            .env(environment);
    try {
      return TaskResultBuilder.create(context)
          .checkReturnCode(processService.executeProcess(context, processBuilder))
          .build();
    } finally {
      context.getBuildContext().getBuildResult().addBuildErrors(errorLines.getErrorStringList());
    }
  }