Exemplo n.º 1
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 Map<String, Artifact> createManagedVersionMap() throws MojoExecutionException {
   Map<String, Artifact> map = new HashMap<String, Artifact>();
   DependencyManagement dependencyManagement = project.getDependencyManagement();
   if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
     for (Dependency d : dependencyManagement.getDependencies()) {
       try {
         VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
         Artifact artifact =
             artifactFactory.createDependencyArtifact(
                 d.getGroupId(),
                 d.getArtifactId(),
                 versionRange,
                 d.getType(),
                 d.getClassifier(),
                 d.getScope(),
                 d.isOptional());
         handleExclusions(artifact, d);
         map.put(d.getManagementKey(), artifact);
       } catch (InvalidVersionSpecificationException e) {
         throw new MojoExecutionException(
             String.format(
                 "%1s: unable to parse version '%2s' for dependency '%3s': %4s",
                 project.getId(), d.getVersion(), d.getManagementKey(), e.getMessage()),
             e);
       }
     }
   }
   return map;
 }
 @SuppressWarnings("unchecked")
 private Set<Artifact> getAllArtifacts() throws MojoExecutionException {
   Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
   for (Dependency dep : (List<Dependency>) project.getDependencies()) {
     VersionRange versionRange;
     try {
       versionRange = VersionRange.createFromVersionSpec(dep.getVersion());
     } catch (InvalidVersionSpecificationException e) {
       throw new MojoExecutionException(
           String.format(
               "%1s: unable to parse version '%2s' for dependency '%3s': %4s",
               dep.getArtifactId(), dep.getVersion(), dep.getManagementKey(), e.getMessage()),
           e);
     }
     String type = dep.getType() == null ? "jar" : dep.getType();
     boolean optional = dep.isOptional();
     String scope = dep.getScope() == null ? Artifact.SCOPE_COMPILE : dep.getScope();
     Artifact artifact =
         artifactFactory.createDependencyArtifact(
             dep.getGroupId(),
             dep.getArtifactId(),
             versionRange,
             type,
             dep.getClassifier(),
             scope,
             optional);
     if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
       artifact.setFile(new File(dep.getSystemPath()));
     }
     handleExclusions(artifact, dep);
     artifacts.add(artifact);
   }
   return artifacts;
 }
Exemplo n.º 4
0
  private Set<Artifact> processTransitiveDependencies(
      List<Dependency> dependencies, boolean sharedLibraries) throws MojoExecutionException {
    final Set<Artifact> transitiveArtifacts = new LinkedHashSet<Artifact>();
    for (Dependency dependency : dependencies) {
      if (!Artifact.SCOPE_PROVIDED.equals(dependency.getScope()) && !dependency.isOptional()) {
        transitiveArtifacts.addAll(processTransitiveDependencies(dependency, sharedLibraries));
      }
    }

    return transitiveArtifacts;
  }
  public GemSpecification createSpecification(final MavenArtifact artifact) {
    final GemSpecification result = new GemSpecification();

    // this is fix
    result.setPlatform(this.PLATFORM_JAVA);

    // the must ones
    result.setName(
        createGemName(
            artifact.getCoordinates().getGroupId(),
            artifact.getCoordinates().getArtifactId(),
            artifact.getCoordinates().getVersion()));
    result.setVersion(new GemVersion(createGemVersion(artifact.getCoordinates().getVersion())));

    // dependencies
    if (artifact.getPom().getDependencies().size() > 0) {
      for (final Dependency dependency : artifact.getPom().getDependencies()) {
        if (!dependency.isOptional()) {
          result.getDependencies().add(convertDependency(artifact, dependency));
        }
      }
    }

    // and other stuff "nice to have"
    result.setDate(new Date()); // now
    result.setDescription(
        sanitizeStringValue(
            artifact.getPom().getDescription() != null
                ? artifact.getPom().getDescription()
                : artifact.getPom().getName()));
    result.setSummary(sanitizeStringValue(artifact.getPom().getName()));
    result.setHomepage(sanitizeStringValue(artifact.getPom().getUrl()));

    if (artifact.getPom().getLicenses().size() > 0) {
      for (final License license : artifact.getPom().getLicenses()) {
        result
            .getLicenses()
            .add(sanitizeStringValue(license.getName() + " (" + license.getUrl() + ")"));
      }
    }
    if (artifact.getPom().getDevelopers().size() > 0) {
      for (final Developer developer : artifact.getPom().getDevelopers()) {
        result
            .getAuthors()
            .add(sanitizeStringValue(developer.getName() + " (" + developer.getEmail() + ")"));
      }
    }

    // by default, we pack into lib/ inside gem (where is the jar and the
    // stub ruby)
    result.getRequire_paths().add("lib");
    return result;
  }
Exemplo n.º 6
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;
  }
  private Set<Artifact> processTransientDependencies(
      List<org.apache.maven.model.Dependency> dependencies, boolean sharedLibraries)
      throws MojoExecutionException {

    Set<Artifact> transientArtifacts = new LinkedHashSet<Artifact>();
    for (org.apache.maven.model.Dependency dependency : dependencies) {
      if (!"provided".equals(dependency.getScope()) && !dependency.isOptional()) {
        transientArtifacts.addAll(
            processTransientDependencies(
                toDependency(dependency, repoSession.getArtifactTypeRegistry()), sharedLibraries));
      }
    }

    return transientArtifacts;
  }
 /**
  * Adds information about artifact dependencies.
  *
  * @param dependent The dependent to add artifacts as dependencies
  * @param dependencies The dependencies information.
  * @param scannerContext The scanner context
  */
 private <P extends MavenDependentDescriptor, D extends BaseDependencyDescriptor>
     void addDependencies(
         P dependent,
         List<Dependency> dependencies,
         Class<D> dependencyType,
         ScannerContext scannerContext) {
   for (Dependency dependency : dependencies) {
     MavenArtifactDescriptor dependencyArtifactDescriptor =
         getMavenArtifactDescriptor(dependency, scannerContext);
     D dependencyDescriptor =
         scannerContext.getStore().create(dependent, dependencyType, dependencyArtifactDescriptor);
     dependencyDescriptor.setOptional(dependency.isOptional());
     dependencyDescriptor.setScope(dependency.getScope());
   }
 }
