Search in sources :

Example 1 with InvalidVersionSpecificationException

use of org.eclipse.aether.version.InvalidVersionSpecificationException in project buck by facebook.

the class Resolver method getNewerVersionFile.

/**
   * @return {@link Path} to the file in {@code project} with filename consistent with the given
   * {@link Artifact}, but with a newer version. If no such file exists, {@link Optional#empty} is
   * returned. If multiple such files are present one with the newest version will be returned.
   */
@VisibleForTesting
Optional<Path> getNewerVersionFile(final Artifact artifactToDownload, Path project) throws IOException {
    final Version artifactToDownloadVersion;
    try {
        artifactToDownloadVersion = versionScheme.parseVersion(artifactToDownload.getVersion());
    } catch (InvalidVersionSpecificationException e) {
        throw new RuntimeException(e);
    }
    final Pattern versionExtractor = Pattern.compile(String.format(ARTIFACT_FILE_NAME_REGEX_FORMAT, artifactToDownload.getArtifactId(), VERSION_REGEX_GROUP, artifactToDownload.getExtension()));
    Iterable<Version> versionsPresent = FluentIterable.from(Files.newDirectoryStream(project)).transform(new Function<Path, Version>() {

        @Nullable
        @Override
        public Version apply(Path input) {
            Matcher matcher = versionExtractor.matcher(input.getFileName().toString());
            if (matcher.matches()) {
                try {
                    return versionScheme.parseVersion(matcher.group(1));
                } catch (InvalidVersionSpecificationException e) {
                    throw new RuntimeException(e);
                }
            } else {
                return null;
            }
        }
    }).filter(Objects::nonNull);
    List<Version> newestPresent = Ordering.natural().greatestOf(versionsPresent, 1);
    if (newestPresent.isEmpty() || newestPresent.get(0).compareTo(artifactToDownloadVersion) <= 0) {
        return Optional.empty();
    } else {
        return Optional.of(project.resolve(String.format(ARTIFACT_FILE_NAME_FORMAT, artifactToDownload.getArtifactId(), newestPresent.get(0).toString(), artifactToDownload.getExtension())));
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Function(com.google.common.base.Function) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) Version(org.eclipse.aether.version.Version) Matcher(java.util.regex.Matcher) Objects(java.util.Objects) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with InvalidVersionSpecificationException

use of org.eclipse.aether.version.InvalidVersionSpecificationException in project fabric8 by jboss-fuse.

the class AetherBasedResolver method resolve.

private File resolve(List<LocalRepository> defaultRepos, List<RemoteRepository> remoteRepos, Artifact artifact) throws IOException {
    if (artifact.getExtension().isEmpty()) {
        artifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "jar", artifact.getVersion());
    }
    // Try with default repositories
    try {
        VersionConstraint vc = new GenericVersionScheme().parseVersionConstraint(artifact.getVersion());
        if (vc.getVersion() != null) {
            for (LocalRepository repo : defaultRepos) {
                if (vc.getVersion().toString().endsWith("SNAPSHOT") && !handlesSnapshot(repo)) {
                    continue;
                }
                DefaultRepositorySystemSession session = newSession(repo);
                try {
                    return m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, null, null)).getArtifact().getFile();
                } catch (ArtifactResolutionException e) {
                // Ignore
                } finally {
                    releaseSession(session);
                }
            }
        }
    } catch (InvalidVersionSpecificationException e) {
    // Should not happen
    }
    DefaultRepositorySystemSession session = newSession(null);
    try {
        artifact = resolveLatestVersionRange(session, remoteRepos, artifact);
        ArtifactResult result = m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, remoteRepos, null));
        File resolved = result.getArtifact().getFile();
        LOG.debug("Resolved ({}) as {}", artifact.toString(), resolved.getAbsolutePath());
        return resolved;
    } catch (ArtifactResolutionException e) {
        // we know there's one ArtifactResult, because there was one ArtifactRequest
        ArtifactResolutionException original = new ArtifactResolutionException(e.getResults(), "Error resolving artifact " + artifact.toString(), null);
        original.setStackTrace(e.getStackTrace());
        List<String> messages = new ArrayList<>(e.getResult().getExceptions().size());
        List<Exception> suppressed = new ArrayList<>();
        for (Exception ex : e.getResult().getExceptions()) {
            messages.add(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
            suppressed.add(ex);
        }
        IOException exception = new IOException(original.getMessage() + ": " + messages, original);
        for (Exception ex : suppressed) {
            exception.addSuppressed(ex);
        }
        LOG.warn(exception.getMessage(), exception);
        for (Exception ex : suppressed) {
            LOG.warn(" - " + ex.getMessage());
        }
        throw exception;
    } catch (RepositoryException e) {
        throw new IOException("Error resolving artifact " + artifact.toString(), e);
    } finally {
        releaseSession(session);
    }
}
Also used : VersionConstraint(org.eclipse.aether.version.VersionConstraint) LocalRepository(org.eclipse.aether.repository.LocalRepository) RepositoryException(org.eclipse.aether.RepositoryException) IOException(java.io.IOException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) File(java.io.File) JarFile(java.util.jar.JarFile) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

InvalidVersionSpecificationException (org.eclipse.aether.version.InvalidVersionSpecificationException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Function (com.google.common.base.Function)1 File (java.io.File)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Objects (java.util.Objects)1 JarFile (java.util.jar.JarFile)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)1 RepositoryException (org.eclipse.aether.RepositoryException)1 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)1