Пример #1
0
  List<URL> createMavenGavRecursiveURLs(String artifactGav, String... excludes)
      throws MalformedURLException, DependencyCollectionException, DependencyResolutionException {
    Artifact artifact = new DefaultArtifact(artifactGav);
    if (artifact.getVersion() == null) {
      throw new IllegalArgumentException("Null version");
    }

    VersionScheme versionScheme = new GenericVersionScheme();
    try {
      versionScheme.parseVersion(artifact.getVersion());
    } catch (InvalidVersionSpecificationException e) {
      throw new IllegalArgumentException(e);
    }

    try {
      versionScheme.parseVersionRange(artifact.getVersion());
      throw new IllegalArgumentException(
          artifact.getVersion() + " is a version range. A specific version is needed");
    } catch (InvalidVersionSpecificationException expected) {

    }

    RepositorySystemSession session = newRepositorySystemSession();

    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(artifact);
    for (RemoteRepository remoteRepo : remoteRepositories) {
      artifactRequest.addRepository(remoteRepo);
    }

    ArtifactResult artifactResult;
    try {
      artifactResult = REPOSITORY_SYSTEM.resolveArtifact(session, artifactRequest);
    } catch (ArtifactResolutionException e) {
      throw new RuntimeException(e);
    }

    List<URL> urls = new ArrayList<>();
    urls.add(artifactToUrl(artifactResult.getArtifact()));

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, "compile"));
    for (RemoteRepository remoteRepo : remoteRepositories) {
      collectRequest.addRepository(remoteRepo);
    }

    DependencyNode node = REPOSITORY_SYSTEM.collectDependencies(session, collectRequest).getRoot();
    DependencyFilter filter = new ExclusionsDependencyFilter(Arrays.asList(excludes));
    DependencyRequest dependencyRequest = new DependencyRequest(node, filter);

    REPOSITORY_SYSTEM.resolveDependencies(session, dependencyRequest);

    PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
    node.accept(nlg);
    for (Artifact cur : nlg.getArtifacts(false)) {
      urls.add(artifactToUrl(cur));
    }

    log.debug("--------------------");
    log.debug(nlg.getClassPath());
    log.debug("--------------------");

    return urls;
  }
  private Set<Artifact> processTransientDependencies(Dependency dependency, boolean sharedLibraries)
      throws MojoExecutionException {
    try {
      final Set<Artifact> artifacts = new LinkedHashSet<Artifact>();

      final CollectRequest collectRequest = new CollectRequest();

      collectRequest.setRoot(dependency);
      collectRequest.setRepositories(projectRepos);
      final DependencyNode node =
          repoSystem.collectDependencies(repoSession, collectRequest).getRoot();

      Collection<String> exclusionPatterns = new ArrayList<String>();
      if (dependency.getExclusions() != null && !dependency.getExclusions().isEmpty()) {
        for (Exclusion exclusion : dependency.getExclusions()) {
          exclusionPatterns.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
        }
      }

      final DependencyRequest dependencyRequest =
          new DependencyRequest(
              node,
              new AndDependencyFilter(
                  new ExclusionsDependencyFilter(exclusionPatterns),
                  new AndDependencyFilter(
                      new ScopeDependencyFilter(
                          Arrays.asList("compile", "runtime"), Arrays.asList("test")),
                      // Also exclude any optional dependencies
                      new DependencyFilter() {
                        @Override
                        public boolean accept(
                            DependencyNode dependencyNode, List<DependencyNode> dependencyNodes) {
                          return !dependencyNode.getDependency().isOptional();
                        }
                      })));

      repoSystem.resolveDependencies(repoSession, dependencyRequest);

      PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
      node.accept(nlg);

      final List<Dependency> dependencies = nlg.getDependencies(false);

      for (Dependency dep : dependencies) {
        final org.eclipse.aether.artifact.Artifact depAetherArtifact = dep.getArtifact();
        if (isNativeLibrary(sharedLibraries, depAetherArtifact.getExtension())) {
          final Artifact mavenArtifact =
              artifactFactory.createDependencyArtifact(
                  depAetherArtifact.getGroupId(),
                  depAetherArtifact.getArtifactId(),
                  VersionRange.createFromVersion(depAetherArtifact.getVersion()),
                  depAetherArtifact.getExtension(),
                  depAetherArtifact.getClassifier(),
                  dep.getScope());
          mavenArtifact.setFile(depAetherArtifact.getFile());
          artifacts.add(mavenArtifact);
        }
      }

      return artifacts;
    } catch (Exception e) {
      throw new MojoExecutionException("Error while processing transient dependencies", e);
    }
  }