use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader in project indy by Commonjava.
the class MavenMetadataMergerTest method mergeTwoFilesWithInterleavedVersions.
@Test
public void mergeTwoFilesWithInterleavedVersions() throws Exception {
String path = "org/foo/bar/maven-metadata.xml";
HostedRepository h1 = new HostedRepository("test-hosted-1");
HostedRepository h2 = new HostedRepository("test-hosted-2");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "simple-skip.xml");
Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
initTestData(t2, VERSION_META + "simple-2.xml");
Group g = new Group("test-group", h1.getKey(), h2.getKey());
List<Transfer> sources = Arrays.asList(t1, t2);
byte[] output = new MavenMetadataMerger(Collections.emptyList()).merge(sources, g, path);
Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
assertThat(merged.getGroupId(), equalTo("org.foo"));
assertThat(merged.getArtifactId(), equalTo("bar"));
Versioning versioning = merged.getVersioning();
assertThat(versioning, notNullValue());
List<String> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(3));
assertThat(versioning.getRelease(), equalTo("1.2"));
assertThat(versioning.getLatest(), equalTo("1.2"));
int idx = 0;
assertThat(versions.get(idx), equalTo("1.0"));
idx++;
assertThat(versions.get(idx), equalTo("1.1"));
idx++;
assertThat(versions.get(idx), equalTo("1.2"));
}
use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader in project indy by Commonjava.
the class MavenMetadataMergerTest method mergeTwoSimpleVersionMetadataFiles.
@Test
public void mergeTwoSimpleVersionMetadataFiles() throws Exception {
String path = "org/foo/bar/maven-metadata.xml";
HostedRepository h1 = new HostedRepository("test-hosted-1");
HostedRepository h2 = new HostedRepository("test-hosted-2");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "simple-1.xml");
Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
initTestData(t2, VERSION_META + "simple-2.xml");
Group g = new Group("test-group", h1.getKey(), h2.getKey());
List<Transfer> sources = Arrays.asList(t1, t2);
byte[] output = new MavenMetadataMerger(Collections.emptyList()).merge(sources, g, path);
Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
assertThat(merged.getGroupId(), equalTo("org.foo"));
assertThat(merged.getArtifactId(), equalTo("bar"));
Versioning versioning = merged.getVersioning();
assertThat(versioning, notNullValue());
List<String> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(2));
assertThat(versioning.getRelease(), equalTo("1.1"));
assertThat(versioning.getLatest(), equalTo("1.1"));
int idx = 0;
assertThat(versions.get(idx), equalTo("1.0"));
idx++;
assertThat(versions.get(idx), equalTo("1.1"));
}
use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader in project indy by Commonjava.
the class MavenMetadataMergerTest method mergeOneTransferWithProviderError.
@Test
public void mergeOneTransferWithProviderError() throws Exception {
String path = "org/foo/bar/maven-metadata.xml";
HostedRepository h1 = new HostedRepository("test-hosted-1");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "simple-1.xml");
Group g = new Group("test-group", h1.getKey());
List<Transfer> sources = Arrays.asList(t1);
TestMavenMetadataProvider testProvider = new TestMavenMetadataProvider(new IndyWorkflowException("Failed to get provider content"));
byte[] output = new MavenMetadataMerger(Collections.singletonList(testProvider)).merge(sources, g, path);
Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
assertThat(merged.getGroupId(), equalTo("org.foo"));
assertThat(merged.getArtifactId(), equalTo("bar"));
Versioning versioning = merged.getVersioning();
assertThat(versioning, notNullValue());
List<String> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(1));
assertThat(versioning.getRelease(), equalTo("1.0"));
assertThat(versioning.getLatest(), equalTo("1.0"));
int idx = 0;
assertThat(versions.get(idx), equalTo("1.0"));
}
use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader in project indy by Commonjava.
the class MavenMetadataMerger method merge.
@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("Generating merged metadata in: {}:{}", group.getKey(), path);
final Metadata master = new Metadata();
master.setVersioning(new Versioning());
final MetadataXpp3Reader reader = new MetadataXpp3Reader();
final FileReader fr = null;
InputStream stream = null;
boolean merged = false;
Transfer snapshotProvider = null;
for (final Transfer src : sources) {
if (!src.exists()) {
continue;
}
try {
stream = src.openInputStream();
String content = IOUtils.toString(stream);
logger.debug("Adding in metadata content from: {}\n\n{}\n\n", src, content);
// there is a lot of junk in here to make up for Metadata's anemic merge() method.
final Metadata md = reader.read(new StringReader(content), false);
if (md.getGroupId() != null) {
master.setGroupId(md.getGroupId());
}
if (md.getArtifactId() != null) {
master.setArtifactId(md.getArtifactId());
}
if (md.getVersion() != null) {
master.setVersion(md.getVersion());
}
master.merge(md);
Versioning versioning = master.getVersioning();
Versioning mdVersioning = md.getVersioning();
// FIXME: Should we try to merge snapshot lists instead of using the first one we encounter??
if (versioning.getSnapshot() == null && mdVersioning != null) {
logger.info("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
snapshotProvider = src;
versioning.setSnapshot(mdVersioning.getSnapshot());
final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
boolean added = false;
for (final SnapshotVersion snap : mdVersioning.getSnapshotVersions()) {
if (!snapshotVersions.contains(snap)) {
snapshotVersions.add(snap);
added = true;
}
}
if (added) {
Collections.sort(snapshotVersions, new SnapshotVersionComparator());
}
} else {
logger.warn("SKIPPING snapshot information from: {} in: {}:{} (obscured by: {})", src, group.getKey(), path, snapshotProvider);
}
merged = true;
} catch (final IOException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot read metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} catch (final XmlPullParserException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot parse metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} finally {
closeQuietly(fr);
closeQuietly(stream);
}
}
Versioning versioning = master.getVersioning();
if (versioning != null && versioning.getVersions() != null) {
if (metadataProviders != null) {
for (MavenMetadataProvider provider : metadataProviders) {
try {
Metadata toMerge = provider.getMetadata(group.getKey(), path);
if (toMerge != null) {
merged = master.merge(toMerge) || merged;
}
} catch (IndyWorkflowException e) {
logger.error(String.format("Cannot read metadata: %s from metadata provider: %s. Reason: %s", path, provider.getClass().getSimpleName(), e.getMessage()), e);
}
}
}
List<SingleVersion> versionObjects = versioning.getVersions().stream().map(VersionUtils::createSingleVersion).collect(Collectors.toList());
Collections.sort(versionObjects);
versioning.setVersions(versionObjects.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
if (versionObjects.size() > 0) {
String latest = versionObjects.get(versionObjects.size() - 1).renderStandard();
versioning.setLatest(latest);
versioning.setRelease(latest);
}
}
if (merged) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
new MetadataXpp3Writer().write(baos, master);
return baos.toByteArray();
} catch (final IOException e) {
logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
}
}
return null;
}
use of org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader in project indy by Commonjava.
the class MavenMetadataMergerTest method mergeOneTransferWithProviderContent.
@Test
public void mergeOneTransferWithProviderContent() throws Exception {
String path = "org/foo/bar/maven-metadata.xml";
HostedRepository h1 = new HostedRepository("test-hosted-1");
Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
initTestData(t1, VERSION_META + "simple-1.xml");
Group g = new Group("test-group", h1.getKey());
List<Transfer> sources = Arrays.asList(t1);
Versioning providedVersioning = new Versioning();
providedVersioning.setLatest("1.1");
providedVersioning.setRelease("1.1");
providedVersioning.addVersion("1.1");
Metadata provided = new Metadata();
provided.setGroupId("org.foo");
provided.setArtifactId("bar");
provided.setVersioning(providedVersioning);
TestMavenMetadataProvider testProvider = new TestMavenMetadataProvider(provided);
byte[] output = new MavenMetadataMerger(Collections.singletonList(testProvider)).merge(sources, g, path);
Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
assertThat(merged.getGroupId(), equalTo("org.foo"));
assertThat(merged.getArtifactId(), equalTo("bar"));
Versioning versioning = merged.getVersioning();
assertThat(versioning, notNullValue());
List<String> versions = versioning.getVersions();
assertThat(versions, notNullValue());
assertThat(versions.size(), equalTo(2));
assertThat(versioning.getRelease(), equalTo("1.1"));
assertThat(versioning.getLatest(), equalTo("1.1"));
int idx = 0;
assertThat(versions.get(idx), equalTo("1.0"));
idx++;
assertThat(versions.get(idx), equalTo("1.1"));
}
Aggregations