Exemplo n.º 9
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());
 }
 /**
  * Adds information about profile dependencies.
  *
  * @param profileDescriptor The descriptor for the current profile.
  * @param dependencies The dependencies information.
  * @param scannerContext The scanner context.
  */
 private void addProfileDependencies(
     MavenProfileDescriptor profileDescriptor,
     List<Dependency> dependencies,
     ScannerContext scannerContext) {
   for (Dependency dependency : dependencies) {
     MavenArtifactDescriptor dependencyArtifactDescriptor =
         getMavenArtifactDescriptor(dependency, scannerContext);
     ProfileDependsOnDescriptor profileDependsOnDescriptor =
         scannerContext
             .getStore()
             .create(
                 profileDescriptor,
                 ProfileDependsOnDescriptor.class,
                 dependencyArtifactDescriptor);
     profileDependsOnDescriptor.setOptional(dependency.isOptional());
     profileDependsOnDescriptor.setScope(dependency.getScope());
   }
 }
  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;
  }
Exemplo n.º 12
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;
  }
  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());
  }
Exemplo n.º 14
0
  private Map createManagedVersionMap(
      ArtifactFactory artifactFactory, String projectId, DependencyManagement dependencyManagement)
      throws MojoExecutionException {
    Map map;
    if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
      map = new HashMap();
      for (Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext(); ) {
        Dependency d = (Dependency) i.next();

        try {
          VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
          Artifact artifact =
              artifactFactory.createDependencyArtifact(
                  d.getGroupId(),
                  d.getArtifactId(),
                  versionRange,
                  d.getType(),
                  d.getClassifier(),
                  d.getScope(),
                  d.isOptional());
          map.put(d.getManagementKey(), artifact);
        } catch (InvalidVersionSpecificationException e) {
          throw new MojoExecutionException(
              Messages.getString(
                  "unabletoparseversion",
                  new Object[] { // $NON-NLS-1$
                    projectId, d.getVersion(), d.getManagementKey(), e.getMessage()
                  }),
              e);
        }
      }
    } else {
      map = Collections.EMPTY_MAP;
    }
    return map;
  }
  private DefaultCoreExtension parseMavenPom(
      URL descriptorUrl, DefaultCoreExtensionRepository repository)
      throws IOException, XmlPullParserException {
    DefaultCoreExtension coreExtension = null;

    InputStream descriptorStream = descriptorUrl.openStream();
    try {
      MavenXpp3Reader reader = new MavenXpp3Reader();
      Model mavenModel = reader.read(descriptorStream);

      String version = resolveVersion(mavenModel.getVersion(), mavenModel, false);
      String groupId = resolveGroupId(mavenModel.getGroupId(), mavenModel, false);

      URL extensionURL = getExtensionURL(descriptorUrl);

      coreExtension =
          new MavenCoreExtension(
              repository,
              extensionURL,
              new ExtensionId(groupId + ':' + mavenModel.getArtifactId(), version),
              packagingToType(mavenModel.getPackaging()),
              mavenModel);

      coreExtension.setName(mavenModel.getName());
      coreExtension.setSummary(mavenModel.getDescription());
      for (Developer developer : mavenModel.getDevelopers()) {
        URL authorURL = null;
        if (developer.getUrl() != null) {
          try {
            authorURL = new URL(developer.getUrl());
          } catch (MalformedURLException e) {
            // TODO: log ?
          }
        }

        coreExtension.addAuthor(new DefaultExtensionAuthor(developer.getId(), authorURL));
      }
      coreExtension.setWebsite(mavenModel.getUrl());

      // licenses
      for (License license : mavenModel.getLicenses()) {
        coreExtension.addLicense(getExtensionLicense(license));
      }

      // features
      String featuresString = mavenModel.getProperties().getProperty("xwiki.extension.features");
      if (StringUtils.isNotBlank(featuresString)) {
        coreExtension.setFeatures(
            this.converter.<Collection<String>>convert(List.class, featuresString));
      }

      // custom properties
      coreExtension.putProperty("maven.groupId", groupId);
      coreExtension.putProperty("maven.artifactId", mavenModel.getArtifactId());

      // dependencies
      for (Dependency mavenDependency : mavenModel.getDependencies()) {
        if (!mavenDependency.isOptional()
            && (mavenDependency.getScope() == null
                || mavenDependency.getScope().equals("compile")
                || mavenDependency.getScope().equals("runtime"))) {

          String dependencyGroupId = resolveGroupId(mavenDependency.getGroupId(), mavenModel, true);
          String dependencyArtifactId = mavenDependency.getArtifactId();
          String dependencyClassifier = mavenDependency.getClassifier();
          String dependencyVersion = resolveVersion(mavenDependency.getVersion(), mavenModel, true);

          DefaultExtensionDependency extensionDependency =
              new MavenCoreExtensionDependency(
                  toExtensionId(dependencyGroupId, dependencyArtifactId, dependencyClassifier),
                  new DefaultVersionConstraint(dependencyVersion),
                  mavenDependency);

          coreExtension.addDependency(extensionDependency);
        }
      }
    } finally {
      IOUtils.closeQuietly(descriptorStream);
    }

    return coreExtension;
  }
Exemplo n.º 16
0
  /**
   * 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;
  }