RepositoryMetadataMerger extends ComponentSupport { public void merge(final OutputStream outputStream, final MavenPath mavenPath, final Map<Repository, Content> contents) { log.debug("Merge metadata for {}", mavenPath.getPath()); List<Envelope> metadatas = new ArrayList<>(contents.size()); try { for (Map.Entry<Repository, Content> entry : contents.entrySet()) { addReadMetadata(mavenPath, metadatas, entry); } final Metadata mergedMetadata = merge(metadatas); if (mergedMetadata == null) { return; } MavenModels.writeMetadata(outputStream, mergedMetadata); } catch (IOException e) { log.error("Unable to merge {}", mavenPath, e); } }  void merge(final OutputStream outputStream,
                    final MavenPath mavenPath,
                    final Map<Repository, Content> contents); boolean metadataEquals(final Metadata md1, final Metadata md2); @Nullable Metadata merge(final Iterable<Envelope> metadatas);  }
@Test public void groupLevelMd() throws Exception { final Metadata m1 = g("foo"); final Metadata m2 = g("foo", "bar"); final Metadata m3 = g("baz"); final Metadata m = merger.merge( ImmutableList.of(new Envelope("1", m1), new Envelope("2", m2), new Envelope("3", m3)) ); assertThat(m, notNullValue()); assertThat(m.getModelVersion(), equalTo("1.1.0")); assertThat(m.getPlugins(), hasSize(3)); final List<String> prefixes = Lists.newArrayList(Iterables.transform(m.getPlugins(), new Function<Plugin, String>() { @Override public String apply(final Plugin input) { return input.getArtifactId(); } })); assertThat(prefixes, containsInAnyOrder("foo-maven-plugin", "bar-maven-plugin", "baz-maven-plugin")); }
@Test public void artifactLevelMd() throws Exception { final Metadata m1 = a("org.foo", "some-project", "20150324121500", "1.0.1", "1.0.1", "1.0.0", "1.0.1"); final Metadata m2 = a("org.foo", "some-project", "20150324121700", "1.0.2", "1.0.2", "1.0.2"); final Metadata m3 = a("org.foo", "some-project", "20150324121600", "1.1.0-SNAPSHOT", null, "1.1.0-SNAPSHOT"); final Metadata m = merger.merge( ImmutableList.of(new Envelope("1", m1), new Envelope("2", m2), new Envelope("3", m3)) ); assertThat(m, notNullValue()); assertThat(m.getModelVersion(), equalTo("1.1.0")); assertThat(m.getGroupId(), equalTo("org.foo")); assertThat(m.getArtifactId(), equalTo("some-project")); assertThat(m.getVersioning().getLastUpdated(), equalTo("20150324121700")); assertThat(m.getVersioning().getSnapshot(), nullValue()); assertThat(m.getVersioning().getRelease(), equalTo("1.0.2")); assertThat(m.getVersioning().getLatest(), equalTo("1.1.0-SNAPSHOT")); assertThat(m.getVersioning().getVersions(), contains("1.0.0", "1.0.1", "1.0.2", "1.1.0-SNAPSHOT")); }
@Test public void versionLevelMd() throws Exception { final Metadata m1 = v("org.foo", "some-project", "1.0.0", "20150324.121500", 3); final Metadata m2 = v("org.foo", "some-project", "1.0.0", "20150323.121500", 2); final Metadata m3 = v("org.foo", "some-project", "1.0.0", "20150322.121500", 1); final Metadata m = merger.merge( ImmutableList.of(new Envelope("1", m1), new Envelope("2", m2), new Envelope("3", m3)) ); assertThat(m, notNullValue()); assertThat(m.getModelVersion(), equalTo("1.1.0")); assertThat(m.getGroupId(), equalTo("org.foo")); assertThat(m.getArtifactId(), equalTo("some-project")); assertThat(m.getVersioning().getLastUpdated(), equalTo("20150324121500")); assertThat(m.getVersioning().getSnapshot(), notNullValue()); assertThat(m.getVersioning().getSnapshot().getTimestamp(), equalTo("20150324.121500")); assertThat(m.getVersioning().getSnapshot().getBuildNumber(), equalTo(3)); }
@Test public void mixedLevelMd() throws Exception { final Metadata m1 = a("org.foo", "some-project", "20150324121500", "1.0.1", "1.0.1", "1.0.0", "1.0.1"); final Metadata m2 = g("foo", "bar"); final Metadata m3 = v("org.foo", "some-project", "1.1.0", "20150322.121500", 3); final Metadata m = merger.merge( ImmutableList.of(new Envelope("1", m1), new Envelope("2", m2), new Envelope("3", m3)) ); assertThat(m, notNullValue()); assertThat(m.getModelVersion(), equalTo("1.1.0")); assertThat(m.getGroupId(), equalTo("org.foo")); assertThat(m.getArtifactId(), equalTo("some-project")); assertThat(m.getVersion(), equalTo("1.1.0-SNAPSHOT")); assertThat(m.getVersioning().getLastUpdated(), equalTo("20150324121500")); assertThat(m.getVersioning().getSnapshot(), notNullValue()); assertThat(m.getVersioning().getSnapshot().getTimestamp(), equalTo("20150322.121500")); assertThat(m.getVersioning().getSnapshot().getBuildNumber(), equalTo(3)); assertThat(m.getVersioning().getRelease(), equalTo("1.0.1")); assertThat(m.getVersioning().getLatest(), equalTo("1.0.1")); assertThat(m.getVersioning().getVersions(), contains("1.0.0", "1.0.1")); assertThat(m.getPlugins(), hasSize(2)); final List<String> prefixes = Lists.newArrayList(Iterables.transform(m.getPlugins(), new Function<Plugin, String>() { @Override public String apply(final Plugin input) { return input.getArtifactId(); } })); assertThat(prefixes, containsInAnyOrder("foo-maven-plugin", "bar-maven-plugin")); }
@Test public void allowVersionInArtifactLevelMetadata() { Metadata m1 = a("org.foo", "some-project", "20150324121500", "1.0.0","1.0.0", "1.0.0"); m1.setVersion("1.0.0"); Metadata m2 = a("org.foo", "some-project", "20150324121501", "1.0.1","1.0.1", "1.0.1"); m2.setVersion("1.0.1"); final Metadata m = merger.merge( ImmutableList.of(new Envelope("1", m1), new Envelope("2", m2)) ); assertThat(m.getVersion(), is(m1.getVersion())); assertThat(m.getVersioning().getRelease(), is(m2.getVersion())); assertThat(m.getVersioning().getLastUpdated(), is(m2.getVersioning().getLastUpdated())); assertThat(m.getVersioning().getVersions(), contains("1.0.0", "1.0.1")); }
@Test public void handleNullSnapshotTimestamps() { Metadata m1 = v("org.foo", "some-project", "1.0.0", "20150322.121500", 1); m1.getVersioning().getSnapshot().setTimestamp(null); Metadata m2 = v("org.foo", "some-project", "1.0.0", "20150323.121500", 2); final Metadata m = merger.merge( ImmutableList.of(new Envelope("1", m1), new Envelope("2", m2)) ); assertThat(m, notNullValue()); assertThat(m.getModelVersion(), equalTo("1.1.0")); assertThat(m.getGroupId(), equalTo("org.foo")); assertThat(m.getArtifactId(), equalTo("some-project")); assertThat(m.getVersioning().getLastUpdated(), equalTo("20150323121500")); assertThat(m.getVersioning().getSnapshot(), notNullValue()); assertThat(m.getVersioning().getSnapshot().getTimestamp(), equalTo("20150323.121500")); assertThat(m.getVersioning().getSnapshot().getBuildNumber(), equalTo(2)); }