use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class MetadataTransferTest method assertRepoReleaseMetadataContents.
/**
* Ensures that the repository specific maven metadata file exists, and contains the appropriate
* list of expected versions within.
*
* @param proxiedRepoId
* @param requestedResource
*/
private void assertRepoReleaseMetadataContents(String proxiedRepoId, String requestedResource) throws Exception {
String proxiedFile = metadataTools.getRepositorySpecificName(proxiedRepoId, requestedResource);
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue("Release metadata for repo should exist: " + actualFile, Files.exists(actualFile));
VersionedReference metadata = createVersionedReference(requestedResource);
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId(metadata.getGroupId());
m.setArtifactId(metadata.getArtifactId());
m.setVersion(metadata.getVersion());
RepositoryMetadataWriter.write(m, expectedMetadataXml);
// Compare the file to the actual contents.
assertMetadataEquals(expectedMetadataXml.toString(), actualFile);
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata 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);
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class MetadataTools method getMetadatasForManagedRepository.
private List<ArchivaRepositoryMetadata> getMetadatasForManagedRepository(ManagedRepositoryContent managedRepository, String logicalResource) {
List<ArchivaRepositoryMetadata> metadatas = new ArrayList<>();
Path file = Paths.get(managedRepository.getRepoRoot(), logicalResource);
if (Files.exists(file)) {
try {
ArchivaRepositoryMetadata existingMetadata = MavenMetadataReader.read(file);
if (existingMetadata != null) {
metadatas.add(existingMetadata);
}
} catch (XMLException e) {
log.debug("Could not read metadata at {}. Metadata will be removed.", file.toAbsolutePath());
FileUtils.deleteQuietly(file);
}
}
Set<String> proxyIds = proxies.get(managedRepository.getId());
if (proxyIds != null) {
for (String proxyId : proxyIds) {
ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, logicalResource, proxyId);
if (proxyMetadata != null) {
metadatas.add(proxyMetadata);
}
}
}
return metadatas;
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class RepositoryMetadataMerge method merge.
public static ArchivaRepositoryMetadata merge(final ArchivaRepositoryMetadata mainMetadata, final ArchivaRepositoryMetadata sourceMetadata) throws RepositoryMetadataException {
if (mainMetadata == null) {
throw new RepositoryMetadataException("Cannot merge a null main project.");
}
if (sourceMetadata == null) {
throw new RepositoryMetadataException("Cannot copy to a null parent project.");
}
ArchivaRepositoryMetadata merged = new ArchivaRepositoryMetadata();
merged.setGroupId(merge(mainMetadata.getGroupId(), sourceMetadata.getGroupId()));
merged.setArtifactId(merge(mainMetadata.getArtifactId(), sourceMetadata.getArtifactId()));
merged.setVersion(merge(mainMetadata.getVersion(), sourceMetadata.getVersion()));
merged.setReleasedVersion(merge(mainMetadata.getReleasedVersion(), sourceMetadata.getReleasedVersion()));
merged.setSnapshotVersion(merge(mainMetadata.getSnapshotVersion(), sourceMetadata.getSnapshotVersion()));
merged.setAvailableVersions(mergeAvailableVersions(mainMetadata.getAvailableVersions(), sourceMetadata.getAvailableVersions()));
merged.setPlugins(mergePlugins(mainMetadata.getPlugins(), sourceMetadata.getPlugins()));
// Don't set if merge was not possible
long lastUpdated = mergeTimestamp(mainMetadata.getLastUpdated(), sourceMetadata.getLastUpdated());
if (lastUpdated > -1) {
merged.setLastUpdated(Long.toString(lastUpdated));
}
return merged;
}
use of org.apache.archiva.model.ArchivaRepositoryMetadata in project archiva by apache.
the class RepositoryMetadataReaderTest method testLoadComplex.
@Test
public void testLoadComplex() throws XMLException {
Path defaultRepoDir = Paths.get("src/test/repositories/default-repository");
Path metadataFile = defaultRepoDir.resolve("org/apache/maven/samplejar/maven-metadata.xml");
ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(metadataFile);
assertNotNull(metadata);
assertEquals("Group Id", "org.apache.maven", metadata.getGroupId());
assertEquals("Artifact Id", "samplejar", metadata.getArtifactId());
assertEquals("Released Version", "2.0", metadata.getReleasedVersion());
assertEquals("Latest Version", "6.0-SNAPSHOT", metadata.getLatestVersion());
assertEquals("List of Available Versions", 18, metadata.getAvailableVersions().size());
assertTrue("Available version 6.0-20060311.183228-10", metadata.getAvailableVersions().contains("6.0-20060311.183228-10"));
assertTrue("Available version 6.0-SNAPSHOT", metadata.getAvailableVersions().contains("6.0-SNAPSHOT"));
}
Aggregations