From 0d961d34aa221d43473155148ca5976740549df6 Mon Sep 17 00:00:00 2001 From: jonmv Date: Tue, 14 Jun 2022 12:15:06 +0200 Subject: Handle multiple artifacts with different classifiers --- .../container/plugin/mojo/AssembleTestBundleMojo.java | 2 +- .../mojo/GenerateTestBundleOsgiManifestMojo.java | 2 +- .../util/TestBundleDependencyScopeTranslator.java | 19 +++++++++---------- .../util/TestBundleDependencyScopeTranslatorTest.java | 14 ++++++++------ 4 files changed, 19 insertions(+), 18 deletions(-) (limited to 'bundle-plugin') diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java index 117d2cdc87e..acf0950decd 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java @@ -27,7 +27,7 @@ public class AssembleTestBundleMojo extends AbstractAssembleBundleMojo { @Override public void execute() throws MojoExecutionException { Artifacts.ArtifactSet artifacts = Artifacts.getArtifacts( - project, TestBundleDependencyScopeTranslator.from(project.getArtifactMap(), testBundleScopeOverrides)); + project, TestBundleDependencyScopeTranslator.from(project.getArtifacts(), testBundleScopeOverrides)); JarArchiver archiver = new JarArchiver(); addDirectory(archiver, Paths.get(project.getBuild().getOutputDirectory())); addDirectory(archiver, Paths.get(project.getBuild().getTestOutputDirectory())); diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java index 0b10627d396..5708d90f82f 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java @@ -40,7 +40,7 @@ public class GenerateTestBundleOsgiManifestMojo extends AbstractGenerateOsgiMani public void execute() throws MojoExecutionException { try { Artifacts.ArtifactSet artifactSet = Artifacts.getArtifacts( - project, TestBundleDependencyScopeTranslator.from(project.getArtifactMap(), testBundleScopeOverrides)); + project, TestBundleDependencyScopeTranslator.from(project.getArtifacts(), testBundleScopeOverrides)); List providedJars = artifactSet.getJarArtifactsProvided().stream() .map(Artifact::getFile) diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslator.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslator.java index bd6151aea9f..65606633dee 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslator.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslator.java @@ -4,13 +4,17 @@ package com.yahoo.container.plugin.util; import org.apache.maven.artifact.Artifact; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.Function; import java.util.logging.Logger; +import java.util.stream.Collectors; import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; /** * Translates the scope of dependencies when constructing a test bundle. @@ -40,11 +44,12 @@ public class TestBundleDependencyScopeTranslator implements Artifacts.ScopeTrans return Objects.requireNonNull(dependencyScopes.get(artifact), () -> "Could not lookup scope for " + artifact); } - public static TestBundleDependencyScopeTranslator from(Map dependencies, String rawConfig) { + public static TestBundleDependencyScopeTranslator from(Collection dependencies, String rawConfig) { List dependencyOverrides = toDependencyOverrides(rawConfig); Map dependencyScopes = new HashMap<>(); - for (Artifact dependency : dependencies.values()) { - dependencyScopes.put(dependency, getScopeForDependency(dependency, dependencyOverrides, dependencies)); + Map dependenciesById = dependencies.stream().collect(toMap(Artifact::getId, Function.identity())); + for (Artifact dependency : dependencies) { + dependencyScopes.put(dependency, getScopeForDependency(dependency, dependencyOverrides, dependenciesById)); } return new TestBundleDependencyScopeTranslator(dependencyScopes); } @@ -66,12 +71,6 @@ public class TestBundleDependencyScopeTranslator implements Artifacts.ScopeTrans return new DependencyOverride(elements[0], elements[1], elements[2]); } - private static String stripVersionAndScope(String idInDependencyTrail) { - int firstDelimiter = idInDependencyTrail.indexOf(':'); - int secondDelimiter = idInDependencyTrail.indexOf(':', firstDelimiter + 1); - return idInDependencyTrail.substring(0, secondDelimiter); - } - private static String getScopeForDependency( Artifact dependency, List overrides, Map otherArtifacts) { String oldScope = dependency.getScope(); @@ -95,7 +94,7 @@ public class TestBundleDependencyScopeTranslator implements Artifacts.ScopeTrans private static List dependencyTrailOf(Artifact artifact, Map otherArtifacts) { return artifact.getDependencyTrail().stream() .skip(1) // Maven project itself is the first entry - .map(parentId -> otherArtifacts.get(stripVersionAndScope(parentId))) + .map(otherArtifacts::get) .filter(Objects::nonNull) .collect(toList()); } diff --git a/bundle-plugin/src/test/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslatorTest.java b/bundle-plugin/src/test/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslatorTest.java index d5986b00077..c0f2766b5ed 100644 --- a/bundle-plugin/src/test/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslatorTest.java +++ b/bundle-plugin/src/test/java/com/yahoo/container/plugin/util/TestBundleDependencyScopeTranslatorTest.java @@ -10,7 +10,9 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.TreeMap; +import java.util.TreeSet; import static org.junit.Assert.assertEquals; @@ -23,7 +25,7 @@ public class TestBundleDependencyScopeTranslatorTest { @Test public void test_dependencies_are_translated_to_compile_scope_by_default() { - Map artifacts = new TreeMap<>(); + Set artifacts = new TreeSet<>(); Artifact a = createArtifact(artifacts, "a", "test", List.of()); Artifact aa = createArtifact(artifacts, "a-a", "test", List.of("a")); Artifact ab = createArtifact(artifacts, "a-b", "test", List.of("a")); @@ -39,7 +41,7 @@ public class TestBundleDependencyScopeTranslatorTest { @Test public void non_test_scope_dependencies_keep_original_scope() { - Map artifacts = new TreeMap<>(); + Set artifacts = new TreeSet<>(); Artifact a = createArtifact(artifacts, "a", "provided", List.of()); Artifact aa = createArtifact(artifacts, "a-a", "provided", List.of("a")); Artifact ab = createArtifact(artifacts, "a-b", "provided", List.of("a")); @@ -60,7 +62,7 @@ public class TestBundleDependencyScopeTranslatorTest { @Test public void ordering_in_config_string_determines_translation() { - Map artifacts = new TreeMap<>(); + Set artifacts = new TreeSet<>(); Artifact a = createArtifact(artifacts, "a", "test", List.of()); Artifact aa = createArtifact(artifacts, "a-a", "test", List.of("a")); { @@ -83,7 +85,7 @@ public class TestBundleDependencyScopeTranslatorTest { @Test public void transitive_non_test_dependencies_of_test_dependencies_keep_original_scope() { - Map artifacts = new TreeMap<>(); + Set artifacts = new TreeSet<>(); Artifact a = createArtifact(artifacts, "a", "test", List.of()); Artifact aa = createArtifact(artifacts, "a-a", "test", List.of("a")); Artifact ab = createArtifact(artifacts, "a-b", "test", List.of("a")); @@ -106,9 +108,9 @@ public class TestBundleDependencyScopeTranslatorTest { } private static Artifact createArtifact( - Map artifacts, String artifactId, String scope, List transitiveDependents) { + Set artifacts, String artifactId, String scope, List transitiveDependents) { Artifact artifact = createArtifact(artifactId, scope, transitiveDependents); - artifacts.put(simpleId(artifactId), artifact); + artifacts.add(artifact); return artifact; } -- cgit v1.2.3