Search in sources :

Example 6 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project galley by Commonjava.

the class VersionResolverImpl method resolveLatestMultiRefWithLocation.

private ProjectVersionRefLocation resolveLatestMultiRefWithLocation(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
    final Map<SingleVersion, Location> available = new TreeMap<>();
    for (final Location location : locations) {
        try {
            final MavenMetadataView metadata = metadataReader.getMetadata(ref.asProjectRef(), Collections.singletonList(location), eventMetadata);
            if (metadata != null) {
                final List<String> versions = metadata.resolveValues("/metadata/versioning/versions/version");
                if (versions != null) {
                    for (final String version : versions) {
                        try {
                            final SingleVersion spec = VersionUtils.createSingleVersion(version);
                            if (!available.containsKey(spec)) {
                                available.put(spec, location);
                            }
                        } catch (final InvalidVersionSpecificationException e) {
                            debug("Unparsable version spec found in metadata: '%s' for: %s from: %s.", e, version, ref, location);
                        }
                    }
                }
            }
        } catch (final GalleyMavenException e) {
            debug("Failed to resolve/parse metadata for variable version of: '%s' from: %s.", e, ref, location);
        }
    }
    if (!available.isEmpty()) {
        final VersionSpec spec = ref.getVersionSpec();
        final List<SingleVersion> versions = new ArrayList<>(available.keySet());
        Collections.sort(versions);
        while (!versions.isEmpty()) {
            final SingleVersion selected = selectionStrategy.select(versions);
            if (selected == null) {
                return null;
            }
            versions.remove(selected);
            if (selected.isConcrete() && spec.contains(selected)) {
                return new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected));
            }
        }
    }
    return null;
}
Also used : GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ArrayList(java.util.ArrayList) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) TreeMap(java.util.TreeMap) VersionSpec(org.commonjava.atlas.maven.ident.version.VersionSpec) MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) InvalidVersionSpecificationException(org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) Location(org.commonjava.maven.galley.model.Location)

Example 7 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project galley by Commonjava.

the class VersionResolverImpl method resolveFirstSnapshotRefWithLocation.

private ProjectVersionRefLocation resolveFirstSnapshotRefWithLocation(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
    for (final Location location : locations) {
        final Map<SingleVersion, Location> available = new TreeMap<>();
        try {
            final MavenMetadataView metadata = metadataReader.getMetadata(ref, Collections.singletonList(location), eventMetadata);
            if (metadata != null) {
                addSnapshotFrom(metadata, location, ref, available);
            }
        } catch (final GalleyMavenException e) {
            debug("Failed to resolve/parse metadata for snapshot version of: %s from: %s.", e, ref, location);
        }
        if (!available.isEmpty()) {
            final List<SingleVersion> versions = new ArrayList<>(available.keySet());
            Collections.sort(versions);
            final SingleVersion selected = selectionStrategy.select(versions);
            if (selected == null) {
                continue;
            }
            return new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected));
        }
    }
    return null;
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) ArrayList(java.util.ArrayList) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) TreeMap(java.util.TreeMap) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) Location(org.commonjava.maven.galley.model.Location)

Example 8 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project galley by Commonjava.

the class VersionResolverImpl method addSnapshotFrom.

private void addSnapshotFrom(final MavenMetadataView metadata, final Location location, final ProjectVersionRef ref, final Map<SingleVersion, Location> available) throws GalleyMavenException {
    final String version = metadata.resolveSingleValue(SNAP_VERSION_XPATH);
    logger.debug("Latest snapshot version in metadata is: {}", version);
    if (version != null) {
        try {
            final SingleVersion ver = VersionUtils.createSingleVersion(version);
            if (!available.containsKey(ver)) {
                logger.debug("Found candidate snapshot: {}", ver);
                available.put(ver, location);
            }
        } catch (final InvalidVersionSpecificationException e) {
            debug("Unparsable version spec found in metadata: '%s' for: %s from: %s", e, version, ref, location);
        }
    }
}
Also used : InvalidVersionSpecificationException(org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion)

Example 9 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project galley by Commonjava.

the class VersionResolverImpl method resolveAllSnapshotRefsWithLocations.

