Skip to content

Commit

Permalink
Support latest Guava version published with Gradle Metadata (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Jul 1, 2023
1 parent 67c616b commit 38459dd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -31,10 +34,15 @@ abstract public class GuavaComponentRule implements ComponentMetadataRule {
private final static Attribute<String> TARGET_JVM_ENVIRONMENT_ATTRIBUTE =
Attribute.of("org.gradle.jvm.environment", String.class);

private final List<String> 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());
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("-")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
Expand Down Expand Up @@ -133,7 +130,6 @@ class GuavaClasspathTest extends Specification {
repositories {
mavenCentral()
${guavaVersion == devGuavaVersion? 'mavenLocal()' : ''}
}
val envAttribute = $attr
Expand Down Expand Up @@ -166,12 +162,12 @@ class GuavaClasspathTest extends Specification {

Set<String> expectedClasspath(String guavaVersion, String jvmEnv, String classpath, Map<String, String> 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<String> 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"
}
Expand All @@ -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 {
Expand All @@ -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
}
}
}

0 comments on commit 38459dd

Please sign in to comment.