diff --git a/CHANGES.md b/CHANGES.md index dc11ed9af2..8e88ccbd44 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * **BREAKING** Bump minimum supported Gradle version from 7.3 to 8.1. [#2719](https://github.com/diffplug/spotless/pull/2719) ### Fixed * palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685)) +* Use Provider API for Gradle properties. ([#2718](https://github.com/diffplug/spotless/pull/2718) ### Removed * **BREAKING** Drop support for older Ktlint versions. ([#2711](https://github.com/diffplug/spotless/pull/2711)) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 14dcc8ce82..a368335841 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -630,8 +630,7 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) { FormatterStep createStep() { return builder.withYearModeLazy(() -> { - if ("true".equals(spotless.project - .findProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) { + if (Boolean.parseBoolean(GradleCompat.findOptionalProperty(spotless.project, LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) { return YearMode.SET_FROM_GIT; } else { boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleCompat.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleCompat.java new file mode 100644 index 0000000000..16d02b75b8 --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GradleCompat.java @@ -0,0 +1,41 @@ +/* + * Copyright 2025 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.spotless; + +import javax.annotation.Nullable; + +import org.gradle.api.Project; + +public final class GradleCompat { + private GradleCompat() {} + + @Nullable public static String findOptionalProperty(Project project, String propertyName) { + @Nullable String value = project.getProviders().gradleProperty(propertyName).getOrNull(); + if (value != null) { + return value; + } + @Nullable Object property = project.findProperty(propertyName); + if (property != null) { + return property.toString(); + } + return null; + } + + public static boolean isPropertyPresent(Project project, String propertyName) { + return project.getProviders().gradleProperty(propertyName).isPresent() || + project.hasProperty(propertyName); + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java index ffa1a18efb..98bb35069c 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/IdeHook.java @@ -36,10 +36,10 @@ static class State extends NoLambda.EqualityBasedOnSerialization { final boolean useStdOut; State(Project project) { - path = (String) project.findProperty(PROPERTY); + path = GradleCompat.findOptionalProperty(project, PROPERTY); if (path != null) { - useStdIn = project.hasProperty(USE_STD_IN); - useStdOut = project.hasProperty(USE_STD_OUT); + useStdIn = GradleCompat.isPropertyPresent(project, USE_STD_IN); + useStdOut = GradleCompat.isPropertyPresent(project, USE_STD_OUT); } else { useStdIn = false; useStdOut = false; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java index e85f49f204..2bd773f2d3 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java @@ -42,7 +42,7 @@ public void apply(Project project) { + "https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation"); } // if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff - if (project.hasProperty(SPOTLESS_MODERN)) { + if (GradleCompat.isPropertyPresent(project, SPOTLESS_MODERN)) { project.getLogger().warn("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it."); } // make sure there's a `clean` and a `check`