private List<ProjectVersionRefLocation> resolveAllSnapshotRefsWithLocations(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
    final Map<SingleVersion, Location> available = new TreeMap<>();
    for (final Location location : locations) {
        try {
            final MavenMetadataView metadata = metadataReader.getMetadata(ref, Collections.singletonList(location), eventMetadata);
            if (metadata != null) {
                final String latest = metadata.resolveSingleValue("/metadata/versioning/latest");
                if (latest != null) {
                    try {
                        final SingleVersion ver = VersionUtils.createSingleVersion(latest);
                        if (ver.isSnapshot()) {
                            if (!available.containsKey(ver)) {
                                available.put(ver, location);
                            }
                        }
                    } catch (final InvalidVersionSpecificationException e) {
                        debug("Unparsable version spec found in metadata: '%s' for: %s from: %s", e, latest, ref, location);
                    }
                }
            }
        } catch (final GalleyMavenException e) {
            debug("Failed to resolve/parse metadata for snapshot version of: %s from: %s.", e, ref, location);
        }
    }
    if (!available.isEmpty()) {
        return Collections.emptyList();
    }
    final List<SingleVersion> versions = new ArrayList<>(available.keySet());
    Collections.sort(versions);
    final List<ProjectVersionRefLocation> result = new ArrayList<>();
    while (!versions.isEmpty()) {
        final SingleVersion selected = selectionStrategy.select(versions);
        if (selected != null) {
            versions.remove(selected);
            result.add(new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected)));
        }
    }
    return result;
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) InvalidVersionSpecificationException(org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) ArrayList(java.util.ArrayList) JoinString(org.commonjava.atlas.maven.ident.util.JoinString) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) TreeMap(java.util.TreeMap) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) Location(org.commonjava.maven.galley.model.Location)

Example 10 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project offliner by release-engineering.

the class OfflinerUtils method generateMetadata.

/**
 * Given a list of Maven POM files, generate appropriate Maven repository metadata files by parsing the POM's paths
 * and extracting GroupId / ArtifactId / Version information.
 * @param pomPaths List of POM paths to parse
 * @param outputRootPath
 */
public static void generateMetadata(Set<String> pomPaths, final String outputRootPath) {
    Map<ProjectRef, List<SingleVersion>> metas = new HashMap<>();
    for (String path : pomPaths) {
        ArtifactPathInfo artifactPathInfo = ArtifactPathInfo.parse(path);
        ProjectVersionRef gav = artifactPathInfo.getProjectId();
        List<SingleVersion> singleVersions = new ArrayList<SingleVersion>();
        if (!metas.isEmpty() && metas.containsKey(gav.asProjectRef())) {
            singleVersions = metas.get(gav.asProjectRef());
        }
        singleVersions.add((SingleVersion) gav.getVersionSpec());
        metas.put(gav.asProjectRef(), singleVersions);
    }
    for (ProjectRef ga : metas.keySet()) {
        List<SingleVersion> singleVersions = metas.get(ga);
        Collections.sort(singleVersions);
        Metadata master = new Metadata();
        master.setGroupId(ga.getGroupId());
        master.setArtifactId(ga.getArtifactId());
        Versioning versioning = new Versioning();
        for (SingleVersion v : singleVersions) {
            versioning.addVersion(v.renderStandard());
        }
        String latest = singleVersions.get(singleVersions.size() - 1).renderStandard();
        versioning.setLatest(latest);
        versioning.setRelease(latest);
        master.setVersioning(versioning);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        File metadataFile = Paths.get(outputRootPath, ga.getGroupId().replace('.', File.separatorChar), ga.getArtifactId(), "maven-metadata.xml").toFile();
        try {
            new MetadataXpp3Writer().write(baos, master);
            FileUtils.writeByteArrayToFile(metadataFile, baos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
            System.err.printf("\n\nFailed to generate maven-metadata file: %s. See above for more information.\n", metadataFile);
        }
    }
}
Also used : Metadata(org.apache.maven.artifact.repository.metadata.Metadata) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) ProjectVersionRef(org.commonjava.atlas.maven.ident.ref.ProjectVersionRef) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) ArtifactList(com.redhat.red.offliner.model.ArtifactList) ProjectRef(org.commonjava.atlas.maven.ident.ref.ProjectRef) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) File(java.io.File)

Aggregations

SingleVersion (org.commonjava.atlas.maven.ident.version.SingleVersion)14 ArrayList (java.util.ArrayList)8 MavenMetadataView (org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView)7 InvalidVersionSpecificationException (org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException)6 TreeMap (java.util.TreeMap)5 GalleyMavenException (org.commonjava.maven.galley.maven.GalleyMavenException)5 ProjectVersionRefLocation (org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation)5 Location (org.commonjava.maven.galley.model.Location)5 JoinString (org.commonjava.atlas.maven.ident.util.JoinString)4 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)3 StoreResource (org.commonjava.indy.content.StoreResource)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 Transfer (org.commonjava.maven.galley.model.Transfer)3 Logger (org.slf4j.Logger)3 File (java.io.File)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)2 SimpleProjectVersionRef (org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef)2