diff --git a/lib/build.gradle b/lib/build.gradle index 4d95cfe84f..3c8b6e75e4 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -72,7 +72,7 @@ dependencies { palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' // this version needs to stay compilable against Java 8 for CI Job testNpm - googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.8' // minimum required version due to api changes before then + googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.16.0' // minimum required version due to api changes before then // used jackson-based formatters jacksonCompileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2' diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 903064e2b1..6c7268eab6 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -19,6 +19,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Changes * **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) * Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) +### Fixed +* Stop using deprecated conventions when used in Gradle >= `7.1`. ([#1618](https://github.com/diffplug/spotless/pull/1618)) ## [6.17.0] - 2023-03-13 ### Added diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java index d25433293d..a7f3d98ce4 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GroovyExtension.java @@ -24,19 +24,18 @@ import org.gradle.api.GradleException; import org.gradle.api.Project; -import org.gradle.api.file.FileCollection; import org.gradle.api.internal.plugins.DslObject; import org.gradle.api.plugins.GroovyBasePlugin; -import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.tasks.GroovySourceDirectorySet; import org.gradle.api.tasks.GroovySourceSet; -import org.gradle.api.tasks.SourceSet; +import org.gradle.util.GradleVersion; import com.diffplug.spotless.extra.EquoBasedStepBuilder; import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep; import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.java.ImportOrderStep; -public class GroovyExtension extends FormatExtension implements HasBuiltinDelimiterForLicense { +public class GroovyExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang { static final String NAME = "groovy"; @Inject @@ -112,22 +111,28 @@ public GrEclipseConfig withP2Mirrors(Map mirrors) { @Override protected void setupTask(SpotlessTask task) { if (target == null) { - JavaPluginConvention convention = getProject().getConvention().getPlugin(JavaPluginConvention.class); - if (convention == null || !getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) { - throw new GradleException("You must apply the groovy plugin before the spotless plugin if you are using the groovy extension."); + final String message = "You must either specify 'target' manually or apply the 'groovy' plugin."; + if (!getProject().getPlugins().hasPlugin(GroovyBasePlugin.class)) { + throw new GradleException(message); } - //Add all Groovy files (may contain Java files as well) - - FileCollection union = getProject().files(); - for (SourceSet sourceSet : convention.getSourceSets()) { - GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class); - if (excludeJava) { - union = union.plus(groovySourceSet.getAllGroovy()); - } else { - union = union.plus(groovySourceSet.getGroovy()); - } - } - target = union; + target = getSources(getProject(), + message, + sourceSet -> { + if (GradleVersion.current().compareTo(GradleVersion.version(SpotlessPlugin.VER_GRADLE_javaPluginExtension)) >= 0) { + return sourceSet.getExtensions().getByType(GroovySourceDirectorySet.class); + } else { + final GroovySourceSet groovySourceSet = new DslObject(sourceSet).getConvention().getPlugin(GroovySourceSet.class); + return groovySourceSet.getGroovy(); + } + }, + file -> { + final String name = file.getName(); + if (excludeJava) { + return name.endsWith(".groovy"); + } else { + return name.endsWith(".groovy") || name.endsWith(".java"); + } + }); } else if (excludeJava) { throw new IllegalArgumentException("'excludeJava' is not supported in combination with a custom 'target'."); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index d85cb8de5e..3ca582dd21 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -26,10 +26,7 @@ import javax.inject.Inject; -import org.gradle.api.GradleException; import org.gradle.api.Project; -import org.gradle.api.file.FileCollection; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; import com.diffplug.spotless.FormatterStep; @@ -43,7 +40,7 @@ import com.diffplug.spotless.java.PalantirJavaFormatStep; import com.diffplug.spotless.java.RemoveUnusedImportsStep; -public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense { +public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang { static final String NAME = "java"; @Inject @@ -374,15 +371,10 @@ private FormatterStep createStep() { @Override protected void setupTask(SpotlessTask task) { if (target == null) { - JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class); - if (javaPlugin == null) { - throw new GradleException("You must either specify 'target' manually or apply the 'java' plugin."); - } - FileCollection union = getProject().files(); - for (SourceSet sourceSet : javaPlugin.getSourceSets()) { - union = union.plus(sourceSet.getAllJava()); - } - target = union; + target = getSources(getProject(), + "You must either specify 'target' manually or apply the 'java' plugin.", + SourceSet::getAllJava, + file -> file.getName().endsWith(".java")); } steps.replaceAll(step -> { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JvmLang.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JvmLang.java new file mode 100644 index 0000000000..1a9a06b986 --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JvmLang.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023 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 java.io.File; +import java.util.function.Function; + +import org.gradle.api.GradleException; +import org.gradle.api.Project; +import org.gradle.api.file.FileCollection; +import org.gradle.api.file.SourceDirectorySet; +import org.gradle.api.plugins.JavaPluginConvention; +import org.gradle.api.plugins.JavaPluginExtension; +import org.gradle.api.specs.Spec; +import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.util.GradleVersion; + +interface JvmLang { + + default FileCollection getSources(Project project, String message, Function sourceSetSourceDirectory, Spec filterSpec) { + final SourceSetContainer sourceSets; + FileCollection union = project.files(); + if (GradleVersion.current().compareTo(GradleVersion.version(SpotlessPlugin.VER_GRADLE_javaPluginExtension)) >= 0) { + final JavaPluginExtension javaPluginExtension = project.getExtensions().findByType(JavaPluginExtension.class); + if (javaPluginExtension == null) { + throw new GradleException(message); + } + sourceSets = javaPluginExtension.getSourceSets(); + } else { + final JavaPluginConvention javaPluginConvention = project.getConvention().findPlugin(JavaPluginConvention.class); + if (javaPluginConvention == null) { + throw new GradleException(message); + } + sourceSets = javaPluginConvention.getSourceSets(); + } + for (SourceSet sourceSet : sourceSets) { + union = union.plus(sourceSetSourceDirectory.apply(sourceSet).filter(filterSpec)); + } + return union; + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index 5a7419f68d..363a9cac6f 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -27,9 +27,6 @@ import javax.annotation.Nullable; import javax.inject.Inject; -import org.gradle.api.GradleException; -import org.gradle.api.file.FileCollection; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; import com.diffplug.common.collect.ImmutableSortedMap; @@ -41,7 +38,7 @@ import com.diffplug.spotless.kotlin.KtfmtStep.KtfmtFormattingOptions; import com.diffplug.spotless.kotlin.KtfmtStep.Style; -public class KotlinExtension extends FormatExtension implements HasBuiltinDelimiterForLicense { +public class KotlinExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang { static final String NAME = "kotlin"; @Inject @@ -230,18 +227,13 @@ private FormatterStep createStep() { @Override protected void setupTask(SpotlessTask task) { if (target == null) { - JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class); - if (javaPlugin == null) { - throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin."); - } - FileCollection union = getProject().files(); - for (SourceSet sourceSet : javaPlugin.getSourceSets()) { - union = union.plus(sourceSet.getAllSource().filter(file -> { - String name = file.getName(); - return name.endsWith(".kt") || name.endsWith(".kts"); - })); - } - target = union; + target = getSources(getProject(), + "You must either specify 'target' manually or apply a kotlin plugin.", + SourceSet::getAllSource, + file -> { + final String name = file.getName(); + return name.endsWith(".kt") || name.endsWith(".kts"); + }); } super.setupTask(task); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java index 8a8765b539..1639f3473c 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/ScalaExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,15 +21,12 @@ import javax.annotation.Nullable; import javax.inject.Inject; -import org.gradle.api.GradleException; -import org.gradle.api.file.FileCollection; -import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSet; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.scala.ScalaFmtStep; -public class ScalaExtension extends FormatExtension { +public class ScalaExtension extends FormatExtension implements JvmLang { static final String NAME = "scala"; @Inject @@ -79,18 +76,13 @@ private FormatterStep createStep() { @Override protected void setupTask(SpotlessTask task) { if (target == null) { - JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class); - if (javaPlugin == null) { - throw new GradleException("You must either specify 'target' manually or apply the 'scala' plugin."); - } - FileCollection union = getProject().files(); - for (SourceSet sourceSet : javaPlugin.getSourceSets()) { - union = union.plus(sourceSet.getAllSource().filter(file -> { - String name = file.getName(); - return name.endsWith(".scala") || name.endsWith(".sc"); - })); - } - target = union; + target = getSources(getProject(), + "You must either specify 'target' manually or apply the 'scala' plugin.", + SourceSet::getAllSource, + file -> { + final String name = file.getName(); + return name.endsWith(".scala") || name.endsWith(".sc"); + }); } super.setupTask(task); } 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 91847af0cf..0043e2b274 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 @@ -26,13 +26,14 @@ public class SpotlessPlugin implements Plugin { static final String SPOTLESS_MODERN = "spotlessModern"; - static final String MINIMUM_GRADLE = "6.1.1"; + static final String VER_GRADLE_min = "6.1.1"; + static final String VER_GRADLE_javaPluginExtension = "7.1"; private static final int MINIMUM_JRE = 11; @Override public void apply(Project project) { if (SpotlessPluginRedirect.gradleIsTooOld(project)) { - throw new GradleException("Spotless requires Gradle " + MINIMUM_GRADLE + " or newer, this was " + project.getGradle().getGradleVersion()); + throw new GradleException("Spotless requires Gradle " + VER_GRADLE_min + " or newer, this was " + project.getGradle().getGradleVersion()); } if (Jvm.version() < MINIMUM_JRE) { throw new GradleException("Spotless requires JRE " + MINIMUM_JRE + " or newer, this was " + JavaVersion.current() + ".\n" diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPluginRedirect.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPluginRedirect.java index da4d3dcd8b..975ec5576e 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPluginRedirect.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPluginRedirect.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 DiffPlug + * Copyright 2020-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,7 @@ private static int badSemver(int major, int minor) { static boolean gradleIsTooOld(Project project) { if (gradleIsTooOld == null) { - gradleIsTooOld = badSemver(project.getGradle().getGradleVersion()) < badSemver(SpotlessPlugin.MINIMUM_GRADLE); + gradleIsTooOld = badSemver(project.getGradle().getGradleVersion()) < badSemver(SpotlessPlugin.VER_GRADLE_min); } return gradleIsTooOld.booleanValue(); } @@ -73,7 +73,7 @@ public void apply(Project project) { "If you like the idea behind 'ratchetFrom', you should checkout spotless-changelog", "https://github.com/diffplug/spotless-changelog"); if (gradleIsTooOld(project)) { - errorMsg = errorMsg.replace("To migrate:\n", "To migrate:\n- Upgrade gradle to " + SpotlessPlugin.MINIMUM_GRADLE + " or newer (you're on " + project.getGradle().getGradleVersion() + ")\n"); + 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"); } throw new GradleException(errorMsg); } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java index 1763fd088f..61d191bab1 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java @@ -43,7 +43,7 @@ public class GradleIntegrationHarness extends ResourceHarness { public enum GradleVersionSupport { - JRE_11("5.0"), MINIMUM(SpotlessPlugin.MINIMUM_GRADLE), + JRE_11("5.0"), MINIMUM(SpotlessPlugin.VER_GRADLE_min), // technically, this API exists in 6.5, but the flags for it change in 6.6, so we build to that CONFIGURATION_CACHE("6.6"), // https://docs.gradle.org/7.5/userguide/configuration_cache.html#config_cache:stable diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GroovyExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GroovyExtensionTest.java index b9c7d2f66e..c38df40d54 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GroovyExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GroovyExtensionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -103,7 +103,7 @@ void groovyPluginMissingCheck() throws IOException { Throwable error = assertThrows(Throwable.class, () -> gradleRunner().withArguments("spotlessApply").build()); - assertThat(error).hasMessageContaining("must apply the groovy plugin before"); + assertThat(error).hasMessageContaining("You must either specify 'target' manually or apply the 'groovy' plugin."); } } diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 69877ce03e..231d68e0e7 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,10 +5,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Added * The `style` option in Palantir Java Format ([#1654](https://github.com/diffplug/spotless/pull/1654)). +* Added support for Gherkin feature files ([#1649](https://github.com/diffplug/spotless/issues/1649)). +### Fixed +* Fix non deterministic computation of cache fingerprint when using multiple formatters. ([#1643](https://github.com/diffplug/spotless/pull/1643) fixes [#1642](https://github.com/diffplug/spotless/pull/1642)) ### Changes * **POTENTIALLY BREAKING** Drop support for `googleJavaFormat` versions < `1.8`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) * Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#1630](https://github.com/diffplug/spotless/pull/1630)) -* Added support for Gherkin feature files ([#1649](https://github.com/diffplug/spotless/issues/1649)). ## [2.35.0] - 2023-03-13 ### Added diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index b864a28f4c..0b019ea844 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -23,8 +23,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -215,7 +215,7 @@ public final void execute() throws MojoExecutionException { List formatterFactories = getFormatterFactories(); FormatterConfig config = getFormatterConfig(); - Map>> formatterFactoryToFiles = new HashMap<>(); + Map>> formatterFactoryToFiles = new LinkedHashMap<>(); for (FormatterFactory formatterFactory : formatterFactories) { Supplier> filesToFormat = () -> collectFiles(formatterFactory, config); formatterFactoryToFiles.put(formatterFactory, filesToFormat); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormattersHolder.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormattersHolder.java index 1c226423f6..873321f9df 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormattersHolder.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormattersHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package com.diffplug.spotless.maven; import java.io.File; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -33,7 +33,7 @@ class FormattersHolder implements AutoCloseable { } static FormattersHolder create(Map>> formatterFactoryToFiles, FormatterConfig config) { - Map>> formatterToFiles = new HashMap<>(); + Map>> formatterToFiles = new LinkedHashMap<>(); try { for (Entry>> entry : formatterFactoryToFiles.entrySet()) { FormatterFactory formatterFactory = entry.getKey();