Search in sources :

Example 1 with SnapshotPart

use of org.commonjava.atlas.maven.ident.version.part.SnapshotPart in project indy by Commonjava.

the class MavenMetadataGenerator method writeSnapshotMetadata.

/**
 * First level will contain files that have the timestamp-buildNumber version suffix, e.g., 'o11yphant-metrics-api-1.0-20200805.065728-1.pom'
 * we need to parse each this info and add them to snapshot versions.
 */
private boolean writeSnapshotMetadata(final ArtifactPathInfo info, final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    final Map<SnapshotPart, Set<ArtifactPathInfo>> infosBySnap = new HashMap<>();
    for (final StoreResource resource : firstLevelFiles) {
        final ArtifactPathInfo resInfo = ArtifactPathInfo.parse(resource.getPath());
        if (resInfo != null) {
            final SnapshotPart snap = resInfo.getSnapshotInfo();
            Set<ArtifactPathInfo> infos = infosBySnap.computeIfAbsent(snap, k -> new HashSet<>());
            infos.add(resInfo);
        }
    }
    if (infosBySnap.isEmpty()) {
        return false;
    }
    final List<SnapshotPart> snaps = new ArrayList<>(infosBySnap.keySet());
    Collections.sort(snaps);
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();
        final Map<String, String> coordMap = new HashMap<>();
        coordMap.put(ARTIFACT_ID, info.getArtifactId());
        coordMap.put(GROUP_ID, info.getGroupId());
        coordMap.put(VERSION, info.getReleaseVersion() + LOCAL_SNAPSHOT_VERSION_PART);
        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);
        // the last one is the most recent
        SnapshotPart snap = snaps.get(snaps.size() - 1);
        Map<String, String> snapMap = new HashMap<>();
        if (snap.isLocalSnapshot()) {
            snapMap.put(LOCAL_COPY, Boolean.TRUE.toString());
        } else {
            snapMap.put(TIMESTAMP, SnapshotUtils.generateSnapshotTimestamp(snap.getTimestamp()));
            snapMap.put(BUILD_NUMBER, Integer.toString(snap.getBuildNumber()));
        }
        final Date currentTimestamp = getCurrentTimestamp();
        final String lastUpdated = getUpdateTimestamp(snap, currentTimestamp);
        xml.createElement(doc, "versioning", Collections.singletonMap(LAST_UPDATED, lastUpdated));
        xml.createElement(doc, "versioning/snapshot", snapMap);
        for (SnapshotPart snap1 : snaps) {
            final Set<ArtifactPathInfo> infos = infosBySnap.get(snap1);
            for (final ArtifactPathInfo pathInfo : infos) {
                snapMap = new HashMap<>();
                final TypeAndClassifier tc = new SimpleTypeAndClassifier(pathInfo.getType(), pathInfo.getClassifier());
                final TypeMapping mapping = typeMapper.lookup(tc);
                final String classifier = mapping == null ? pathInfo.getClassifier() : mapping.getClassifier();
                if (classifier != null && classifier.length() > 0) {
                    snapMap.put(CLASSIFIER, classifier);
                }
                snapMap.put(EXTENSION, mapping == null ? pathInfo.getType() : mapping.getExtension());
                snapMap.put(VALUE, pathInfo.getVersion());
                snapMap.put(UPDATED, getUpdateTimestamp(pathInfo.getSnapshotInfo(), currentTimestamp));
                xml.createElement(doc, "versioning/snapshotVersions/snapshotVersion", snapMap);
            }
        }
        final String xmlStr = xml.toXML(doc, true);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes(UTF_8));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path, e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e, metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }
    return true;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) StoreResource(org.commonjava.indy.content.StoreResource) SnapshotPart(org.commonjava.atlas.maven.ident.version.part.SnapshotPart) TypeAndClassifier(org.commonjava.atlas.maven.ident.ref.TypeAndClassifier) SimpleTypeAndClassifier(org.commonjava.atlas.maven.ident.ref.SimpleTypeAndClassifier) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleTypeAndClassifier(org.commonjava.atlas.maven.ident.ref.SimpleTypeAndClassifier) TypeMapping(org.commonjava.maven.galley.model.TypeMapping)

Example 2 with SnapshotPart

use of org.commonjava.atlas.maven.ident.version.part.SnapshotPart in project indy by Commonjava.

the class MavenMetadataGeneratorTest method generateFileContent_SnapshotMetadataWith2Versions.

