private String buildNotFoundMessage(PluginRequest pluginRequest, Result result) {
    if (result.notFoundList.isEmpty()) {
      // this shouldn't happen, resolvers should call notFound()
      return String.format("Plugin %s was not found", pluginRequest.getDisplayName());
    } else {
      StringBuilder sb =
          new StringBuilder("Plugin ")
              .append(pluginRequest.getDisplayName())
              .append(" was not found in any of the following sources:\n");

      for (NotFound notFound : result.notFoundList) {
        sb.append('\n').append("- ").append(notFound.source);
        if (notFound.detail != null) {
          sb.append(" (").append(notFound.detail).append(")");
        }
      }

      return sb.toString();
    }
  }
 private void applyPlugin(PluginRequest request, String id, Runnable applicator) {
   try {
     try {
       applicator.run();
     } catch (UnknownPluginException e) {
       throw new InvalidPluginException(
           String.format(
               "Could not apply requested plugin %s as it does not provide a plugin with id '%s'."
                   + " This is caused by an incorrect plugin implementation."
                   + " Please contact the plugin author(s).",
               request, id),
           e);
     } catch (Exception e) {
       throw new InvalidPluginException(
           String.format("An exception occurred applying plugin request %s", request), e);
     }
   } catch (Exception e) {
     throw new LocationAwareException(e, request.getScriptSource(), request.getLineNumber());
   }
 }
  private Result resolveToFoundResult(PluginRequest request) {
    Result result = new Result(request);
    try {
      pluginResolver.resolve(request, result);
    } catch (Exception e) {
      throw new LocationAwareException(
          new GradleException(
              String.format("Error resolving plugin %s.", request.getDisplayName()), e),
          request.getScriptSource(),
          request.getLineNumber());
    }

    if (!result.isFound()) {
      String message = buildNotFoundMessage(request, result);
      Exception exception = new UnknownPluginException(message);
      throw new LocationAwareException(
          exception, request.getScriptSource(), request.getLineNumber());
    }

    return result;
  }