From 5b11b87a109659c0892c43d6849c9c82396179e9 Mon Sep 17 00:00:00 2001 From: Chengyuan Zhang Date: Wed, 21 Apr 2021 01:42:23 -0700 Subject: [PATCH] Make consumer dependency variant selection criteria tighter by adding usage attribute to CompileProtoPath configuration. This can reduce the chance of getting ambiguous variant selection error. --- .../protobuf/gradle/ProtobufPlugin.groovy | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index 2a31bdfd..91ba8321 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -40,6 +40,7 @@ import org.gradle.api.Task import org.gradle.api.artifacts.Configuration import org.gradle.api.attributes.Attribute import org.gradle.api.attributes.LibraryElements +import org.gradle.api.attributes.Usage import org.gradle.api.file.FileCollection import org.gradle.api.file.SourceDirectorySet import org.gradle.api.internal.file.FileResolver @@ -179,7 +180,7 @@ class ProtobufPlugin implements Plugin { * Creates an internal 'compileProtoPath' configuration for the given source set that extends * compilation configurations as a bucket of dependencies with resources attribute. * The extract-include-protos task of each source set will extract protobuf files from - * dependencies in this configuration. + * resolved dependencies in this configuration. * *

For Java projects only. *

This works around 'java-library' plugin not exposing resources to consumers for compilation. @@ -196,8 +197,22 @@ class ProtobufPlugin implements Plugin { transitive = true extendsFrom = [compileConfig, implementationConfig] canBeConsumed = false - }.getAttributes().attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, - project.getObjects().named(LibraryElements, LibraryElements.RESOURCES)) + }.getAttributes() + // Variant attributes are not inherited. Setting it too loosely can + // result in ambiguous variant selection errors. + // CompileProtoPath only need proto files from dependency's resources. + // LibraryElement "resources" is compatible with "jar" (if a "resources" variant is + // not found, the "jar" variant will be used). + .attribute( + LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, + project.getObjects().named(LibraryElements, LibraryElements.RESOURCES)) + // Although variants with any usage has proto files, not setting usage attribute + // can result in ambiguous variant selection if the producer provides multiple + // variants with different usage attribute. + // Preserve the usage attribute from CompileOnly and Implementation. + .attribute( + Usage.USAGE_ATTRIBUTE, + project.getObjects().named(Usage, Usage.JAVA_RUNTIME)) } }