Search in sources :

Example 1 with NoMatchingCapabilitiesException

use of org.gradle.internal.component.NoMatchingCapabilitiesException in project gradle by gradle.

the class AttributeConfigurationSelector method selectConfigurationUsingAttributeMatching.

private static ConfigurationMetadata selectConfigurationUsingAttributeMatching(ImmutableAttributes consumerAttributes, Collection<? extends Capability> explicitRequestedCapabilities, ComponentResolveMetadata targetComponent, AttributesSchemaInternal consumerSchema, List<IvyArtifactName> requestedArtifacts, AttributeMatchingExplanationBuilder explanationBuilder) {
    Optional<ImmutableList<? extends ConfigurationMetadata>> variantsForGraphTraversal = targetComponent.getVariantsForGraphTraversal();
    ImmutableList<? extends ConfigurationMetadata> consumableConfigurations = variantsForGraphTraversal.or(ImmutableList.of());
    AttributesSchemaInternal producerAttributeSchema = targetComponent.getAttributesSchema();
    AttributeMatcher attributeMatcher = consumerSchema.withProducer(producerAttributeSchema);
    ConfigurationMetadata fallbackConfiguration = targetComponent.getConfiguration(Dependency.DEFAULT_CONFIGURATION);
    if (fallbackConfiguration != null && !fallbackConfiguration.isCanBeConsumed()) {
        fallbackConfiguration = null;
    }
    ModuleVersionIdentifier versionId = targetComponent.getModuleVersionId();
    if (!consumableConfigurations.isEmpty()) {
        ImmutableList<ConfigurationMetadata> variantsProvidingRequestedCapabilities = filterVariantsByRequestedCapabilities(targetComponent, explicitRequestedCapabilities, consumableConfigurations, versionId.getGroup(), versionId.getName(), true, explanationBuilder);
        if (variantsProvidingRequestedCapabilities.isEmpty()) {
            throw new NoMatchingCapabilitiesException(targetComponent, explicitRequestedCapabilities, consumableConfigurations);
        }
        consumableConfigurations = variantsProvidingRequestedCapabilities;
    }
    List<ConfigurationMetadata> matches = attributeMatcher.matches(consumableConfigurations, consumerAttributes, fallbackConfiguration, explanationBuilder);
    if (matches.size() > 1) {
        // there's an ambiguity, but we may have several variants matching the requested capabilities.
        // Here we're going to check if in the candidates, there's a single one _strictly_ matching the requested capabilities.
        List<ConfigurationMetadata> strictlyMatchingCapabilities = filterVariantsByRequestedCapabilities(targetComponent, explicitRequestedCapabilities, matches, versionId.getGroup(), versionId.getName(), false, explanationBuilder);
        if (strictlyMatchingCapabilities.size() == 1) {
            return singleVariant(variantsForGraphTraversal, strictlyMatchingCapabilities);
        } else if (strictlyMatchingCapabilities.size() > 1) {
            // there are still more than one candidate, but this time we know only a subset strictly matches the required attributes
            // so we perform another round of selection on the remaining candidates
            strictlyMatchingCapabilities = attributeMatcher.matches(strictlyMatchingCapabilities, consumerAttributes, fallbackConfiguration, explanationBuilder);
            if (strictlyMatchingCapabilities.size() == 1) {
                return singleVariant(variantsForGraphTraversal, strictlyMatchingCapabilities);
            }
        }
        if (requestedArtifacts.size() == 1) {
            // Here, we know that the user requested a specific classifier. There may be multiple
            // candidate variants left, but maybe only one of them provides the classified artifact
            // we're looking for.
            String classifier = requestedArtifacts.get(0).getClassifier();
            if (classifier != null) {
                List<ConfigurationMetadata> sameClassifier = findVariantsProvidingExactlySameClassifier(matches, classifier);
                if (sameClassifier != null && sameClassifier.size() == 1) {
                    return singleVariant(variantsForGraphTraversal, sameClassifier);
                }
            }
        }
    }
    if (matches.size() == 1) {
        return singleVariant(variantsForGraphTraversal, matches);
    } else if (!matches.isEmpty()) {
        AttributeDescriber describer = DescriberSelector.selectDescriber(consumerAttributes, consumerSchema);
        if (explanationBuilder instanceof TraceDiscardedConfigurations) {
            Set<ConfigurationMetadata> discarded = Cast.uncheckedCast(((TraceDiscardedConfigurations) explanationBuilder).discarded);
            throw new AmbiguousConfigurationSelectionException(describer, consumerAttributes, attributeMatcher, matches, targetComponent, variantsForGraphTraversal.isPresent(), discarded);
        } else {
            // Perform a second resolution with tracing
            return selectConfigurationUsingAttributeMatching(consumerAttributes, explicitRequestedCapabilities, targetComponent, consumerSchema, requestedArtifacts, new TraceDiscardedConfigurations());
        }
    } else {
        AttributeDescriber describer = DescriberSelector.selectDescriber(consumerAttributes, consumerSchema);
        throw new NoMatchingConfigurationSelectionException(describer, consumerAttributes, attributeMatcher, targetComponent, variantsForGraphTraversal.isPresent());
    }
}
Also used : Set(java.util.Set) AmbiguousConfigurationSelectionException(org.gradle.internal.component.AmbiguousConfigurationSelectionException) ImmutableList(com.google.common.collect.ImmutableList) NoMatchingCapabilitiesException(org.gradle.internal.component.NoMatchingCapabilitiesException) AttributesSchemaInternal(org.gradle.api.internal.attributes.AttributesSchemaInternal) NoMatchingConfigurationSelectionException(org.gradle.internal.component.NoMatchingConfigurationSelectionException) ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) AttributeDescriber(org.gradle.api.internal.attributes.AttributeDescriber)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Set (java.util.Set)1 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)1 AttributeDescriber (org.gradle.api.internal.attributes.AttributeDescriber)1 AttributesSchemaInternal (org.gradle.api.internal.attributes.AttributesSchemaInternal)1 AmbiguousConfigurationSelectionException (org.gradle.internal.component.AmbiguousConfigurationSelectionException)1 NoMatchingCapabilitiesException (org.gradle.internal.component.NoMatchingCapabilitiesException)1 NoMatchingConfigurationSelectionException (org.gradle.internal.component.NoMatchingConfigurationSelectionException)1