示例#1
0
  private static boolean equals(
      org.eclipse.aether.artifact.Artifact a1, org.eclipse.aether.artifact.Artifact a2) {
    if (a1 == a2) {
      return true;
    }

    return a1.getArtifactId().equals(a2.getArtifactId())
        && a1.getGroupId().equals(a2.getGroupId())
        && a1.getVersion().equals(a2.getVersion())
        && a1.getExtension().equals(a2.getExtension())
        && a1.getClassifier().equals(a2.getClassifier());
  }
  private Artifact unify(Artifact dependent, Artifact dependency) {
    for (Entry<String, String> unification : unified.entrySet()) {
      String groupFilter = unification.getKey();
      String version = unification.getValue();
      String artifactId = dependency.toString();

      if ("".equals(version)) {
        version = dependent.getVersion();
      }
      if (artifactId.startsWith(groupFilter) && (!"".equals(version))) {
        Artifact retVal =
            new DefaultArtifact(
                dependency.getGroupId(),
                dependency.getArtifactId(),
                dependency.getClassifier(),
                dependency.getExtension(),
                // The important one
                version);
        retVal.setProperties(dependency.getProperties());
        return retVal;
      }
    }

    return dependency;
  }
  @Override
  protected void merge(Metadata recessive) {
    Snapshot snapshot;
    String lastUpdated;

    if (metadata.getVersioning() == null) {
      DateFormat utcDateFormatter = new SimpleDateFormat("yyyyMMdd.HHmmss");
      utcDateFormatter.setCalendar(new GregorianCalendar());
      utcDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

      snapshot = new Snapshot();
      snapshot.setBuildNumber(getBuildNumber(recessive) + 1);
      snapshot.setTimestamp(utcDateFormatter.format(new Date()));

      Versioning versioning = new Versioning();
      versioning.setSnapshot(snapshot);
      versioning.setLastUpdated(snapshot.getTimestamp().replace(".", ""));
      lastUpdated = versioning.getLastUpdated();

      metadata.setVersioning(versioning);
    } else {
      snapshot = metadata.getVersioning().getSnapshot();
      lastUpdated = metadata.getVersioning().getLastUpdated();
    }

    for (Artifact artifact : artifacts) {
      String version = artifact.getVersion();

      if (version.endsWith(SNAPSHOT)) {
        String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
        version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
      }

      SnapshotVersion sv = new SnapshotVersion();
      sv.setClassifier(artifact.getClassifier());
      sv.setExtension(artifact.getExtension());
      sv.setVersion(version);
      sv.setUpdated(lastUpdated);

      versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
    }

    artifacts.clear();

    Versioning versioning = recessive.getVersioning();
    if (versioning != null) {
      for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
        String key = getKey(sv.getClassifier(), sv.getExtension());
        if (!versions.containsKey(key)) {
          versions.put(key, sv);
        }
      }
    }

    if (!legacyFormat) {
      metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
    }
  }
示例#4
0
  URL createMavenGavURL(String artifactGav) throws MalformedURLException {
    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);
    }

    File file = artifactResult.getArtifact().getFile().getAbsoluteFile();
    return file.toURI().toURL();
  }
 @Override
 public void packageResolveFailed(DependencyResolutionException exception) {
   clearProgressBar();
   Artifact artifact = exception.getResult().getRoot().getArtifact();
   try {
     reader.println(
         String.format(
             "Could not resolve dependencies for %s.%s version %s",
             artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()));
     reader.println("The following artifacts could not be located:");
     for (ArtifactResult result : exception.getResult().getArtifactResults()) {
       if (result.isMissing()) {
         reader.println("* " + result.getRequest().getArtifact());
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 private String artifactAsString(Artifact artifact) {
   return Settings.GAV_JOINER.join(
       artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
 }
 public String getVersion() {
   return delegate.getVersion();
 }
示例#8
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;
  }
  @Override
  public void resolveProjectDependencies(
      final IMavenProjectFacade facade,
      Set<Capability> capabilities,
      Set<RequiredCapability> requirements,
      final IProgressMonitor monitor)
      throws CoreException {
    long start = System.currentTimeMillis();
    log.debug("Resolving dependencies for {}", facade.toString()); // $NON-NLS-1$

    markerManager.deleteMarkers(facade.getPom(), IMavenConstants.MARKER_DEPENDENCY_ID);

    ProjectBuildingRequest configuration =
        getMaven().getExecutionContext().newProjectBuildingRequest();
    configuration.setProject(facade.getMavenProject()); // TODO do we need this?
    configuration.setResolveDependencies(true);
    MavenExecutionResult mavenResult =
        getMaven().readMavenProject(facade.getPomFile(), configuration);

    markerManager.addMarkers(facade.getPom(), IMavenConstants.MARKER_DEPENDENCY_ID, mavenResult);

    if (!facade.getResolverConfiguration().shouldResolveWorkspaceProjects()) {
      return;
    }

    MavenProject mavenProject = facade.getMavenProject();

    // dependencies

    // resolved dependencies
    for (Artifact artifact : mavenProject.getArtifacts()) {
      requirements.add(
          MavenRequiredCapability.createMavenArtifact(
              new ArtifactKey(artifact), artifact.getScope(), artifact.isOptional()));
    }

    // extension plugins (affect packaging type calculation)
    for (Plugin plugin : mavenProject.getBuildPlugins()) {
      if (plugin.isExtensions()) {
        ArtifactKey artifactKey =
            new ArtifactKey(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), null);
        requirements.add(
            MavenRequiredCapability.createMavenArtifact(
                artifactKey, "plugin", false)); // $NON-NLS-1$
      }
    }

    // missing dependencies
    DependencyResolutionResult resolutionResult = mavenResult.getDependencyResolutionResult();
    if (resolutionResult != null && resolutionResult.getUnresolvedDependencies() != null) {
      for (Dependency dependency : resolutionResult.getUnresolvedDependencies()) {
        org.eclipse.aether.artifact.Artifact artifact = dependency.getArtifact();
        ArtifactKey dependencyKey =
            new ArtifactKey(
                artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), null);
        requirements.add(
            MavenRequiredCapability.createMavenArtifact(
                dependencyKey, dependency.getScope(), dependency.isOptional()));
      }
    }

    log.debug(
        "Resolved dependencies for {} in {} ms",
        facade.toString(),
        System.currentTimeMillis() - start); // $NON-NLS-1$
  }
  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);
    }
  }