diff --git a/platforms/software/maven/src/integTest/groovy/org/gradle/api/publish/maven/MavenPublishDependenciesIntegTest.groovy b/platforms/software/maven/src/integTest/groovy/org/gradle/api/publish/maven/MavenPublishDependenciesIntegTest.groovy index 40bd833121692..87ca46b1bd42a 100644 --- a/platforms/software/maven/src/integTest/groovy/org/gradle/api/publish/maven/MavenPublishDependenciesIntegTest.groovy +++ b/platforms/software/maven/src/integTest/groovy/org/gradle/api/publish/maven/MavenPublishDependenciesIntegTest.groovy @@ -270,6 +270,62 @@ class MavenPublishDependenciesIntegTest extends AbstractMavenPublishIntegTest { } } + @Issue("https://github.com/gradle/gradle/issues/26996") + def "BOMs are not published multiple times"() { + given: + mavenRepo.module("org", "foo").publish() + settingsFile << "rootProject.name = 'root'" + buildFile << """ + plugins { + id("java-library") + id("maven-publish") + } + + group = 'group' + version = '1.0' + + repositories { maven { url "${mavenRepo.uri}" } } + dependencies { + api platform("org:foo:1.0") + } + + publishing { + repositories { + maven { url "${mavenRepo.uri}" } + } + publications { + maven(MavenPublication) { + from components.java + } + } + } + """ + + when: + succeeds "publish" + + then: + repoModule.assertPublished() + def depMan = (repoModule.parsedPom.dependencyManagement.get("dependencies"))[0] as Node + depMan.children().size() == 1 + with(depMan.children().first()) { + groupId.text() == "org" + artifactId.text() == "foo" + version.text() == "1.0" + type.text() == "pom" + scope.text() == "import" + } + + ["apiElements", "runtimeElements"].each { + repoModule.parsedModuleMetadata.variant(it) { + dependency("org:foo:1.0") { + exists() + isLast() + } + } + } + } + def "dependencies with multiple dependency artifacts are mapped to multiple dependency declarations in GMM"() { given: settingsFile << "rootProject.name = 'root'" diff --git a/platforms/software/maven/src/main/java/org/gradle/api/publish/maven/internal/publication/MavenComponentParser.java b/platforms/software/maven/src/main/java/org/gradle/api/publish/maven/internal/publication/MavenComponentParser.java index a01d01ec43f69..20d534379440b 100644 --- a/platforms/software/maven/src/main/java/org/gradle/api/publish/maven/internal/publication/MavenComponentParser.java +++ b/platforms/software/maven/src/main/java/org/gradle/api/publish/maven/internal/publication/MavenComponentParser.java @@ -141,6 +141,7 @@ public ParsedDependencyResult parseDependencies( LOG, UNSUPPORTED_FEATURE, INCOMPATIBLE_FEATURE, PUBLICATION_WARNING_FOOTER, "suppressPomMetadataWarningsFor"); Set seenDependencies = Sets.newHashSet(); + Set seenPlatforms = Sets.newHashSet(); Set seenConstraints = Sets.newHashSet(); List dependencies = new ArrayList<>(); @@ -170,6 +171,11 @@ public ParsedDependencyResult parseDependencies( dependencies.add(dep); } }; + Consumer platformsAdder = dep -> { + if (seenPlatforms.add(MavenDependencyKey.of(dep))) { + platforms.add(dep); + } + }; for (ModuleDependency dependency : variant.getDependencies()) { if (isDependencyWithDefaultArtifact(dependency) && dependencyMatchesProject(dependency, coordinates)) { @@ -177,7 +183,7 @@ public ParsedDependencyResult parseDependencies( continue; } if (platformSupport.isTargetingPlatform(dependency)) { - dependencyFactory.convertImportDependencyConstraint(dependency, platforms::add); + dependencyFactory.convertImportDependencyConstraint(dependency, platformsAdder); } else { dependencyFactory.convertDependency(dependency, dependencyAdder); }