From d83eee28c04a97b20054cf93e35f510b92e10438 Mon Sep 17 00:00:00 2001 From: Bradley Austin Davis Date: Wed, 13 Oct 2021 15:15:16 -0700 Subject: [PATCH] Consolidate skip behavior --- plugin-maven/CHANGES.md | 2 ++ .../diffplug/spotless/maven/AbstractSpotlessMojo.java | 11 +++++++++++ .../diffplug/spotless/maven/SpotlessApplyMojo.java | 10 +--------- .../diffplug/spotless/maven/SpotlessCheckMojo.java | 11 +---------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 3e9a77f4f6..35e34b0eff 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed + * [skip bug](https://github.com/diffplug/spotless/issues/968) if ratchetFrom is specified, the build will still fail in if no Git repository is found, even if `skip` is true. ## [2.17.1] - 2021-10-13 ### Fixed 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 245ab80a3a..c3409d8b9b 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 @@ -73,6 +73,12 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Component private ResourceManager resourceManager; + @Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true) + private String goal; + + @Parameter(property = "spotless.check.skip", defaultValue = "false") + private boolean skip; + @Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true) private RepositorySystemSession repositorySystemSession; @@ -147,6 +153,11 @@ public final void execute() throws MojoExecutionException { } private void execute(FormatterFactory formatterFactory) throws MojoExecutionException { + if (skip) { + getLog().info(String.format("Spotless %s skipped", goal)); + return; + } + FormatterConfig config = getFormatterConfig(); List files = collectFiles(formatterFactory, config); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java index 893a70f3f4..67cd79b961 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; import com.diffplug.spotless.Formatter; import com.diffplug.spotless.PaddedCell; @@ -30,16 +29,9 @@ */ @Mojo(name = "apply", threadSafe = true) public class SpotlessApplyMojo extends AbstractSpotlessMojo { - @Parameter(property = "spotless.apply.skip", defaultValue = "false") - private boolean skip; @Override protected void process(Iterable files, Formatter formatter) throws MojoExecutionException { - if (skip) { - getLog().info("Spotless apply skipped"); - return; - } - for (File file : files) { try { PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java index 6f555890ca..8df382cfd0 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; import com.diffplug.spotless.Formatter; import com.diffplug.spotless.PaddedCell; @@ -36,16 +35,8 @@ @Mojo(name = "check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true) public class SpotlessCheckMojo extends AbstractSpotlessMojo { - @Parameter(property = "spotless.check.skip", defaultValue = "false") - private boolean skip; - @Override protected void process(Iterable files, Formatter formatter) throws MojoExecutionException { - if (skip) { - getLog().info("Spotless check skipped"); - return; - } - List problemFiles = new ArrayList<>(); for (File file : files) { try {