Skip to content

Commit 1443d80

Browse files
committed
Add compatibility utils for Gradle properties
1 parent 3c6baef commit 1443d80

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

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

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

631631
FormatterStep createStep() {
632632
return builder.withYearModeLazy(() -> {
633-
if (spotless.project
634-
.getProviders()
635-
.gradleProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY())
636-
.map(Boolean::parseBoolean)
637-
.getOrElse(false)) {
633+
if (Boolean.parseBoolean(GradleCompat.findOptionalProperty(spotless.project, LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
638634
return YearMode.SET_FROM_GIT;
639635
} else {
640636
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javax.annotation.Nullable;
2323

2424
import org.gradle.api.Project;
25-
import org.gradle.api.provider.ProviderFactory;
2625

2726
import com.diffplug.common.base.Errors;
2827
import com.diffplug.common.io.ByteStreams;
@@ -37,11 +36,10 @@ static class State extends NoLambda.EqualityBasedOnSerialization {
3736
final boolean useStdOut;
3837

3938
State(Project project) {
40-
ProviderFactory providers = project.getProviders();
41-
path = providers.gradleProperty(PROPERTY).getOrNull();
39+
path = GradleCompat.findOptionalProperty(project, PROPERTY);
4240
if (path != null) {
43-
useStdIn = providers.gradleProperty(USE_STD_IN).isPresent();
44-
useStdOut = providers.gradleProperty(USE_STD_OUT).isPresent();
41+
useStdIn = GradleCompat.isPropertyPresent(project, USE_STD_IN);
42+
useStdOut = GradleCompat.isPropertyPresent(project, USE_STD_OUT);
4543
} else {
4644
useStdIn = false;
4745
useStdOut = false;

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18-
import java.lang.reflect.Method;
19-
20-
import javax.annotation.Nonnull;
21-
2218
import org.gradle.api.GradleException;
2319
import org.gradle.api.JavaVersion;
2420
import org.gradle.api.Plugin;
2521
import org.gradle.api.Project;
2622
import org.gradle.api.plugins.BasePlugin;
27-
import org.gradle.api.provider.Provider;
2823
import org.gradle.util.GradleVersion;
2924

3025
import com.diffplug.spotless.Jvm;
@@ -47,8 +42,7 @@ public void apply(Project project) {
4742
+ "https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation");
4843
}
4944
// if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
50-
Provider<String> spotlessModernProperty = forUseAtConfigurationTime(project.getProviders().gradleProperty(SPOTLESS_MODERN));
51-
if (spotlessModernProperty.isPresent()) {
45+
if (GradleCompat.isPropertyPresent(project, SPOTLESS_MODERN)) {
5246
project.getLogger().warn("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it.");
5347
}
5448
// make sure there's a `clean` and a `check`
@@ -70,30 +64,4 @@ public void apply(Project project) {
7064
static String capitalize(String input) {
7165
return Character.toUpperCase(input.charAt(0)) + input.substring(1);
7266
}
73-
74-
static Provider<String> forUseAtConfigurationTime(@Nonnull Provider<String> provider) {
75-
if (isGradle73OrNewer() && !isGradle74OrNewer()) {
76-
try {
77-
// Use reflection to access the forUseAtConfigurationTime method as it was removed in Gradle 9.
78-
Method method = Provider.class.getMethod("forUseAtConfigurationTime");
79-
return (Provider<String>) method.invoke(provider);
80-
} catch (Exception e) {
81-
throw new RuntimeException("Failed to invoke forUseAtConfigurationTime via reflection", e);
82-
}
83-
} else {
84-
return provider;
85-
}
86-
}
87-
88-
static boolean isGradle73OrNewer() {
89-
return isGradleNewerThan("7.3");
90-
}
91-
92-
static boolean isGradle74OrNewer() {
93-
return isGradleNewerThan("7.4");
94-
}
95-
96-
private static boolean isGradleNewerThan(String version) {
97-
return GradleVersion.current().compareTo(GradleVersion.version(version)) >= 0;
98-
}
9967
}

0 commit comments

Comments
 (0)