diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 14d231c113..73d2a340c2 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+. ### Fixed * Revert change from 2.17.2 regarding [skip bug](https://github.com/diffplug/spotless/pull/969) because fixing the skip bug caused inconsistent behavior between `check.skip` and `apply.skip`. + * [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 (new fix). ## [2.17.2] - 2021-10-14 ### 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..3a98ee240b 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 @@ -66,6 +66,8 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { private static final String DEFAULT_ENCODING = "UTF-8"; private static final String DEFAULT_LINE_ENDINGS = "GIT_ATTRIBUTES"; + static final String GOAL_CHECK = "check"; + static final String GOAL_APPLY = "apply"; @Component private RepositorySystem repositorySystem; @@ -73,6 +75,15 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Component private ResourceManager resourceManager; + @Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true) + private String goal; + + @Parameter(property = "spotless.apply.skip", defaultValue = "false") + private boolean applySkip; + + @Parameter(property = "spotless.check.skip", defaultValue = "false") + private boolean checkSkip; + @Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true) private RepositorySystemSession repositorySystemSession; @@ -146,7 +157,24 @@ public final void execute() throws MojoExecutionException { } } + private boolean shouldSkip() { + switch (goal) { + case GOAL_CHECK: + return checkSkip; + case GOAL_APPLY: + return applySkip; + default: + break; + } + return false; + } + private void execute(FormatterFactory formatterFactory) throws MojoExecutionException { + if (shouldSkip()) { + 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 9e70a67a82..b7c0c31080 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 @@ -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; @@ -28,18 +27,11 @@ /** * Performs formatting of all source files according to configured formatters. */ -@Mojo(name = "apply", threadSafe = true) +@Mojo(name = AbstractSpotlessMojo.GOAL_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 dc09c3cffe..440a43f3bb 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 @@ -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; @@ -33,19 +32,11 @@ * Performs code formatting analysis and prints all violations to the console. * Fails the build if violations are discovered. */ -@Mojo(name = "check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true) +@Mojo(name = AbstractSpotlessMojo.GOAL_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 {