@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();
  }
  @NotNull
  @Override
  public TaskResult execute(@NotNull DeploymentTaskContext deploymentTaskContext)
      throws TaskException {

    buildLogger = deploymentTaskContext.getBuildLogger();
    ServerConfigManager serverConfigManager = ServerConfigManager.getInstance();
    String serverId =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + AbstractBuildContext.SERVER_ID_PARAM);
    if (StringUtils.isBlank(serverId)) {
      // Compatibility with version 1.8.0
      serverId = deploymentTaskContext.getConfigurationMap().get("artifactoryServerId");
    }
    ServerConfig serverConfig = serverConfigManager.getServerConfigById(Long.parseLong(serverId));
    if (serverConfig == null) {
      buildLogger.addErrorLogEntry(
          "Could not find Artifactpry server. Please check the Artifactory server in the task configuration.");
      return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
    }

    repositoryKey =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + ArtifactoryDeploymentConfiguration.DEPLOYMENT_REPOSITORY);
    if (StringUtils.isBlank(repositoryKey)) {
      // Compatibility with version 1.8.0
      repositoryKey =
          deploymentTaskContext
              .getConfigurationMap()
              .get(ArtifactoryDeploymentConfiguration.DEPLOYMENT_REPOSITORY);
    }
    artifactsRootDirectory = deploymentTaskContext.getRootDirectory().getAbsolutePath();

    // Get the deployer credentials configured in the task configuration
    String username =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + ArtifactoryDeploymentConfiguration.USERNAME);
    String password =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + ArtifactoryDeploymentConfiguration.PASSWORD);
    // If deployer credentials were not configured in the task configuration, use the credentials
    // configured
    // globally
    if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
      username = serverConfig.getUsername();
      password = serverConfig.getPassword();
    }
    TaskResult result;
    client =
        new ArtifactoryBuildInfoClient(
            serverConfig.getUrl(), username, password, new BuildInfoLog(log));

    try {
      RuntimeTaskDefinition artifactDownloadTask =
          TaskUtils.findDownloadArtifactsTask(
              deploymentTaskContext.getCommonContext().getRuntimeTaskDefinitions());
      FilesCollector filesCollector =
          new FilesCollector(artifactsRootDirectory, artifactDownloadTask);
      Map<String, Set<File>> artifacts = filesCollector.getCollectedFiles();
      Set<DeployDetails> deployDetailsSet = createDeploymentDetailsForArtifacts(artifacts);
      deploy(deployDetailsSet);
      result = TaskResultBuilder.newBuilder(deploymentTaskContext).success().build();
    } catch (Exception e) {
      buildLogger.addErrorLogEntry(
          "Error while deploying artifacts to Artifactory: " + e.getMessage());
      result = TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
    } finally {
      client.shutdown();
    }
    return result;
  }
  @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());
    }
  }