private Set<Artifact> processTransitiveDependencies(
      Dependency dependency, boolean sharedLibraries) throws MojoExecutionException {
    log.debug("Processing transitive dependencies for : " + dependency);

    try {
      final Set<Artifact> artifacts = new LinkedHashSet<Artifact>();

      final List<String> exclusionPatterns = new ArrayList<String>();
      if (dependency.getExclusions() != null && !dependency.getExclusions().isEmpty()) {
        for (final Exclusion exclusion : dependency.getExclusions()) {
          exclusionPatterns.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
        }
      }
      final ArtifactFilter optionalFilter =
          new ArtifactFilter() {
            @Override
            public boolean include(Artifact artifact) {
              return !artifact.isOptional();
            }
          };

      final AndArtifactFilter filter = new AndArtifactFilter();
      filter.add(new ExcludesArtifactFilter(exclusionPatterns));
      filter.add(
          new OrArtifactFilter(
              Arrays.<ArtifactFilter>asList(
                  new ScopeArtifactFilter("compile"),
                  new ScopeArtifactFilter("runtime"),
                  new ScopeArtifactFilter("test"))));
      filter.add(optionalFilter);

      final DependencyNode node = dependencyGraphBuilder.buildDependencyGraph(project, filter);
      final CollectingDependencyNodeVisitor collectingVisitor =
          new CollectingDependencyNodeVisitor();
      node.accept(collectingVisitor);

      final List<DependencyNode> dependencies = collectingVisitor.getNodes();
      for (final DependencyNode dep : dependencies) {
        final boolean isNativeLibrary =
            isNativeLibrary(sharedLibraries, dep.getArtifact().getType());
        log.debug("Processing library : " + dep.getArtifact() + " isNative=" + isNativeLibrary);
        if (isNativeLibrary) {
          artifacts.add(dep.getArtifact());
        }
      }

      return artifacts;
    } catch (Exception e) {
      throw new MojoExecutionException("Error while processing transitive dependencies", e);
    }
  }
示例#2
0
  public List<String> getDependencies() {
    List<Dependency> deps = pom.getDependencies();
    if (deps == null) return null;

    final List<String> dependencies = new ArrayList<String>();
    for (Dependency dep : deps) {
      if (!dep.isOptional()) {
        String coords =
            dep.getGroupId()
                + ":"
                + dep.getArtifactId()
                + ":"
                + dep.getVersion()
                + (dep.getClassifier() != null && !dep.getClassifier().isEmpty()
                    ? ":" + dep.getClassifier()
                    : "");
        List<Exclusion> exclusions = dep.getExclusions();
        if (exclusions != null && !exclusions.isEmpty()) {
          StringBuilder sb = new StringBuilder();
          sb.append('(');
          for (Exclusion ex : exclusions)
            sb.append(ex.getGroupId()).append(':').append(ex.getArtifactId()).append(',');
          sb.delete(sb.length() - 1, sb.length());
          sb.append(')');
          coords += sb.toString();
        }
        dependencies.add(coords);
      }
    }
    return dependencies;
  }
 private void handleExclusions(Artifact artifact, Dependency dependency) {
   List<String> exclusions = new ArrayList<String>();
   for (Exclusion exclusion : dependency.getExclusions()) {
     exclusions.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
   }
   ArtifactFilter filter = new ExcludesArtifactFilter(exclusions);
   artifact.setDependencyFilter(filter);
 }
示例#4
0
 /**
  * Convert dependency to root artifact.
  *
  * @param dep Dependency
  * @return Root artifact
  */
 private RootArtifact root(final Dependency dep) {
   return new RootArtifact(
       this.aether,
       new DefaultArtifact(
           dep.getGroupId(),
           dep.getArtifactId(),
           dep.getClassifier(),
           dep.getType(),
           dep.getVersion()),
       dep.getExclusions());
 }
示例#5
0
  // generic method to retrieve all the transitive dependencies
  private Collection<Artifact> getAllDependencies() throws MojoExecutionException {
    List<Artifact> artifacts = new ArrayList<Artifact>();

    for (Iterator<?> dependencies = project.getDependencies().iterator();
        dependencies.hasNext(); ) {
      Dependency dependency = (Dependency) dependencies.next();

      String groupId = dependency.getGroupId();
      String artifactId = dependency.getArtifactId();

      VersionRange versionRange;
      try {
        versionRange = VersionRange.createFromVersionSpec(dependency.getVersion());
      } catch (InvalidVersionSpecificationException e) {
        throw new MojoExecutionException("unable to parse version", e);
      }

      String type = dependency.getType();
      if (type == null) {
        type = "jar";
      }
      String classifier = dependency.getClassifier();
      boolean optional = dependency.isOptional();
      String scope = dependency.getScope();
      if (scope == null) {
        scope = Artifact.SCOPE_COMPILE;
      }

      Artifact art =
          this.artifactFactory.createDependencyArtifact(
              groupId, artifactId, versionRange, type, classifier, scope, null, optional);

      if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
        art.setFile(new File(dependency.getSystemPath()));
      }

      List<String> exclusions = new ArrayList<String>();
      for (Iterator<?> j = dependency.getExclusions().iterator(); j.hasNext(); ) {
        Exclusion e = (Exclusion) j.next();
        exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
      }

      ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

      art.setDependencyFilter(newFilter);

      artifacts.add(art);
    }

    return artifacts;
  }
