Search in sources :

Example 6 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class RepositoryMetadataMerge method merge.

private static SnapshotVersion merge(SnapshotVersion mainSnapshotVersion, SnapshotVersion sourceSnapshotVersion) {
    if (sourceSnapshotVersion == null) {
        return mainSnapshotVersion;
    }
    if (mainSnapshotVersion == null) {
        return ArchivaModelCloner.clone(sourceSnapshotVersion);
    }
    SnapshotVersion merged = new SnapshotVersion();
    long mainSnapshotLastUpdated = convertTimestampToLong(mainSnapshotVersion.getTimestamp());
    long sourceSnapshotLastUpdated = convertTimestampToLong(sourceSnapshotVersion.getTimestamp());
    long lastUpdated = mergeTimestamp(mainSnapshotLastUpdated, sourceSnapshotLastUpdated);
    if (lastUpdated == mainSnapshotLastUpdated) {
        merged.setTimestamp(mainSnapshotVersion.getTimestamp());
        merged.setBuildNumber(mainSnapshotVersion.getBuildNumber());
    } else {
        merged.setTimestamp(sourceSnapshotVersion.getTimestamp());
        merged.setBuildNumber(sourceSnapshotVersion.getBuildNumber());
    }
    return merged;
}
Also used : SnapshotVersion(org.apache.archiva.model.SnapshotVersion)

Example 7 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class MetadataTransferTest method assertSnapshotMetadata.

private void assertSnapshotMetadata(Path actualFile, VersionedReference actualMetadata, String expectedDate, String expectedTime, int expectedBuildnumber) throws RepositoryMetadataException, Exception {
    // Build expected metadata XML
    StringWriter expectedMetadataXml = new StringWriter();
    ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
    m.setGroupId(actualMetadata.getGroupId());
    m.setArtifactId(actualMetadata.getArtifactId());
    m.setVersion(VersionUtil.getBaseVersion(actualMetadata.getVersion()));
    m.setSnapshotVersion(new SnapshotVersion());
    if (StringUtils.isNotBlank(expectedDate) && StringUtils.isNotBlank(expectedTime)) {
        m.getSnapshotVersion().setTimestamp(expectedDate + "." + expectedTime);
    }
    m.getSnapshotVersion().setBuildNumber(expectedBuildnumber);
    m.setLastUpdated(expectedDate + expectedTime);
    RepositoryMetadataWriter.write(m, expectedMetadataXml);
    // Compare the file to the actual contents.
    assertMetadataEquals(expectedMetadataXml.toString(), actualFile);
}
Also used : StringWriter(java.io.StringWriter) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 8 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class MavenMetadataReader method read.

/**
 * Read and return the {@link org.apache.archiva.model.ArchivaRepositoryMetadata} object from the provided xml file.
 *
 * @param metadataFile the maven-metadata.xml file to read.
 * @return the archiva repository metadata object that represents the provided file contents.
 * @throws XMLException
 */
public static ArchivaRepositoryMetadata read(Path metadataFile) throws XMLException {
    XMLReader xml = new XMLReader("metadata", metadataFile);
    // invoke this to remove namespaces, see MRM-1136
    xml.removeNamespaces();
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    metadata.setGroupId(xml.getElementText("//metadata/groupId"));
    metadata.setArtifactId(xml.getElementText("//metadata/artifactId"));
    metadata.setVersion(xml.getElementText("//metadata/version"));
    Date modTime;
    try {
        modTime = new Date(Files.getLastModifiedTime(metadataFile).toMillis());
    } catch (IOException e) {
        modTime = new Date();
        log.error("Could not read modification time of {}", metadataFile);
    }
    metadata.setFileLastModified(modTime);
    try {
        metadata.setFileSize(Files.size(metadataFile));
    } catch (IOException e) {
        metadata.setFileSize(0);
        log.error("Could not read file size of {}", metadataFile);
    }
    metadata.setLastUpdated(xml.getElementText("//metadata/versioning/lastUpdated"));
    metadata.setLatestVersion(xml.getElementText("//metadata/versioning/latest"));
    metadata.setReleasedVersion(xml.getElementText("//metadata/versioning/release"));
    metadata.setAvailableVersions(xml.getElementListText("//metadata/versioning/versions/version"));
    Element snapshotElem = xml.getElement("//metadata/versioning/snapshot");
    if (snapshotElem != null) {
        SnapshotVersion snapshot = new SnapshotVersion();
        snapshot.setTimestamp(snapshotElem.elementTextTrim("timestamp"));
        String tmp = snapshotElem.elementTextTrim("buildNumber");
        if (NumberUtils.isNumber(tmp)) {
            snapshot.setBuildNumber(NumberUtils.toInt(tmp));
        }
        metadata.setSnapshotVersion(snapshot);
    }
    for (Element plugin : xml.getElementList("//metadata/plugins/plugin")) {
        Plugin p = new Plugin();
        p.setPrefix(plugin.elementTextTrim("prefix"));
        p.setArtifactId(plugin.elementTextTrim("artifactId"));
        p.setName(plugin.elementTextTrim("name"));
        metadata.addPlugin(p);
    }
    return metadata;
}
Also used : SnapshotVersion(org.apache.archiva.model.SnapshotVersion) Element(org.dom4j.Element) IOException(java.io.IOException) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) XMLReader(org.apache.archiva.xml.XMLReader) Date(java.util.Date) Plugin(org.apache.archiva.model.Plugin)

