use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class MavenMetadataGenerator method writeVersionMetadata.
private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
ArtifactPathInfo samplePomInfo = null;
logger.debug("writeVersionMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());
// first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
// as a valid version
final List<SingleVersion> versions = new ArrayList<>();
nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
final String topPath = topResource.getPath();
if (topPath.endsWith("/")) {
final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
for (final StoreResource fileResource : secondLevelListing) {
if (fileResource.getPath().endsWith(".pom")) {
ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
// check if the pom is valid for the path
if (filePomInfo != null) {
versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
if (samplePomInfo == null) {
samplePomInfo = filePomInfo;
}
continue nextTopResource;
}
}
}
}
}
if (versions.isEmpty()) {
logger.debug("writeVersionMetadata, versions is empty, store:{}", store.getKey());
return false;
}
logger.debug("writeVersionMetadata, versions: {}, store:{}", versions, store.getKey());
Collections.sort(versions);
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, samplePomInfo == null ? null : samplePomInfo.getArtifactId());
coordMap.put(GROUP_ID, samplePomInfo == null ? null : samplePomInfo.getGroupId());
final String lastUpdated = generateUpdateTimestamp(getCurrentTimestamp());
doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
xml.createElement(doc.getDocumentElement(), null, coordMap);
final Map<String, String> versioningMap = new HashMap<>();
versioningMap.put(LAST_UPDATED, lastUpdated);
final SingleVersion latest = versions.get(versions.size() - 1);
versioningMap.put(LATEST, latest.renderStandard());
SingleVersion release = null;
for (int i = versions.size() - 1; i >= 0; i--) {
final SingleVersion r = versions.get(i);
if (r.isRelease()) {
release = r;
break;
}
}
if (release != null) {
versioningMap.put(RELEASE, release.renderStandard());
}
xml.createElement(doc, "versioning", versioningMap);
final Element versionsElem = xml.createElement(doc, "versioning/versions", Collections.emptyMap());
for (final SingleVersion version : versions) {
final Element vElem = doc.createElement(VERSION);
vElem.setTextContent(version.renderStandard());
versionsElem.appendChild(vElem);
}
final String xmlStr = xml.toXML(doc, true);
logger.debug("writeVersionMetadata, xmlStr: {}", xmlStr);
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);
}
logger.debug("writeVersionMetadata, DONE, store: {}", store.getKey());
return true;
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class MavenMetadataGenerator method writeGroupMergeInfo.
private String writeGroupMergeInfo(final Metadata md, final Group group, final List<StoreKey> contributingMembers, final String path) throws IndyWorkflowException {
logger.trace("Start write .info file based on if the cache exists for group {} of members {} in path {}. ", group.getKey(), contributingMembers, path);
final Transfer mergeInfoTarget = fileManager.getTransfer(group, path + GroupMergeHelper.MERGEINFO_SUFFIX);
logger.trace(".info file not found for {} of members {} in path {}", group.getKey(), contributingMembers, path);
logger.trace("metadata merge info not cached for group {} of members {} in path {}, will regenerate.", group.getKey(), contributingMembers, path);
String metaMergeInfo = helper.generateMergeInfoFromKeys(contributingMembers);
logger.trace("Metadata merge info for {} of members {} in path {} is {}", group.getKey(), contributingMembers, path, metaMergeInfo);
helper.writeMergeInfo(metaMergeInfo, group, path);
logger.trace(".info file regenerated for group {} of members {} in path. Full path: {}", group.getKey(), contributingMembers, path);
return metaMergeInfo;
}
use of org.commonjava.maven.galley.model.Transfer 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;
}
use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.
the class MavenMetadataGeneratorTest method generateFileContent_VersionsMetadataWith2Versions.
@Test
public void generateFileContent_VersionsMetadataWith2Versions() throws Exception {
final StoreResource resource = setupVersionsStructureWith2Versions();
final ConcreteResource metadataFile = resource.getChild("maven-metadata.xml");
final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), metadataFile.getPath(), new EventMetadata());
assertThat(transfer, notNullValue());
final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), new EventMetadata());
assertThat(metadata, notNullValue());
final VersioningView versioning = metadata.getVersioning();
final List<SingleVersion> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.get(0).renderStandard(), equalTo("1.0"));
assertThat(versions.get(1).renderStandard(), equalTo("1.1"));
assertThat(versioning.getReleaseVersion().renderStandard(), equalTo("1.1"));
assertThat(versioning.getLatestVersion().renderStandard(), equalTo("1.1"));
}
use of org.commonjava.maven.galley.model.Transfer 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));
}
}
Aggregations