Skip to content

Commit

Permalink
Fix Java 21 compatibilities (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Dec 3, 2023
2 parents 4297aa0 + c95574f commit 7717dba
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ jobs:
fail-fast: false
matrix:
kind: [maven, gradle]
jre: [11, 17]
# Test on the latest Java version once Gradle & Maven support it.
jre: [11, 17, 21]
os: [ubuntu-latest]
include:
# test windows at the diagonals of the above matrix
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

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

## [2.43.0] - 2023-11-27
### Added
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dependencies {
gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
// googleJavaFormat
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.17.0'
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.18.1'
// gson
gsonCompileOnly 'com.google.code.gson:gson:2.10.1'
// jackson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private GoogleJavaFormatStep() {}
private static final boolean DEFAULT_REORDER_IMPORTS = false;
private static final boolean DEFAULT_FORMAT_JAVADOC = true;
static final String NAME = "google-java-format";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
public static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";

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

public static String defaultGroupArtifact() {
return MAVEN_COORDINATE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ private PalantirJavaFormatStep() {}

private static final String DEFAULT_STYLE = "PALANTIR";
private static final String NAME = "palantir-java-format";
private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.28.0");
public static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:";
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");

/** Creates a step which formats everything - code, import order, and unused imports. */
public static FormatterStep create(Provisioner provisioner) {
Expand Down
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Changes
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
### Fixed
* Make `KtfmtConfig.ConfigurableStyle#configure` public. ([#1926](https://github.com/diffplug/spotless/pull/1926))
* **BREAKING CHANGE** `6.23.0` made breaking changes to the ABI of the `KotlinExtension` and `GroovyExtension`. Those are reflected retroactively now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.Bundling;
import org.gradle.api.attributes.Category;
import org.gradle.api.initialization.dsl.ScriptHandler;
Expand All @@ -35,6 +36,8 @@
import com.diffplug.common.base.Unhandled;
import com.diffplug.common.collect.ImmutableList;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.java.GoogleJavaFormatStep;
import com.diffplug.spotless.java.PalantirJavaFormatStep;

/** Should be package-private. */
class GradleProvisioner {
Expand Down Expand Up @@ -116,14 +119,26 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
+ new Request(withTransitives, mavenCoords).hashCode());
mavenCoords.stream()
.map(dependencies::create)
.forEach(config.getDependencies()::add);
.forEach(dependency -> {
config.getDependencies().add(dependency);
String coordinate = dependency.getGroup() + ":" + dependency.getName();
if (coordinate.startsWith(GoogleJavaFormatStep.MAVEN_COORDINATE) ||
coordinate.startsWith(PalantirJavaFormatStep.MAVEN_COORDINATE)) {
// Use Guava 32.1.3, see https://github.com/google/guava/issues/6657.
// 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.
config.getDependencies().add(dependencies.create("com.google.guava:guava:32.1.3-jre"));
}
});
config.setDescription(mavenCoords.toString());
config.setTransitive(withTransitives);
config.setCanBeConsumed(false);
config.setVisible(false);
config.attributes(attr -> {
attr.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, Category.LIBRARY));
attr.attribute(Bundling.BUNDLING_ATTRIBUTE, project.getObjects().named(Bundling.class, Bundling.EXTERNAL));
// 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.
// Add this attribute for resolving Guava dependency, see https://github.com/google/guava/issues/6801.
attr.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm");
});
return config.resolve();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

public class ConfigurationCacheTest extends GradleIntegrationHarness {
@Override
Expand Down Expand Up @@ -63,6 +65,7 @@ public void helpConfiguresIfTasksAreCreated() throws IOException {
}

@Test
@EnabledForJreRange(max = JRE.JAVA_20)
public void jvmLocalCache() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
Expand Down Expand Up @@ -91,6 +94,10 @@ public void jvmLocalCache() throws IOException {
gradleRunner().withArguments("spotlessApply").build();
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test");

// the withDebug forces it to start a new deamon, but only in Gradle 8.3 and older
// starting with Gradle 8.5 this doesn't work anymore
// and we need Gradle 8.5 for Java 21
// so we can't test this on Java 21 for now
BuildResult failure = gradleRunner().withDebug(true).withArguments("spotlessApply", "--stacktrace").buildAndFail();
failure.getOutput().contains("Spotless daemon-local cache is stale. Regenerate the cache with\n" +
" rm -rf .gradle/configuration-cache\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void integration() throws IOException {
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.10.0')",
" googleJavaFormat('1.17.0')",
" }",
"}");

Expand All @@ -41,7 +41,7 @@ void integration() throws IOException {

checkRunsThenUpToDate();
replace("build.gradle",
"googleJavaFormat('1.10.0')",
"googleJavaFormat('1.17.0')",
"googleJavaFormat()");
checkRunsThenUpToDate();
}
Expand All @@ -57,7 +57,7 @@ void integrationWithReorderImports() throws IOException {
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.12.0').aosp().reorderImports(true)",
" googleJavaFormat('1.17.0').aosp().reorderImports(true)",
" }",
"}");

Expand All @@ -67,7 +67,7 @@ void integrationWithReorderImports() throws IOException {

checkRunsThenUpToDate();
replace("build.gradle",
"googleJavaFormat('1.12.0')",
"googleJavaFormat('1.17.0')",
"googleJavaFormat()");
checkRunsThenUpToDate();
}
Expand All @@ -83,7 +83,7 @@ void integrationWithSkipJavadocFormatting() throws IOException {
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.12.0').skipJavadocFormatting()",
" googleJavaFormat('1.17.0').skipJavadocFormatting()",
" }",
"}");

Expand All @@ -93,7 +93,7 @@ void integrationWithSkipJavadocFormatting() throws IOException {

checkRunsThenUpToDate();
replace("build.gradle",
"googleJavaFormat('1.12.0')",
"googleJavaFormat('1.17.0')",
"googleJavaFormat()");
checkRunsThenUpToDate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ public enum GradleVersionSupport {
GradleVersionSupport(String version) {
String minVersionForRunningJRE;
switch (Jvm.version()) {
case 22:
// TODO: https://docs.gradle.org/current/userguide/compatibility.html
case 21:
minVersionForRunningJRE = "8.5";
break;
case 20:
// TODO: https://docs.gradle.org/current/userguide/compatibility.html
minVersionForRunningJRE = "8.3";
break;
case 19:
minVersionForRunningJRE = "7.6";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void createSubproject(String name) throws IOException {
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.16.0')",
" googleJavaFormat('1.17.0')",
" }",
"}");
setFile(name + "/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
Expand All @@ -71,7 +71,7 @@ public void hasRootSpotless() throws IOException {
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.16.0')",
" googleJavaFormat('1.17.0')",
" }",
"}");
setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
Expand All @@ -88,7 +88,7 @@ public void predeclaredFails() throws IOException {
"spotless { predeclareDeps() }");
createNSubprojects();
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())
.contains("Add a step with [com.google.googlejavaformat:google-java-format:1.16.0] into the `spotlessPredeclare` block in the root project.");
.contains("Add a step with [com.google.googlejavaformat:google-java-format:1.17.0] into the `spotlessPredeclare` block in the root project.");
}

@Test
Expand All @@ -100,7 +100,7 @@ public void predeclaredSucceeds() throws IOException {
"repositories { mavenCentral() }",
"spotless { predeclareDeps() }",
"spotlessPredeclare {",
" java { googleJavaFormat('1.16.0') }",
" java { googleJavaFormat('1.17.0') }",
"}");
createNSubprojects();
gradleRunner().withArguments("spotlessApply").build();
Expand All @@ -115,7 +115,7 @@ public void predeclaredFromBuildscriptSucceeds() throws IOException {
"repositories { mavenCentral() }",
"spotless { predeclareDepsFromBuildscript() }",
"spotlessPredeclare {",
" java { googleJavaFormat('1.16.0') }",
" java { googleJavaFormat('1.17.0') }",
"}");
createNSubprojects();
gradleRunner().withArguments("spotlessApply").build();
Expand All @@ -129,7 +129,7 @@ public void predeclaredOrdering() throws IOException {
"}",
"repositories { mavenCentral() }",
"spotlessPredeclare {",
" java { googleJavaFormat('1.16.0') }",
" java { googleJavaFormat('1.17.0') }",
"}",
"spotless { predeclareDepsFromBuildscript() }");
createNSubprojects();
Expand All @@ -145,7 +145,7 @@ public void predeclaredUndeclared() throws IOException {
"}",
"repositories { mavenCentral() }",
"spotlessPredeclare {",
" java { googleJavaFormat('1.16.0') }",
" java { googleJavaFormat('1.17.0') }",
"}");
createNSubprojects();
Assertions.assertThat(gradleRunner().withArguments("spotlessApply").buildAndFail().getOutput())
Expand Down
6 changes: 5 additions & 1 deletion plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changes
* Use palantir-java-format 2.38.0 on Java 21. ([#1920](https://github.com/diffplug/spotless/pull/1920))
* Bump default `googleJavaFormat` version to latest `1.17.0` -> `1.18.1`. ([#1920](https://github.com/diffplug/spotless/pull/1920))
### Fixed
* Revert [#1846](https://github.com/diffplug/spotless/issues/1846) from 2.41.0 which causes the plugin to format generated sources in the `target` directory. ([#1928](https://github.com/diffplug/spotless/pull/1928))

## [2.41.0] - 2023-11-27
### Added
* CompileSourceRoots and TestCompileSourceRoots are now respected as default includes. These properties are commonly set when adding extra source directories. ([#1846](https://github.com/diffplug/spotless/issues/1846))
* ~~CompileSourceRoots and TestCompileSourceRoots are now respected as default includes. These properties are commonly set when adding extra source directories.~~ ([#1846](https://github.com/diffplug/spotless/issues/1846))
* Reverted in the next release (`2.41.1`) due to backward compatibility problems, see [#1914](https://github.com/diffplug/spotless/issues/1914).
* Support custom rule sets for Ktlint. ([#1896](https://github.com/diffplug/spotless/pull/1896))
### Fixed
* Fix crash when build dir is a softlink to another directory. ([#1859](https://github.com/diffplug/spotless/pull/1859))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void testMultipleDependenciesExcludingTransitives() throws Exception {
void testSingleDependencyIncludingTransitives() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.10.0</version>",
"</googleJavaFormat>");
assertResolveDependenciesWorks();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private void integration(String patterns, boolean firstFormatted, boolean second
" <include>src/**/java/**/*.java</include>",
"</includes>",
"<googleJavaFormat>",
" <version>1.10.0</version>",
"</googleJavaFormat>");

setFile(testFile(1)).toResource(fixture(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GoogleJavaFormatTest extends MavenIntegrationHarness {
void specificVersionDefaultStyle() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.10.0</version>",
" <version>1.17.0</version>",
"</googleJavaFormat>");

runTest("java/googlejavaformat/JavaCodeFormatted.test");
Expand All @@ -34,7 +34,7 @@ void specificVersionDefaultStyle() throws Exception {
void specificVersionSpecificStyle() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.10.0</version>",
" <version>1.17.0</version>",
" <style>AOSP</style>",
"</googleJavaFormat>");

Expand All @@ -45,7 +45,7 @@ void specificVersionSpecificStyle() throws Exception {
void specificVersionReflowLongStrings() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.10.0</version>",
" <version>1.17.0</version>",
" <reflowLongStrings>true</reflowLongStrings>",
"</googleJavaFormat>");

Expand All @@ -56,7 +56,7 @@ void specificVersionReflowLongStrings() throws Exception {
void specificVersionReorderImports() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.12.0</version>",
" <version>1.17.0</version>",
" <style>AOSP</style>",
" <reorderImports>true</reorderImports>",
"</googleJavaFormat>");
Expand All @@ -68,7 +68,7 @@ void specificVersionReorderImports() throws Exception {
void specificVersionSkipJavadocFormatting() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.12.0</version>",
" <version>1.17.0</version>",
" <formatJavadoc>false</formatJavadoc>",
"</googleJavaFormat>");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void specificVersionDefaultStyle() throws Exception {
void specificJava11Version2() throws Exception {
writePomWithJavaSteps(
"<palantirJavaFormat>",
" <version>2.10.0</version>",
" <version>2.38.0</version>",
"</palantirJavaFormat>");

runTest("java/palantirjavaformat/JavaCodeFormatted.test");
Expand Down
Loading

0 comments on commit 7717dba

Please sign in to comment.