Skip to content

Commit 8bef300

Browse files
authored
Use Provider API for Gradle properties (#2718)
2 parents a2ce4e2 + a2a3ef2 commit 8bef300

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1616
* **BREAKING** Bump minimum supported Gradle version from 7.3 to 8.1. [#2719](https://github.com/diffplug/spotless/pull/2719)
1717
### Fixed
1818
* 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))
19+
* Use Provider API for Gradle properties. ([#2718](https://github.com/diffplug/spotless/pull/2718)
1920
### Removed
2021
* **BREAKING** Drop support for older Ktlint versions. ([#2711](https://github.com/diffplug/spotless/pull/2711))
2122

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,7 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {
630630

631631
FormatterStep createStep() {
632632
return builder.withYearModeLazy(() -> {
633-
if ("true".equals(spotless.project
634-
.findProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
633+
if (Boolean.parseBoolean(GradleCompat.findOptionalProperty(spotless.project, LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
635634
return YearMode.SET_FROM_GIT;
636635
} else {
637636
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import javax.annotation.Nullable;
19+
20+
import org.gradle.api.Project;
21+
22+
public final class GradleCompat {
23+
private GradleCompat() {}
24+
25+
@Nullable public static String findOptionalProperty(Project project, String propertyName) {
26+
@Nullable String value = project.getProviders().gradleProperty(propertyName).getOrNull();
27+
if (value != null) {
28+
return value;
29+
}
30+
@Nullable Object property = project.findProperty(propertyName);
31+
if (property != null) {
32+
return property.toString();
33+
}
34+
return null;
35+
}
36+
37+
public static boolean isPropertyPresent(Project project, String propertyName) {
38+
return project.getProviders().gradleProperty(propertyName).isPresent() ||
39+
project.hasProperty(propertyName);
40+
}
41+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ static class State extends NoLambda.EqualityBasedOnSerialization {
3636
final boolean useStdOut;
3737

3838
State(Project project) {
39-
path = (String) project.findProperty(PROPERTY);
39+
path = GradleCompat.findOptionalProperty(project, PROPERTY);
4040
if (path != null) {
41-
useStdIn = project.hasProperty(USE_STD_IN);
42-
useStdOut = project.hasProperty(USE_STD_OUT);
41+
useStdIn = GradleCompat.isPropertyPresent(project, USE_STD_IN);
42+
useStdOut = GradleCompat.isPropertyPresent(project, USE_STD_OUT);
4343
} else {
4444
useStdIn = false;
4545
useStdOut = false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void apply(Project project) {
4242
+ "https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation");
4343
}
4444
// if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
45-
if (project.hasProperty(SPOTLESS_MODERN)) {
45+
if (GradleCompat.isPropertyPresent(project, SPOTLESS_MODERN)) {
4646
project.getLogger().warn("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it.");
4747
}
4848
// make sure there's a `clean` and a `check`

0 commit comments

Comments
 (0)