Skip to content

Commit 13d4045

Browse files
committed
Restore GradleCompat with IP safe approach
1 parent fd20a1f commit 13d4045

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

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
@@ -635,8 +635,7 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {
635635

636636
FormatterStep createStep() {
637637
return builder.withYearModeLazy(() -> {
638-
String yearProperty = spotless.project.getProviders().gradleProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()).getOrNull();
639-
if (Boolean.parseBoolean(yearProperty)) {
638+
if (Boolean.parseBoolean(GradleCompat.findOptionalProperty(spotless.project, LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
640639
return YearMode.SET_FROM_GIT;
641640
} else {
642641
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025-2026 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+
import org.gradle.api.plugins.ExtraPropertiesExtension;
22+
23+
public final class GradleCompat {
24+
private GradleCompat() {}
25+
26+
@Nullable public static String findOptionalProperty(Project project, String propertyName) {
27+
@Nullable String value = project.getProviders().gradleProperty(propertyName).getOrNull();
28+
if (value != null) {
29+
return value;
30+
}
31+
ExtraPropertiesExtension extras = project.getExtensions().getByType(ExtraPropertiesExtension.class);
32+
if (extras.has(propertyName)) {
33+
@Nullable Object property = extras.get(propertyName);
34+
if (property != null) {
35+
return property.toString();
36+
}
37+
}
38+
return null;
39+
}
40+
41+
public static boolean isPropertyPresent(Project project, String propertyName) {
42+
return project.getProviders().gradleProperty(propertyName).isPresent() ||
43+
project.getExtensions().getByType(ExtraPropertiesExtension.class).has(propertyName);
44+
}
45+
}

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
@@ -39,10 +39,10 @@ static class State extends NoLambda.EqualityBasedOnSerialization {
3939
final boolean useStdOut;
4040

4141
State(Project project) {
42-
var pathsString = project.getProviders().gradleProperty(PROPERTY).getOrNull();
42+
var pathsString = GradleCompat.findOptionalProperty(project, PROPERTY);
4343
if (pathsString != null) {
44-
useStdIn = project.getProviders().gradleProperty(USE_STD_IN).isPresent();
45-
useStdOut = project.getProviders().gradleProperty(USE_STD_OUT).isPresent();
44+
useStdIn = GradleCompat.isPropertyPresent(project, USE_STD_IN);
45+
useStdOut = GradleCompat.isPropertyPresent(project, USE_STD_OUT);
4646
paths = Arrays.stream(pathsString.split(","))
4747
.map(String::trim)
4848
.filter(s -> !s.isEmpty())

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2026 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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.getProviders().gradleProperty(SPOTLESS_MODERN).isPresent()) {
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)