Search in sources :

Example 1 with MavenLatestVersionNotAvailableException

use of org.jboss.galleon.universe.maven.MavenLatestVersionNotAvailableException in project galleon by wildfly.

the class LocalArtifactVersionRangeResolver method resolveLatestVersionDir.

private Path resolveLatestVersionDir(MavenArtifact artifact, String lowestQualifier, Pattern includeVersion, Pattern excludeVersion) throws MavenUniverseException {
    if (artifact.getGroupId() == null) {
        MavenErrors.missingGroupId();
    }
    if (artifact.getArtifactId() == null) {
        MavenErrors.missingArtifactId();
    }
    if (artifact.getVersionRange() == null) {
        throw new MavenUniverseException("Version range is missing for " + artifact.getCoordsAsString());
    }
    Path artifactDir = repoHome;
    final String[] groupParts = artifact.getGroupId().split("\\.");
    for (String part : groupParts) {
        artifactDir = artifactDir.resolve(part);
    }
    artifactDir = artifactDir.resolve(artifact.getArtifactId());
    if (!Files.exists(artifactDir)) {
        throw MavenErrors.artifactNotFound(artifact, repoHome);
    }
    final MavenArtifactVersionRange range = versionRangeParser.parseRange(artifact.getVersionRange());
    if (lowestQualifier == null) {
        lowestQualifier = "";
    }
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(artifactDir)) {
        final Iterable<String> versions = new Iterable<String>() {

            @Override
            public Iterator<String> iterator() {
                return new Iterator<String>() {

                    final Iterator<Path> i = stream.iterator();

                    Path nextPath = toNext(range);

                    @Override
                    public boolean hasNext() {
                        return nextPath != null;
                    }

                    @Override
                    public String next() {
                        if (nextPath != null) {
                            final String s = nextPath.getFileName().toString();
                            nextPath = toNext(range);
                            return s;
                        }
                        throw new NoSuchElementException();
                    }

                    private Path toNext(final MavenArtifactVersionRange range) {
                        while (i.hasNext()) {
                            final Path path = i.next();
                            if (!Files.isDirectory(path)) {
                                continue;
                            }
                            final MavenArtifactVersion next = new MavenArtifactVersion(path.getFileName().toString());
                            if (range.includesVersion(next)) {
                                return path;
                            }
                        }
                        return null;
                    }
                };
            }
        };
        final MavenArtifactVersion latest = MavenArtifactVersion.getLatest(versions, lowestQualifier, includeVersion, excludeVersion);
        if (latest == null) {
            throw new MavenLatestVersionNotAvailableException(MavenErrors.failedToResolveLatestVersion(artifact.getCoordsAsString()));
        }
        return artifactDir.resolve(latest.toString());
    } catch (MavenUniverseException e) {
        throw e;
    } catch (Exception e) {
        throw new MavenUniverseException(MavenErrors.failedToResolveLatestVersion(artifact.getCoordsAsString()), e);
    }
}
Also used : Path(java.nio.file.Path) MavenLatestVersionNotAvailableException(org.jboss.galleon.universe.maven.MavenLatestVersionNotAvailableException) MavenUniverseException(org.jboss.galleon.universe.maven.MavenUniverseException) MavenLatestVersionNotAvailableException(org.jboss.galleon.universe.maven.MavenLatestVersionNotAvailableException) NoSuchElementException(java.util.NoSuchElementException) Iterator(java.util.Iterator) MavenUniverseException(org.jboss.galleon.universe.maven.MavenUniverseException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with MavenLatestVersionNotAvailableException

use of org.jboss.galleon.universe.maven.MavenLatestVersionNotAvailableException in project galleon by wildfly.

the class AbstractMavenArtifactRepositoryManager method doGetHighestVersion.

private String doGetHighestVersion(MavenArtifact mavenArtifact, String lowestQualifier, boolean locallyAvailable, Pattern includeVersion, Pattern excludeVersion) throws MavenUniverseException {
    if (locallyAvailable) {
        if (localRangeResolver == null) {
            localRangeResolver = new LocalArtifactVersionRangeResolver(getSession().getLocalRepository().getBasedir().toPath());
        }
        return localRangeResolver.getLatestVersion(mavenArtifact, lowestQualifier, includeVersion, excludeVersion);
    }
    final VersionRangeResult rangeResult = getVersionRange(new DefaultArtifact(mavenArtifact.getGroupId(), mavenArtifact.getArtifactId(), mavenArtifact.getExtension(), mavenArtifact.getVersionRange()));
    final MavenArtifactVersion latest = rangeResult == null ? null : MavenArtifactVersion.getLatest(rangeResult.getVersions(), lowestQualifier, includeVersion, excludeVersion);
    if (latest == null) {
        throw new MavenLatestVersionNotAvailableException(MavenErrors.failedToResolveLatestVersion(mavenArtifact.getCoordsAsString()));
    }
    return latest.toString();
}
Also used : VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) LocalArtifactVersionRangeResolver(org.jboss.galleon.universe.maven.repo.LocalArtifactVersionRangeResolver) MavenArtifactVersion(org.jboss.galleon.universe.maven.repo.MavenArtifactVersion) MavenLatestVersionNotAvailableException(org.jboss.galleon.universe.maven.MavenLatestVersionNotAvailableException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

MavenLatestVersionNotAvailableException (org.jboss.galleon.universe.maven.MavenLatestVersionNotAvailableException)2 Path (java.nio.file.Path)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)1 VersionRangeResult (org.eclipse.aether.resolution.VersionRangeResult)1 MavenUniverseException (org.jboss.galleon.universe.maven.MavenUniverseException)1 LocalArtifactVersionRangeResolver (org.jboss.galleon.universe.maven.repo.LocalArtifactVersionRangeResolver)1 MavenArtifactVersion (org.jboss.galleon.universe.maven.repo.MavenArtifactVersion)1