示例#6
0
  // DefaultProjectBuilder
  public Artifact createDependencyArtifact(Dependency d) {
    if (d.getVersion() == null) {
      return null;
    }

    VersionRange versionRange;
    try {
      versionRange = VersionRange.createFromVersionSpec(d.getVersion());
    } catch (InvalidVersionSpecificationException e) {
      return null;
    }

    Artifact artifact =
        XcreateDependencyArtifact(
            d.getGroupId(),
            d.getArtifactId(),
            versionRange,
            d.getType(),
            d.getClassifier(),
            d.getScope(),
            d.isOptional());

    if (Artifact.SCOPE_SYSTEM.equals(d.getScope()) && d.getSystemPath() != null) {
      artifact.setFile(new File(d.getSystemPath()));
    }

    if (!d.getExclusions().isEmpty()) {
      List<String> exclusions = new ArrayList<>();

      for (Exclusion exclusion : d.getExclusions()) {
        exclusions.add(exclusion.getGroupId() + ':' + exclusion.getArtifactId());
      }

      artifact.setDependencyFilter(new ExcludesArtifactFilter(exclusions));
    }

    return artifact;
  }
示例#7
0
 private Dependency toAetherDependency(org.apache.maven.model.Dependency dependency) {
   Artifact artifact =
       new DefaultArtifact(
           dependency.getGroupId(),
           dependency.getArtifactId(),
           dependency.getClassifier(),
           dependency.getType(),
           dependency.getVersion());
   ImmutableList.Builder<Exclusion> exclusions = ImmutableList.builder();
   for (org.apache.maven.model.Exclusion exclusion : dependency.getExclusions()) {
     exclusions.add(new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), null, "*"));
   }
   return new Dependency(
       artifact, dependency.getScope(), dependency.isOptional(), exclusions.build());
 }
  private DependencyInfo getDependencyInfo(Dependency dependency) {
    DependencyInfo info = new DependencyInfo();

    // dependency data
    info.setGroupId(dependency.getGroupId());
    info.setArtifactId(dependency.getArtifactId());
    info.setVersion(dependency.getVersion());
    info.setScope(dependency.getScope());
    info.setClassifier(dependency.getClassifier());
    info.setOptional(dependency.isOptional());
    info.setType(dependency.getType());
    info.setSystemPath(dependency.getSystemPath());

    // exclusions
    Collection<ExclusionInfo> exclusions = info.getExclusions();
    final List<Exclusion> mavenExclusions = dependency.getExclusions();
    for (Exclusion exclusion : mavenExclusions) {
      exclusions.add(new ExclusionInfo(exclusion.getGroupId(), exclusion.getArtifactId()));
    }

    return info;
  }
  private File generateRubyStub(
      final GemSpecification gemspec, final MavenArtifact artifact, final RubyDependencyType type)
      throws IOException {
    final VelocityContext context = new VelocityContext();
    switch (type) {
      case RUNTIME:
        if (artifact.getArtifactFile() != null) {
          context.put(
              "jarfile",
              createJarfileName(
                  artifact.getCoordinates().getGroupId(),
                  artifact.getCoordinates().getArtifactId(),
                  artifact.getCoordinates().getVersion()));
        }
        break;
      case DEVELOPMENT:
        context.put("filename", artifact.getCoordinates().getArtifactId() + ".rb");
        break;
    }
    final List<MavenDependency> deps = new ArrayList<MavenDependency>();
    for (final Dependency dependency : artifact.getPom().getDependencies()) {
      if (RubyDependencyType.toRubyDependencyType(dependency.getScope()) == type
          && !dependency.isOptional()) {
        final MavenDependency mavenDependency = new MavenDependency();
        mavenDependency.name =
            createRequireName(
                dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        for (final Exclusion exclusion : dependency.getExclusions()) {
          mavenDependency.exclusions.add(exclusion.getGroupId() + "/" + exclusion.getArtifactId());
        }
        deps.add(mavenDependency);
      }
    }
    context.put("dependencies", deps);

    return generateRubyFile("require" + type.name(), context, "rubyStub" + type.name());
  }
  /**
   * Returns the list of project artifacts. Also artifacts generated from referenced projects will
   * be added, but with the <code>resolved</code> property set to true.
   *
   * @return list of projects artifacts
   * @throws MojoExecutionException if unable to parse dependency versions
   */
  private Set getProjectArtifacts() throws MojoExecutionException {
    // keep it sorted, this should avoid random classpath order in tests
    Set artifacts = new TreeSet();

    for (Iterator dependencies = getProject().getDependencies().iterator();
        dependencies.hasNext(); ) {
      Dependency dependency = (Dependency) dependencies.next();

      String groupId = dependency.getGroupId();
      String artifactId = dependency.getArtifactId();
      VersionRange versionRange;
      try {
        versionRange = VersionRange.createFromVersionSpec(dependency.getVersion());
      } catch (InvalidVersionSpecificationException e) {
        throw new MojoExecutionException(
            Messages.getString(
                "unabletoparseversion",
                new Object[] { // $NON-NLS-1$
                  dependency.getArtifactId(),
                  dependency.getVersion(),
                  dependency.getManagementKey(),
                  e.getMessage()
                }),
            e);
      }

      String type = dependency.getType();
      if (type == null) {
        type = "jar"; // $NON-NLS-1$
      }
      String classifier = dependency.getClassifier();
      boolean optional = dependency.isOptional();
      String scope = dependency.getScope();
      if (scope == null) {
        scope = Artifact.SCOPE_COMPILE;
      }

      Artifact art =
          getArtifactFactory()
              .createDependencyArtifact(
                  groupId, artifactId, versionRange, type, classifier, scope, optional);

      if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
        art.setFile(new File(dependency.getSystemPath()));
      }

      List exclusions = new ArrayList();
      for (Iterator j = dependency.getExclusions().iterator(); j.hasNext(); ) {
        Exclusion e = (Exclusion) j.next();
        exclusions.add(e.getGroupId() + ":" + e.getArtifactId()); // $NON-NLS-1$
      }

      ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

      art.setDependencyFilter(newFilter);

      artifacts.add(art);
    }

    return artifacts;
  }
  public CollectResult collectDependencies(
      Task task,
      Dependencies dependencies,
      LocalRepository localRepository,
      RemoteRepositories remoteRepositories) {
    RepositorySystemSession session = getSession(task, localRepository);

    remoteRepositories =
        remoteRepositories == null
            ? AetherUtils.getDefaultRepositories(project)
            : remoteRepositories;

    List<org.sonatype.aether.repository.RemoteRepository> repos =
        ConverterUtils.toRepositories(project, session, remoteRepositories, getRemoteRepoMan());

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRequestContext("project");

    for (org.sonatype.aether.repository.RemoteRepository repo : repos) {
      task.getProject().log("Using remote repository " + repo, Project.MSG_VERBOSE);
      collectRequest.addRepository(repo);
    }

    if (dependencies != null) {
      List<Exclusion> globalExclusions = dependencies.getExclusions();
      Collection<String> ids = new HashSet<String>();

      for (Dependency dep : dependencies.getDependencies()) {
        ids.add(dep.getVersionlessKey());
        collectRequest.addDependency(ConverterUtils.toDependency(dep, globalExclusions, session));
      }

      if (dependencies.getPom() != null) {
        Model model = dependencies.getPom().getModel(task);
        for (org.apache.maven.model.Dependency dep : model.getDependencies()) {
          Dependency dependency = new Dependency();
          dependency.setArtifactId(dep.getArtifactId());
          dependency.setClassifier(dep.getClassifier());
          dependency.setGroupId(dep.getGroupId());
          dependency.setScope(dep.getScope());
          dependency.setType(dep.getType());
          dependency.setVersion(dep.getVersion());
          if (ids.contains(dependency.getVersionlessKey())) {
            continue;
          }
          if (dep.getSystemPath() != null && dep.getSystemPath().length() > 0) {
            dependency.setSystemPath(task.getProject().resolveFile(dep.getSystemPath()));
          }
          for (org.apache.maven.model.Exclusion exc : dep.getExclusions()) {
            Exclusion exclusion = new Exclusion();
            exclusion.setGroupId(exc.getGroupId());
            exclusion.setArtifactId(exc.getArtifactId());
            exclusion.setClassifier("*");
            exclusion.setExtension("*");
            dependency.addExclusion(exclusion);
          }
          collectRequest.addDependency(
              ConverterUtils.toDependency(dependency, globalExclusions, session));
        }
      }
    }

    task.getProject().log("Collecting dependencies", Project.MSG_VERBOSE);

    CollectResult result;
    try {
      result = getSystem().collectDependencies(session, collectRequest);
    } catch (DependencyCollectionException e) {
      throw new BuildException("Could not collect dependencies: " + e.getMessage(), e);
    }

    return result;
  }