@Test
public void generateFileContent_SnapshotMetadataWith2Versions() throws Exception {
    final StoreResource resource = setupSnapshotDirWith2Snapshots();
    final EventMetadata emd = new EventMetadata();
    final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), resource.getChild("maven-metadata.xml").getPath(), emd);
    assertThat(transfer, notNullValue());
    final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), emd);
    assertThat(metadata, notNullValue());
    final VersioningView versioning = metadata.getVersioning();
    final LatestSnapshotView latestSnapshot = versioning.getLatestSnapshot();
    assertThat(latestSnapshot.isLocalCopy(), equalTo(false));
    assertThat(latestSnapshot.getTimestamp(), equalTo(SnapshotUtils.parseSnapshotTimestamp("20140828.225800")));
    assertThat(latestSnapshot.getBuildNumber(), equalTo(1));
    final List<SnapshotArtifactView> snapshots = versioning.getSnapshotArtifacts();
    assertThat(snapshots.size(), equalTo(4));
    for (final SnapshotArtifactView snap : snapshots) {
        final String extension = snap.getExtension();
        assertThat(extension.equals("jar") || extension.equals("pom"), equalTo(true));
        final String version = snap.getVersion();
        System.out.println(version);
        final SingleVersion parsed = VersionUtils.createSingleVersion(version);
        assertThat(parsed.isSnapshot(), equalTo(true));
        assertThat(parsed.isLocalSnapshot(), equalTo(false));
        final SnapshotPart part = parsed.getSnapshotPart();
        final String tstamp = SnapshotUtils.generateSnapshotTimestamp(part.getTimestamp());
        assertThat(tstamp.equals("20140828.225800") || tstamp.equals("20140828.221400"), equalTo(true));
    }
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) VersioningView(org.commonjava.maven.galley.maven.model.view.meta.VersioningView) StoreResource(org.commonjava.indy.content.StoreResource) SnapshotPart(org.commonjava.atlas.maven.ident.version.part.SnapshotPart) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) LatestSnapshotView(org.commonjava.maven.galley.maven.model.view.meta.LatestSnapshotView) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) SnapshotArtifactView(org.commonjava.maven.galley.maven.model.view.meta.SnapshotArtifactView) Test(org.junit.Test)

Example 3 with SnapshotPart

use of org.commonjava.atlas.maven.ident.version.part.SnapshotPart in project indy by Commonjava.

the class MavenContentsFilteringTransferDecorator method isArtifact.

/**
 * Checks if the given element is an artifact. Artifacts always starts with
 * &lt;artifactId&gt;-&lt;version&gt;.
 */
private boolean isArtifact(final String element, final String artifactId, final String version) {
    if (element.endsWith("/")) {
        return false;
    }
    boolean isRemoteSnapshot = false;
    if (SnapshotUtils.isSnapshotVersion(version) && element.startsWith(artifactId + '-') && !element.startsWith(artifactId + '-' + version)) {
        final SnapshotPart snapshotPart = SnapshotUtils.extractSnapshotVersionPart(version);
        final int artIdLenght = artifactId.length() + 1 + version.length() - snapshotPart.getLiteral().length() + 1;
        isRemoteSnapshot = SnapshotUtils.isRemoteSnapshotVersionPart(StringUtils.substring(element, artIdLenght, artIdLenght + 17));
    }
    return element.startsWith(artifactId + '-' + version + '-') || element.startsWith(artifactId + '-' + version + '.') || isRemoteSnapshot;
}
Also used : SnapshotPart(org.commonjava.atlas.maven.ident.version.part.SnapshotPart)

Aggregations

SnapshotPart (org.commonjava.atlas.maven.ident.version.part.SnapshotPart)3 StoreResource (org.commonjava.indy.content.StoreResource)2 Transfer (org.commonjava.maven.galley.model.Transfer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 SimpleProjectVersionRef (org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef)1 SimpleTypeAndClassifier (org.commonjava.atlas.maven.ident.ref.SimpleTypeAndClassifier)1 TypeAndClassifier (org.commonjava.atlas.maven.ident.ref.TypeAndClassifier)1 ArtifactPathInfo (org.commonjava.atlas.maven.ident.util.ArtifactPathInfo)1 SingleVersion (org.commonjava.atlas.maven.ident.version.SingleVersion)1 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)1 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)1 LatestSnapshotView (org.commonjava.maven.galley.maven.model.view.meta.LatestSnapshotView)1 MavenMetadataView (org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView)1