Skip to content

Commit 02b6f60

Browse files
authored
Prefer using plugin extensions over deprecated conventions (#1618)
2 parents 61c7c10 + 91439e5 commit 02b6f60

File tree

10 files changed

+112
-73
lines changed

10 files changed

+112
-73
lines changed

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1818
### Changes
1919
* **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
2020
* Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630))
21+
### Fixed
22+
* Stop using deprecated conventions when used in Gradle >= `7.1`. ([#1618](https://github.com/diffplug/spotless/pull/1618))
2123

2224
## [6.17.0] - 2023-03-13
2325
### Added

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@
2424

2525
import org.gradle.api.GradleException;
2626
import org.gradle.api.Project;
27-
import org.gradle.api.file.FileCollection;
2827
import org.gradle.api.internal.plugins.DslObject;
2928
import org.gradle.api.plugins.GroovyBasePlugin;
30-
import org.gradle.api.plugins.JavaPluginConvention;
29+
import org.gradle.api.tasks.GroovySourceDirectorySet;
3130
import org.gradle.api.tasks.GroovySourceSet;
32-
import org.gradle.api.tasks.SourceSet;
31+
import org.gradle.util.GradleVersion;
3332

3433
import com.diffplug.spotless.extra.EquoBasedStepBuilder;
3534
import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
3635
import com.diffplug.spotless.generic.LicenseHeaderStep;
3736
import com.diffplug.spotless.java.ImportOrderStep;
3837

39-
public class GroovyExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
38+
public class GroovyExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang {
4039
static final String NAME = "groovy";
4140

4241
@Inject
@@ -112,22 +111,28 @@ public GrEclipseConfig withP2Mirrors(Map<String, String> mirrors) {
112111
@Override
113112
protected void setupTask(SpotlessTask task) {
114113
if (target == null) {
115-
JavaPluginConvention convention = getProject().getConvention().getPlugin(JavaPluginConvention.class);
116-
if (convention == null || !getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) {
117-
throw new GradleException("You must apply the groovy plugin before the spotless plugin if you are using the groovy extension.");
114+
final String message = "You must either specify 'target' manually or apply the 'groovy' plugin.";
115+
if (!getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) {
116+
throw new GradleException(message);
118117
}
119-
//Add all Groovy files (may contain Java files as well)
120-
121-
FileCollection union = getProject().files();
122-
for (SourceSet sourceSet : convention.getSourceSets()) {
123-
GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class);
124-
if (excludeJava) {
125-
union = union.plus(groovySourceSet.getAllGroovy());
126-
} else {
127-
union = union.plus(groovySourceSet.getGroovy());
128-
}
129-
}
130-
target = union;
118+
target = getSources(getProject(),
119+
message,
120+
sourceSet -> {
121+
if (GradleVersion.current().compareTo(GradleVersion.version(SpotlessPlugin.VER_GRADLE_javaPluginExtension)) >= 0) {
122+
return sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class);
123+
} else {
124+
final GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class);
125+
return groovySourceSet.getGroovy();
126+
}
127+
},
128+
file -> {
129+
final String name = file.getName();
130+
if (excludeJava) {
131+
return name.endsWith(".groovy");
132+
} else {
133+
return name.endsWith(".groovy") || name.endsWith(".java");
134+
}
135+
});
131136
} else if (excludeJava) {
132137
throw new IllegalArgumentException("'excludeJava' is not supported in combination with a custom 'target'.");
133138
}

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626

2727
import javax.inject.Inject;
2828

29-
import org.gradle.api.GradleException;
3029
import org.gradle.api.Project;
31-
import org.gradle.api.file.FileCollection;
32-
import org.gradle.api.plugins.JavaPluginConvention;
3330
import org.gradle.api.tasks.SourceSet;
3431

3532
import com.diffplug.spotless.FormatterStep;
@@ -43,7 +40,7 @@
4340
import com.diffplug.spotless.java.PalantirJavaFormatStep;
4441
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
4542

46-
public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
43+
public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang {
4744
static final String NAME = "java";
4845

4946
@Inject
@@ -374,15 +371,10 @@ private FormatterStep createStep() {
374371
@Override
375372
protected void setupTask(SpotlessTask task) {
376373
if (target == null) {
377-
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
378-
if (javaPlugin == null) {
379-
throw new GradleException("You must either specify 'target' manually or apply the 'java' plugin.");
380-
}
381-
FileCollection union = getProject().files();
382-
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
383-
union = union.plus(sourceSet.getAllJava());
384-
}
385-
target = union;
374+
target = getSources(getProject(),
375+
"You must either specify 'target' manually or apply the 'java' plugin.",
376+
SourceSet::getAllJava,
377+
file -> file.getName().endsWith(".java"));
386378
}
387379

388380
steps.replaceAll(step -> {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2023 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 java.io.File;
19+
import java.util.function.Function;
20+
21+
import org.gradle.api.GradleException;
22+
import org.gradle.api.Project;
23+
import org.gradle.api.file.FileCollection;
24+
import org.gradle.api.file.SourceDirectorySet;
25+
import org.gradle.api.plugins.JavaPluginConvention;
26+
import org.gradle.api.plugins.JavaPluginExtension;
27+
import org.gradle.api.specs.Spec;
28+
import org.gradle.api.tasks.SourceSet;
29+
import org.gradle.api.tasks.SourceSetContainer;
30+
import org.gradle.util.GradleVersion;
31+
32+
interface JvmLang {
33+
34+
default FileCollection getSources(Project project, String message, Function<SourceSet, SourceDirectorySet> sourceSetSourceDirectory, Spec<? super File> filterSpec) {
35+
final SourceSetContainer sourceSets;
36+
FileCollection union = project.files();
37+
if (GradleVersion.current().compareTo(GradleVersion.version(SpotlessPlugin.VER_GRADLE_javaPluginExtension)) >= 0) {
38+
final JavaPluginExtension javaPluginExtension = project.getExtensions().findByType(JavaPluginExtension.class);
39+
if (javaPluginExtension == null) {
40+
throw new GradleException(message);
41+
}
42+
sourceSets = javaPluginExtension.getSourceSets();
43+
} else {
44+
final JavaPluginConvention javaPluginConvention = project.getConvention().findPlugin(JavaPluginConvention.class);
45+
if (javaPluginConvention == null) {
46+
throw new GradleException(message);
47+
}
48+
sourceSets = javaPluginConvention.getSourceSets();
49+
}
50+
for (SourceSet sourceSet : sourceSets) {
51+
union = union.plus(sourceSetSourceDirectory.apply(sourceSet).filter(filterSpec));
52+
}
53+
return union;
54+
}
55+
}

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
import javax.annotation.Nullable;
2828
import javax.inject.Inject;
2929

30-
import org.gradle.api.GradleException;
31-
import org.gradle.api.file.FileCollection;
32-
import org.gradle.api.plugins.JavaPluginConvention;
3330
import org.gradle.api.tasks.SourceSet;
3431

3532
import com.diffplug.common.collect.ImmutableSortedMap;
@@ -41,7 +38,7 @@
4138
import com.diffplug.spotless.kotlin.KtfmtStep.KtfmtFormattingOptions;
4239
import com.diffplug.spotless.kotlin.KtfmtStep.Style;
4340

44-
public class KotlinExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
41+
public class KotlinExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang {
4542
static final String NAME = "kotlin";
4643

4744
@Inject
@@ -230,18 +227,13 @@ private FormatterStep createStep() {
230227
@Override
231228
protected void setupTask(SpotlessTask task) {
232229
if (target == null) {
233-
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
234-
if (javaPlugin == null) {
235-
throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin.");
236-
}
237-
FileCollection union = getProject().files();
238-
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
239-
union = union.plus(sourceSet.getAllSource().filter(file -> {
240-
String name = file.getName();
241-
return name.endsWith(".kt") || name.endsWith(".kts");
242-
}));
243-
}
244-
target = union;
230+
target = getSources(getProject(),
231+
"You must either specify 'target' manually or apply a kotlin plugin.",
232+
SourceSet::getAllSource,
233+
file -> {
234+
final String name = file.getName();
235+
return name.endsWith(".kt") || name.endsWith(".kts");
236+
});
245237
}
246238
super.setupTask(task);
247239
}

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -21,15 +21,12 @@
2121
import javax.annotation.Nullable;
2222
import javax.inject.Inject;
2323

24-
import org.gradle.api.GradleException;
25-
import org.gradle.api.file.FileCollection;
26-
import org.gradle.api.plugins.JavaPluginConvention;
2724
import org.gradle.api.tasks.SourceSet;
2825

2926
import com.diffplug.spotless.FormatterStep;
3027
import com.diffplug.spotless.scala.ScalaFmtStep;
3128

32-
public class ScalaExtension extends FormatExtension {
29+
public class ScalaExtension extends FormatExtension implements JvmLang {
3330
static final String NAME = "scala";
3431

3532
@Inject
@@ -79,18 +76,13 @@ private FormatterStep createStep() {
7976
@Override
8077
protected void setupTask(SpotlessTask task) {
8178
if (target == null) {
82-
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
83-
if (javaPlugin == null) {
84-
throw new GradleException("You must either specify 'target' manually or apply the 'scala' plugin.");
85-
}
86-
FileCollection union = getProject().files();
87-
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
88-
union = union.plus(sourceSet.getAllSource().filter(file -> {
89-
String name = file.getName();
90-
return name.endsWith(".scala") || name.endsWith(".sc");
91-
}));
92-
}
93-
target = union;
79+
target = getSources(getProject(),
80+
"You must either specify 'target' manually or apply the 'scala' plugin.",
81+
SourceSet::getAllSource,
82+
file -> {
83+
final String name = file.getName();
84+
return name.endsWith(".scala") || name.endsWith(".sc");
85+
});
9486
}
9587
super.setupTask(task);
9688
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626

2727
public class SpotlessPlugin implements Plugin<Project> {
2828
static final String SPOTLESS_MODERN = "spotlessModern";
29-
static final String MINIMUM_GRADLE = "6.1.1";
29+
static final String VER_GRADLE_min = "6.1.1";
30+
static final String VER_GRADLE_javaPluginExtension = "7.1";
3031
private static final int MINIMUM_JRE = 11;
3132

3233
@Override
3334
public void apply(Project project) {
3435
if (SpotlessPluginRedirect.gradleIsTooOld(project)) {
35-
throw new GradleException("Spotless requires Gradle " + MINIMUM_GRADLE + " or newer, this was " + project.getGradle().getGradleVersion());
36+
throw new GradleException("Spotless requires Gradle " + VER_GRADLE_min + " or newer, this was " + project.getGradle().getGradleVersion());
3637
}
3738
if (Jvm.version() < MINIMUM_JRE) {
3839
throw new GradleException("Spotless requires JRE " + MINIMUM_JRE + " or newer, this was " + JavaVersion.current() + ".\n"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright 2020-2023 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.
@@ -46,7 +46,7 @@ private static int badSemver(int major, int minor) {
4646

4747
static boolean gradleIsTooOld(Project project) {
4848
if (gradleIsTooOld == null) {
49-
gradleIsTooOld = badSemver(project.getGradle().getGradleVersion()) < badSemver(SpotlessPlugin.MINIMUM_GRADLE);
49+
gradleIsTooOld = badSemver(project.getGradle().getGradleVersion()) < badSemver(SpotlessPlugin.VER_GRADLE_min);
5050
}
5151
return gradleIsTooOld.booleanValue();
5252
}
@@ -73,7 +73,7 @@ public void apply(Project project) {
7373
"If you like the idea behind 'ratchetFrom', you should checkout spotless-changelog",
7474
"https://github.com/diffplug/spotless-changelog");
7575
if (gradleIsTooOld(project)) {
76-
errorMsg = errorMsg.replace("To migrate:\n", "To migrate:\n- Upgrade gradle to " + SpotlessPlugin.MINIMUM_GRADLE + " or newer (you're on " + project.getGradle().getGradleVersion() + ")\n");
76+
errorMsg = errorMsg.replace("To migrate:\n", "To migrate:\n- Upgrade gradle to " + SpotlessPlugin.VER_GRADLE_min + " or newer (you're on " + project.getGradle().getGradleVersion() + ")\n");
7777
}
7878
throw new GradleException(errorMsg);
7979
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
public class GradleIntegrationHarness extends ResourceHarness {
4545
public enum GradleVersionSupport {
46-
JRE_11("5.0"), MINIMUM(SpotlessPlugin.MINIMUM_GRADLE),
46+
JRE_11("5.0"), MINIMUM(SpotlessPlugin.VER_GRADLE_min),
4747
// technically, this API exists in 6.5, but the flags for it change in 6.6, so we build to that
4848
CONFIGURATION_CACHE("6.6"),
4949
// https://docs.gradle.org/7.5/userguide/configuration_cache.html#config_cache:stable

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GroovyExtensionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -103,7 +103,7 @@ void groovyPluginMissingCheck() throws IOException {
103103

104104
Throwable error = assertThrows(Throwable.class,
105105
() -> gradleRunner().withArguments("spotlessApply").build());
106-
assertThat(error).hasMessageContaining("must apply the groovy plugin before");
106+
assertThat(error).hasMessageContaining("You must either specify 'target' manually or apply the 'groovy' plugin.");
107107
}
108108

109109
}

0 commit comments

Comments
 (0)