From 38459dd52b72db15626776967c02d3c0afb85fae Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Sat, 1 Jul 2023 09:01:33 +0200 Subject: [PATCH] Support latest Guava version published with Gradle Metadata (#56) See: https://github.com/google/guava/pull/3683 Fixes #54 --- .../componentrules/GuavaComponentRule.java | 16 ++++++++++--- .../rules/GoogleCollectionsRule.java | 11 +++++---- .../rules/GuavaListenableFutureRule.java | 19 +++++++++------ .../capabilities/rules/GuavaRule.java | 5 ++++ .../test/GuavaClasspathTest.groovy | 24 ++++--------------- 5 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/gradlex/javaecosystem/capabilities/componentrules/GuavaComponentRule.java b/src/main/java/org/gradlex/javaecosystem/capabilities/componentrules/GuavaComponentRule.java index a89ad10..317cbdd 100644 --- a/src/main/java/org/gradlex/javaecosystem/capabilities/componentrules/GuavaComponentRule.java +++ b/src/main/java/org/gradlex/javaecosystem/capabilities/componentrules/GuavaComponentRule.java @@ -23,6 +23,9 @@ import org.gradle.api.artifacts.VariantMetadata; import org.gradle.api.attributes.Attribute; +import java.util.Arrays; +import java.util.List; + @CacheableRule abstract public class GuavaComponentRule implements ComponentMetadataRule { @@ -31,10 +34,15 @@ abstract public class GuavaComponentRule implements ComponentMetadataRule { private final static Attribute TARGET_JVM_ENVIRONMENT_ATTRIBUTE = Attribute.of("org.gradle.jvm.environment", String.class); + private final List RUNTIME_VARIANT_NAMES = + Arrays.asList("runtime", "androidRuntimeElements", "jreRuntimeElements"); + public void execute(ComponentMetadataContext ctx) { + int majorVersion = getMajorVersion(ctx.getDetails()); + // if (majorVersion <= 32) // May add this check should https://github.com/google/guava/pull/6606 be done removeAnnotationProcessorDependenciesFromRuntime(ctx.getDetails()); - if (getMajorVersion(ctx.getDetails()) >= 22) { + if ((majorVersion >= 22 && majorVersion <= 31) || ctx.getDetails().getId().getVersion().startsWith("32.0")) { removeAnimalSnifferAnnotations(ctx.getDetails()); addOtherJvmVariant("Compile", ctx.getDetails()); @@ -51,8 +59,10 @@ private void removeAnimalSnifferAnnotations(ComponentMetadataDetails details) { private void removeAnnotationProcessorDependenciesFromRuntime(ComponentMetadataDetails details) { // everything outside the 'com.google.guava' group is an annotation processor String guavaGroup = details.getId().getGroup(); - details.withVariant("runtime", variant -> variant.withDependencies(dependencies -> - dependencies.removeIf(dependency -> !guavaGroup.equals(dependency.getGroup())))); + for (String runtime : RUNTIME_VARIANT_NAMES) { + details.withVariant(runtime, variant -> variant.withDependencies(dependencies -> + dependencies.removeIf(dependency -> !guavaGroup.equals(dependency.getGroup())))); + } } private boolean isAndroidVariantVersion(ComponentMetadataDetails details) { diff --git a/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GoogleCollectionsRule.java b/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GoogleCollectionsRule.java index cbc19fb..74ef86b 100644 --- a/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GoogleCollectionsRule.java +++ b/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GoogleCollectionsRule.java @@ -21,6 +21,7 @@ import org.gradle.api.artifacts.ComponentMetadataContext; import org.gradle.api.artifacts.ComponentMetadataRule; +import static org.gradlex.javaecosystem.capabilities.rules.GuavaRule.parseGuavaMajorVersion; import static org.gradlex.javaecosystem.capabilities.rules.GuavaRule.parseGuavaVersion; @CacheableRule @@ -37,9 +38,11 @@ public abstract class GoogleCollectionsRule implements ComponentMetadataRule { @Override public void execute(ComponentMetadataContext context) { - String version = parseGuavaVersion(context.getDetails()); - context.getDetails().allVariants(variant -> variant.withCapabilities(capabilities -> capabilities.addCapability( - CAPABILITY_GROUP, CAPABILITY_NAME, version - ))); + int version = parseGuavaMajorVersion(context.getDetails()); + if (version <= 31 || context.getDetails().getId().getVersion().startsWith("32.0")) { + context.getDetails().allVariants(variant -> variant.withCapabilities(capabilities -> capabilities.addCapability( + CAPABILITY_GROUP, CAPABILITY_NAME, parseGuavaVersion(context.getDetails()) + ))); + } } } diff --git a/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaListenableFutureRule.java b/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaListenableFutureRule.java index 24f85f3..1abb66d 100644 --- a/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaListenableFutureRule.java +++ b/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaListenableFutureRule.java @@ -21,6 +21,8 @@ import org.gradle.api.artifacts.ComponentMetadataContext; import org.gradle.api.artifacts.ComponentMetadataRule; +import static org.gradlex.javaecosystem.capabilities.rules.GuavaRule.parseGuavaMajorVersion; + @CacheableRule @NonNullApi public abstract class GuavaListenableFutureRule implements ComponentMetadataRule { @@ -35,12 +37,15 @@ public abstract class GuavaListenableFutureRule implements ComponentMetadataRule @Override public void execute(ComponentMetadataContext context) { - context.getDetails().allVariants(variant -> { - // Remove workaround dependency to '9999.0-empty-to-avoid-conflict-with-guava' - variant.withDependencies(dependencies -> dependencies.removeIf(d -> CAPABILITY_NAME.equals(d.getName()))); - variant.withCapabilities(capabilities -> capabilities.addCapability( - CAPABILITY_GROUP, CAPABILITY_NAME, "1.0" - )); - }); + int version = parseGuavaMajorVersion(context.getDetails()); + if (version <= 31 || context.getDetails().getId().getVersion().startsWith("32.0")) { + context.getDetails().allVariants(variant -> { + // Remove workaround dependency to '9999.0-empty-to-avoid-conflict-with-guava' + variant.withDependencies(dependencies -> dependencies.removeIf(d -> CAPABILITY_NAME.equals(d.getName()))); + variant.withCapabilities(capabilities -> capabilities.addCapability( + CAPABILITY_GROUP, CAPABILITY_NAME, "1.0" + )); + }); + } } } diff --git a/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaRule.java b/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaRule.java index 228c6c6..45ba3d8 100644 --- a/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaRule.java +++ b/src/main/java/org/gradlex/javaecosystem/capabilities/rules/GuavaRule.java @@ -42,6 +42,11 @@ public void execute(ComponentMetadataContext context) { ))); } + static int parseGuavaMajorVersion(ComponentMetadataDetails details) { + String version = parseGuavaVersion(details); + return Integer.parseInt(version.substring(0, version.indexOf("."))); + } + static String parseGuavaVersion(ComponentMetadataDetails details) { String versionString = details.getId().getVersion(); if (!versionString.contains("-")) { diff --git a/src/test/groovy/org/gradlex/javaecosystem/capabilities/test/GuavaClasspathTest.groovy b/src/test/groovy/org/gradlex/javaecosystem/capabilities/test/GuavaClasspathTest.groovy index ce25e62..de53bc9 100644 --- a/src/test/groovy/org/gradlex/javaecosystem/capabilities/test/GuavaClasspathTest.groovy +++ b/src/test/groovy/org/gradlex/javaecosystem/capabilities/test/GuavaClasspathTest.groovy @@ -10,16 +10,13 @@ class GuavaClasspathTest extends Specification { GradleBuild build = new GradleBuild() def setup() { - // buildNextGuavaVersion() // -- enable to test with https://github.com/google/guava/pull/3683 settingsFile << 'rootProject.name = "test-project"' } - static String devGuavaVersion = '02.1' - static allGuavaVersions() { [ - // [devGuavaVersion, 'jre' , [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], - // [devGuavaVersion, 'android', [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], + ['32.1.1', 'jre' , [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], + ['32.1.1', 'android', [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], ['32.0.1', 'jre' , [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], ['32.0.1', 'android', [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], ['32.0.0', 'jre' , [errorProne: '2.18.0', j2objc: '2.8', jsr305: '3.0.2', checker: '3.33.0', failureaccess: '1.0.1']], @@ -133,7 +130,6 @@ class GuavaClasspathTest extends Specification { repositories { mavenCentral() - ${guavaVersion == devGuavaVersion? 'mavenLocal()' : ''} } val envAttribute = $attr @@ -166,12 +162,12 @@ class GuavaClasspathTest extends Specification { Set expectedClasspath(String guavaVersion, String jvmEnv, String classpath, Map dependencyVersions) { int majorGuavaVersion = guavaVersion.substring(0, 2) as Integer - String jarSuffix = majorGuavaVersion < 22 && guavaVersion != devGuavaVersion ? '' : jvmEnv == 'android' ? 'android' : (guavaVersion == '22.0' || guavaVersion == '23.0') ? '' : 'jre' + String jarSuffix = majorGuavaVersion < 22 ? '' : jvmEnv == 'android' ? 'android' : (guavaVersion == '22.0' || guavaVersion == '23.0') ? '' : 'jre' Set result = ["guava-${guavaVersion}${jarSuffix? '-' : ''}${jarSuffix}.jar"] if (dependencyVersions.failureaccess) { result += "failureaccess-${dependencyVersions.failureaccess}.jar" } - if (classpath == 'compileClasspath' || guavaVersion == devGuavaVersion) { // Guava itself is planing to be more conservative with reducing the runtime classpath, so 'devGuavaVersion' has more entries right now + if (classpath == 'compileClasspath') { if (classpath == 'compileClasspath' && dependencyVersions.j2objc) { result += "j2objc-annotations-${dependencyVersions.j2objc}.jar" } @@ -184,7 +180,7 @@ class GuavaClasspathTest extends Specification { if (dependencyVersions.checker && dependencyVersions.checkerCompat) { if (jvmEnv == 'android') { result += "checker-compat-qual-${dependencyVersions.checkerCompat}.jar" - if (majorGuavaVersion > 30 || guavaVersion == devGuavaVersion) { + if (majorGuavaVersion > 30) { result += "checker-qual-${dependencyVersions.checker}.jar" } } else { @@ -198,14 +194,4 @@ class GuavaClasspathTest extends Specification { } return result } - - void buildNextGuavaVersion() { - def guavaDir = new File('build/guava') - if (!guavaDir.exists()) { - print "git clone --depth 1 https://github.com/jjohannes/guava.git -b gradle-module-metadata".execute(null, guavaDir.parentFile).text - print "util/set_version.sh $devGuavaVersion".execute(null, guavaDir).text - print "mvn clean install -DskipTests".execute(null, guavaDir).text - print "mvn clean install -DskipTests".execute(null, new File(guavaDir, 'android')).text - } - } } \ No newline at end of file