Skip to content

Commit

Permalink
Allow selection of both the library and platform of a component
Browse files Browse the repository at this point in the history
For published POM files, we want to allow selecting both the
library and platform variants of a component. They are effectively
different things. This commit makes it possible by assigning a
different capability to the platform variants.

Fixes #8420
  • Loading branch information
melix committed Feb 6, 2019
1 parent 1a31151 commit 25e1b3a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
Expand Up @@ -19,7 +19,7 @@ package org.gradle.integtests.resolve.maven
import org.gradle.integtests.fixtures.AbstractHttpDependencyResolutionTest
import org.gradle.integtests.fixtures.resolve.ResolveTestFixture
import org.gradle.test.fixtures.maven.MavenModule
import spock.lang.Ignore
import spock.lang.Issue

class MavenBomResolveIntegrationTest extends AbstractHttpDependencyResolutionTest {
def resolve = new ResolveTestFixture(buildFile).expectDefaultConfiguration('runtime')
Expand Down Expand Up @@ -137,8 +137,8 @@ class MavenBomResolveIntegrationTest extends AbstractHttpDependencyResolutionTes
}
}

@Ignore("This isn't true anymore: a POM is either a platform (BOM) or a library")
def "a bom can declare dependencies"() {
@Issue("gradle/gradle#8420")
def "can depend on both platform and library if a published POM represents both of them"() {
given:
mavenHttpRepo.module('group', 'moduleC', '1.0').allowAll().publish()
bomDependency('moduleA')
Expand All @@ -156,7 +156,8 @@ class MavenBomResolveIntegrationTest extends AbstractHttpDependencyResolutionTes

buildFile << """
dependencies {
compile "group:bom:1.0"
compile platform("group:bom:1.0") // dependency on the platform
compile "group:bom:1.0" // dependency on library
compile "group:moduleA"
}
"""
Expand All @@ -168,7 +169,12 @@ class MavenBomResolveIntegrationTest extends AbstractHttpDependencyResolutionTes
resolve.expectGraph {
root(':', ':testproject:') {
module("group:bom:1.0") {
module("group:moduleA:2.0")
variant("platform-runtime", ['org.gradle.component.category':'platform', 'org.gradle.status':'release', 'org.gradle.usage':'java-runtime'])
constraint("group:moduleA:2.0")
noArtifacts()
}
module("group:bom:1.0") {
variant("runtime", ['org.gradle.component.category':'library', 'org.gradle.status':'release', 'org.gradle.usage':'java-runtime'])
module("group:moduleC:1.0")
noArtifacts()
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableSet;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.capabilities.CapabilitiesMetadata;
import org.gradle.api.capabilities.Capability;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.internal.Factory;
import org.gradle.internal.component.model.DependencyMetadata;
Expand Down Expand Up @@ -86,6 +87,24 @@ private DefaultConfigurationMetadata(ModuleComponentIdentifier componentId, Stri
this.dependencyFilter = dependencyFilter;
}

private DefaultConfigurationMetadata(ModuleComponentIdentifier componentId,
String name,
boolean transitive,
boolean visible,
ImmutableSet<String> hierarchy,
ImmutableList<? extends ModuleComponentArtifactMetadata> artifacts,
VariantMetadataRules componentMetadataRules,
ImmutableList<ExcludeMetadata> excludes,
ImmutableAttributes attributes,
Factory<List<ModuleDependencyMetadata>> configDependenciesFactory,
DependencyFilter dependencyFilter,
List<Capability> capabilities) {
super(componentId, name, transitive, visible, artifacts, hierarchy, excludes, attributes, configDependenciesFactory, ImmutableCapabilities.of(capabilities));
this.componentMetadataRules = componentMetadataRules;
this.componentLevelAttributes = attributes;
this.dependencyFilter = dependencyFilter;
}

@Override
public ImmutableAttributes getAttributes() {
if (computedAttributes == null) {
Expand Down Expand Up @@ -165,6 +184,10 @@ public DefaultConfigurationMetadata withForcedDependencies() {
return new DefaultConfigurationMetadata(getComponentId(), getName(), isTransitive(), isVisible(), getHierarchy(), getArtifacts(), componentMetadataRules, getExcludes(), componentLevelAttributes, lazyConfigDependencies(), dependencyFilter.forcing());
}

public DefaultConfigurationMetadata withCapabilities(List<Capability> capabilities) {
return new DefaultConfigurationMetadata(getComponentId(), getName(), isTransitive(), isVisible(), getHierarchy(), getArtifacts(), componentMetadataRules, getExcludes(), super.getAttributes(), lazyConfigDependencies(), dependencyFilter, capabilities);
}

private ImmutableList<ModuleDependencyMetadata> force(ImmutableList<ModuleDependencyMetadata> configDependencies) {
ImmutableList.Builder<ModuleDependencyMetadata> dependencies = new ImmutableList.Builder<ModuleDependencyMetadata>();
for (ModuleDependencyMetadata configDependency : configDependencies) {
Expand Down
Expand Up @@ -16,12 +16,15 @@
package org.gradle.internal.component.external.model;

import com.google.common.collect.ImmutableList;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.attributes.Usage;
import org.gradle.api.internal.artifacts.repositories.metadata.MavenImmutableAttributesFactory;
import org.gradle.api.internal.attributes.ImmutableAttributes;
import org.gradle.internal.component.external.model.maven.DefaultMavenModuleResolveMetadata;
import org.gradle.internal.component.model.ConfigurationMetadata;

import java.util.Collections;

public class JavaEcosystemVariantDerivationStrategy implements VariantDerivationStrategy {
@Override
public boolean derivesVariants() {
Expand All @@ -37,12 +40,12 @@ public ImmutableList<? extends ConfigurationMetadata> derive(ModuleComponentReso
DefaultConfigurationMetadata compileConfiguration = (DefaultConfigurationMetadata) md.getConfiguration("compile");
DefaultConfigurationMetadata runtimeConfiguration = (DefaultConfigurationMetadata) md.getConfiguration("runtime");
return ImmutableList.of(
libraryWithUsageAttribute(compileConfiguration, attributes, attributesFactory, Usage.JAVA_API),
libraryWithUsageAttribute(runtimeConfiguration, attributes, attributesFactory, Usage.JAVA_RUNTIME),
platformWithUsageAttribute(compileConfiguration, attributes, attributesFactory, Usage.JAVA_API, false),
platformWithUsageAttribute(runtimeConfiguration, attributes, attributesFactory, Usage.JAVA_RUNTIME, false),
platformWithUsageAttribute(compileConfiguration, attributes, attributesFactory, Usage.JAVA_API, true),
platformWithUsageAttribute(runtimeConfiguration, attributes, attributesFactory, Usage.JAVA_RUNTIME, true));
libraryWithUsageAttribute(compileConfiguration, attributes, attributesFactory, Usage.JAVA_API),
libraryWithUsageAttribute(runtimeConfiguration, attributes, attributesFactory, Usage.JAVA_RUNTIME),
platformWithUsageAttribute(compileConfiguration, attributes, attributesFactory, Usage.JAVA_API, false),
platformWithUsageAttribute(runtimeConfiguration, attributes, attributesFactory, Usage.JAVA_RUNTIME, false),
platformWithUsageAttribute(compileConfiguration, attributes, attributesFactory, Usage.JAVA_API, true),
platformWithUsageAttribute(runtimeConfiguration, attributes, attributesFactory, Usage.JAVA_RUNTIME, true));
}
return null;
}
Expand All @@ -54,9 +57,16 @@ private static ConfigurationMetadata libraryWithUsageAttribute(DefaultConfigurat

private static ConfigurationMetadata platformWithUsageAttribute(DefaultConfigurationMetadata conf, ImmutableAttributes originAttributes, MavenImmutableAttributesFactory attributesFactory, String usage, boolean enforcedPlatform) {
ImmutableAttributes attributes = attributesFactory.platformWithUsage(originAttributes, usage, enforcedPlatform);
ModuleComponentIdentifier componentId = conf.getComponentId();
String prefix = enforcedPlatform ? "enforced-platform-" : "platform-";
DefaultConfigurationMetadata metadata = conf.withAttributes(prefix + conf.getName(), attributes);
metadata = metadata.withConstraintsOnly();
metadata = metadata
.withConstraintsOnly()
.withCapabilities(Collections.singletonList(new ImmutableCapability(
componentId.getGroup(),
componentId.getModule() + "-derived-platform",
componentId.getVersion()
)));
if (enforcedPlatform) {
metadata = metadata.withForcedDependencies();
}
Expand Down

0 comments on commit 25e1b3a

Please sign in to comment.