Example 9 with SnapshotVersion

use of org.apache.archiva.model.SnapshotVersion in project archiva by apache.

the class MetadataTools method updateMetadata.

/**
 * Update the metadata based on the following rules.
 * <p>
 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
 * metadata files to represent the current / latest SNAPSHOT available.
 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
 * create the metadata file with contents required of the VersionedReference
 *
 * @param managedRepository the managed repository where the metadata is kept.
 * @param reference         the versioned reference to update
 * @throws LayoutException
 * @throws RepositoryMetadataException
 * @throws IOException
 * @throws ContentNotFoundException
 * @deprecated
 */
public void updateMetadata(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), toPath(reference));
    long lastUpdated = getExistingLastUpdated(metadataFile);
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    metadata.setGroupId(reference.getGroupId());
    metadata.setArtifactId(reference.getArtifactId());
    if (VersionUtil.isSnapshot(reference.getVersion())) {
        // Do SNAPSHOT handling.
        metadata.setVersion(VersionUtil.getBaseVersion(reference.getVersion()));
        // Gather up all of the versions found in the reference dir, and any
        // proxied maven-metadata.xml files.
        Set<String> snapshotVersions = gatherSnapshotVersions(managedRepository, reference);
        if (snapshotVersions.isEmpty()) {
            throw new ContentNotFoundException("No snapshot versions found on reference [" + VersionedReference.toKey(reference) + "].");
        }
        // sort the list to determine to aide in determining the Latest version.
        List<String> sortedVersions = new ArrayList<>();
        sortedVersions.addAll(snapshotVersions);
        Collections.sort(sortedVersions, new VersionComparator());
        String latestVersion = sortedVersions.get(sortedVersions.size() - 1);
        if (VersionUtil.isUniqueSnapshot(latestVersion)) {
            // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
            // This needs to be broken down into ${base}-${timestamp}-${build_number}
            Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher(latestVersion);
            if (m.matches()) {
                metadata.setSnapshotVersion(new SnapshotVersion());
                int buildNumber = NumberUtils.toInt(m.group(3), -1);
                metadata.getSnapshotVersion().setBuildNumber(buildNumber);
                Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher(m.group(2));
                if (mtimestamp.matches()) {
                    String tsDate = mtimestamp.group(1);
                    String tsTime = mtimestamp.group(2);
                    long snapshotLastUpdated = toLastUpdatedLong(tsDate + tsTime);
                    lastUpdated = Math.max(lastUpdated, snapshotLastUpdated);
                    metadata.getSnapshotVersion().setTimestamp(m.group(2));
                }
            }
        } else if (VersionUtil.isGenericSnapshot(latestVersion)) {
            // The latestVersion ends with the generic version string.
            // Example: 1.0-alpha-5-SNAPSHOT
            metadata.setSnapshotVersion(new SnapshotVersion());
        /* Disabled due to decision in [MRM-535].
                 * Do not set metadata.lastUpdated to file.lastModified.
                 * 
                 * Should this be the last updated timestamp of the file, or in the case of an 
                 * archive, the most recent timestamp in the archive?
                 * 
                ArtifactReference artifact = getFirstArtifact( managedRepository, reference );

                if ( artifact == null )
                {
                    throw new IOException( "Not snapshot artifact found to reference in " + reference );
                }

                File artifactFile = managedRepository.toFile( artifact );

                if ( artifactFile.exists() )
                {
                    Date lastModified = new Date( artifactFile.lastModified() );
                    metadata.setLastUpdatedTimestamp( lastModified );
                }
                */
        } else {
            throw new RepositoryMetadataException("Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">");
        }
    } else {
        // Do RELEASE handling.
        metadata.setVersion(reference.getVersion());
    }
    // Set last updated
    if (lastUpdated > 0) {
        metadata.setLastUpdatedTimestamp(toLastUpdatedDate(lastUpdated));
    }
    // Save the metadata model to disk.
    RepositoryMetadataWriter.write(metadata, metadataFile);
    ChecksummedFile checksum = new ChecksummedFile(metadataFile);
    checksum.fixChecksums(algorithms);
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) VersionComparator(org.apache.archiva.common.utils.VersionComparator) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Aggregations

SnapshotVersion (org.apache.archiva.model.SnapshotVersion)9 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)7 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)2 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)2 XMLException (org.apache.archiva.xml.XMLException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)1 ChecksummedFile (org.apache.archiva.checksum.ChecksummedFile)1 VersionComparator (org.apache.archiva.common.utils.VersionComparator)1 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)1 RepositoryProblemFacet (org.apache.archiva.metadata.model.facets.RepositoryProblemFacet)1