Skip to content

Commit 7717dba

Browse files
authored
Fix Java 21 compatibilities (#1920)
2 parents 4297aa0 + c95574f commit 7717dba

File tree

22 files changed

+91
-37
lines changed

22 files changed

+91
-37
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ jobs:
4242
fail-fast: false
4343
matrix:
4444
kind: [maven, gradle]
45-
jre: [11, 17]
45+
# Test on the latest Java version once Gradle & Maven support it.
46+
jre: [11, 17, 21]
4647
os: [ubuntu-latest]
4748
include:
4849
# test windows at the diagonals of the above matrix

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Changes
14+
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
15+
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
1316

1417
## [2.43.0] - 2023-11-27
1518
### Added

gradle/wrapper/gradle-wrapper.jar

-19.8 KB
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ dependencies {
8484
gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
8585
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
8686
// googleJavaFormat
87-
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.17.0'
87+
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.18.1'
8888
// gson
8989
gsonCompileOnly 'com.google.code.gson:gson:2.10.1'
9090
// jackson

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private GoogleJavaFormatStep() {}
3535
private static final boolean DEFAULT_REORDER_IMPORTS = false;
3636
private static final boolean DEFAULT_FORMAT_JAVADOC = true;
3737
static final String NAME = "google-java-format";
38-
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
38+
public static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
3939

4040
/** Creates a step which formats everything - code, import order, and unused imports. */
4141
public static FormatterStep create(Provisioner provisioner) {
@@ -83,7 +83,7 @@ public static FormatterStep create(String groupArtifact, String version, String
8383
.addMin(11, "1.8") // we only support google-java-format >= 1.8 due to api changes
8484
.addMin(16, "1.10.0") // java 16 requires at least 1.10.0 due to jdk api changes in JavaTokenizer
8585
.addMin(21, "1.17.0") // java 21 requires at least 1.17.0 due to https://github.com/google/google-java-format/issues/898
86-
.add(11, "1.17.0"); // default version
86+
.add(11, "1.18.1"); // default version
8787

8888
public static String defaultGroupArtifact() {
8989
return MAVEN_COORDINATE;

lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ private PalantirJavaFormatStep() {}
2929

3030
private static final String DEFAULT_STYLE = "PALANTIR";
3131
private static final String NAME = "palantir-java-format";
32-
private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
33-
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0");
32+
public static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
33+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0").add(21, "2.38.0");
3434

3535
/** Creates a step which formats everything - code, import order, and unused imports. */
3636
public static FormatterStep create(Provisioner provisioner) {

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Changes
7+
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
8+
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
69
### Fixed
710
* Make `KtfmtConfig.ConfigurableStyle#configure` public. ([#1926](https://github.com/diffplug/spotless/pull/1926))
811
* **BREAKING CHANGE** `6.23.0` made breaking changes to the ABI of the `KotlinExtension` and `GroovyExtension`. Those are reflected retroactively now.

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleProvisioner.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.gradle.api.artifacts.Configuration;
2727
import org.gradle.api.artifacts.ConfigurationContainer;
2828
import org.gradle.api.artifacts.dsl.DependencyHandler;
29+
import org.gradle.api.attributes.Attribute;
2930
import org.gradle.api.attributes.Bundling;
3031
import org.gradle.api.attributes.Category;
3132
import org.gradle.api.initialization.dsl.ScriptHandler;
@@ -35,6 +36,8 @@
3536
import com.diffplug.common.base.Unhandled;
3637
import com.diffplug.common.collect.ImmutableList;
3738
import com.diffplug.spotless.Provisioner;
39+
import com.diffplug.spotless.java.GoogleJavaFormatStep;
40+
import com.diffplug.spotless.java.PalantirJavaFormatStep;
3841

3942
/** Should be package-private. */
4043
class GradleProvisioner {
@@ -116,14 +119,26 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
116119
+ new Request(withTransitives, mavenCoords).hashCode());
117120
mavenCoords.stream()
118121
.map(dependencies::create)
119-
.forEach(config.getDependencies()::add);
122+
.forEach(dependency -> {
123+
config.getDependencies().add(dependency);
124+
String coordinate = dependency.getGroup() + ":" + dependency.getName();
125+
if (coordinate.startsWith(GoogleJavaFormatStep.MAVEN_COORDINATE) ||
126+
coordinate.startsWith(PalantirJavaFormatStep.MAVEN_COORDINATE)) {
127+
// Use Guava 32.1.3, see https://github.com/google/guava/issues/6657.
128+
// TODO: May remove this after https://github.com/google/google-java-format/pull/996 and https://github.com/palantir/palantir-java-format/issues/957 are released.
129+
config.getDependencies().add(dependencies.create("com.google.guava:guava:32.1.3-jre"));
130+
}
131+
});
120132
config.setDescription(mavenCoords.toString());
121133
config.setTransitive(withTransitives);
122134
config.setCanBeConsumed(false);
123135
config.setVisible(false);
124136
config.attributes(attr -> {
125137
attr.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY));
126138
attr.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.EXTERNAL));
139+
// TODO: This is a copy-paste from org.gradle.api.attributes.java.TargetJvmEnvironment which is added in Gradle 7.0, remove this once we drop support for Gradle 6.x.
140+
// Add this attribute for resolving Guava dependency, see https://github.com/google/guava/issues/6801.
141+
attr.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm");
127142
});
128143
return config.resolve();
129144
} catch (Exception e) {

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.gradle.testkit.runner.BuildResult;
2121
import org.gradle.testkit.runner.GradleRunner;
2222
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.condition.EnabledForJreRange;
24+
import org.junit.jupiter.api.condition.JRE;
2325

2426
public class ConfigurationCacheTest extends GradleIntegrationHarness {
2527
@Override
@@ -63,6 +65,7 @@ public void helpConfiguresIfTasksAreCreated() throws IOException {
6365
}
6466

6567
@Test
68+
@EnabledForJreRange(max = JRE.JAVA_20)
6669
public void jvmLocalCache() throws IOException {
6770
setFile("build.gradle").toLines(
6871
"plugins {",
@@ -91,6 +94,10 @@ public void jvmLocalCache() throws IOException {
9194
gradleRunner().withArguments("spotlessApply").build();
9295
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test");
9396

97+
// the withDebug forces it to start a new deamon, but only in Gradle 8.3 and older
98+
// starting with Gradle 8.5 this doesn't work anymore
99+
// and we need Gradle 8.5 for Java 21
100+
// so we can't test this on Java 21 for now
94101
BuildResult failure = gradleRunner().withDebug(true).withArguments("spotlessApply", "--stacktrace").buildAndFail();
95102
failure.getOutput().contains("Spotless daemon-local cache is stale. Regenerate the cache with\n" +
96103
" rm -rf .gradle/configuration-cache\n" +

0 commit comments

Comments
 (0)