Пример #1
0
  protected MavenArtifactRepository addRepository(Project project, final String url) {

    RepositoryHandler repositoryHandler = project.getRepositories();

    return repositoryHandler.maven(
        new Action<MavenArtifactRepository>() {

          @Override
          public void execute(MavenArtifactRepository mavenArtifactRepository) {

            mavenArtifactRepository.setUrl(url);
          }
        });
  }
  public void applyPlugins(
      Iterable<? extends PluginRequest> requests,
      final ScriptHandler scriptHandler,
      final PluginAware target,
      ClassLoaderScope classLoaderScope) {
    final PluginResolutionApplicator resolutionApplicator = new PluginResolutionApplicator(target);

    List<Result> results =
        collect(
            requests,
            new Transformer<Result, PluginRequest>() {
              public Result transform(PluginRequest request) {
                return resolveToFoundResult(request);
              }
            });

    ImmutableListMultimap<Boolean, Result> categorizedResults =
        groupBy(
            results,
            new Transformer<Boolean, Result>() {
              public Boolean transform(Result original) {
                return original.legacyFound != null;
              }
            });

    final List<Result> legacy = categorizedResults.get(true);
    List<Result> nonLegacy = categorizedResults.get(false);

    // Could be different to ids in the requests as they may be unqualified
    final Map<Result, String> legacyActualPluginIds = Maps.newLinkedHashMap();
    if (!legacy.isEmpty()) {
      final RepositoryHandler repositories = scriptHandler.getRepositories();
      final List<MavenArtifactRepository> mavenRepos =
          repositories.withType(MavenArtifactRepository.class);

      for (final Result result : legacy) {
        result.legacyFound.action.execute(
            new LegacyPluginResolveContext() {
              public Dependency add(
                  String pluginId, final String m2RepoUrl, Object dependencyNotation) {
                legacyActualPluginIds.put(result, pluginId);

                boolean repoExists =
                    any(
                        mavenRepos,
                        new Spec<MavenArtifactRepository>() {
                          public boolean isSatisfiedBy(MavenArtifactRepository element) {
                            return element.getUrl().toString().equals(m2RepoUrl);
                          }
                        });
                if (!repoExists) {
                  repositories.maven(
                      new Action<MavenArtifactRepository>() {
                        public void execute(MavenArtifactRepository mavenArtifactRepository) {
                          mavenArtifactRepository.setUrl(m2RepoUrl);
                        }
                      });
                }

                return scriptHandler
                    .getDependencies()
                    .add(ScriptHandler.CLASSPATH_CONFIGURATION, dependencyNotation);
              }
            });
      }
    }

    Configuration classpathConfiguration =
        scriptHandler.getConfigurations().getByName(ScriptHandler.CLASSPATH_CONFIGURATION);
    Set<File> files = classpathConfiguration.getFiles();
    if (!files.isEmpty()) {
      ClassPath classPath = new DefaultClassPath(files);
      Factory<? extends ClassLoader> loader = classLoaderScope.getParent().loader(classPath);
      classLoaderScope.export(loader);
    }

    classLoaderScope.lock();

    // We're making an assumption here that the target's plugin registry is backed classLoaderScope.
    // Because we are only build.gradle files right now, this holds.
    // It won't for arbitrary scripts though.
    for (final Map.Entry<Result, String> entry : legacyActualPluginIds.entrySet()) {
      final PluginRequest request = entry.getKey().request;
      final String id = entry.getValue();
      applyPlugin(
          request,
          id,
          new Runnable() {
            public void run() {
              target.getPlugins().apply(id);
            }
          });
    }

    for (final Result result : nonLegacy) {
      applyPlugin(
          result.request,
          result.found.resolution.getPluginId().toString(),
          new Runnable() {
            public void run() {
              resolutionApplicator.execute(result.found.resolution);
            }
          });
    }
  }