Skip to content

Commit 2d3acde

Browse files
authored
Consolidate maven skip behavior (take two) (#973, revision of #969, fixes #968)
2 parents e166150 + 365f623 commit 2d3acde

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-groovy`. Change is only applied for JVM 11+.
88
### Fixed
99
* 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`.
10+
* [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).
1011

1112
## [2.17.2] - 2021-10-14
1213
### Fixed

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

+28
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,24 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
6666

6767
private static final String DEFAULT_ENCODING = "UTF-8";
6868
private static final String DEFAULT_LINE_ENDINGS = "GIT_ATTRIBUTES";
69+
static final String GOAL_CHECK = "check";
70+
static final String GOAL_APPLY = "apply";
6971

7072
@Component
7173
private RepositorySystem repositorySystem;
7274

7375
@Component
7476
private ResourceManager resourceManager;
7577

78+
@Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true)
79+
private String goal;
80+
81+
@Parameter(property = "spotless.apply.skip", defaultValue = "false")
82+
private boolean applySkip;
83+
84+
@Parameter(property = "spotless.check.skip", defaultValue = "false")
85+
private boolean checkSkip;
86+
7687
@Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true)
7788
private RepositorySystemSession repositorySystemSession;
7889

@@ -146,7 +157,24 @@ public final void execute() throws MojoExecutionException {
146157
}
147158
}
148159

160+
private boolean shouldSkip() {
161+
switch (goal) {
162+
case GOAL_CHECK:
163+
return checkSkip;
164+
case GOAL_APPLY:
165+
return applySkip;
166+
default:
167+
break;
168+
}
169+
return false;
170+
}
171+
149172
private void execute(FormatterFactory formatterFactory) throws MojoExecutionException {
173+
if (shouldSkip()) {
174+
getLog().info(String.format("Spotless %s skipped", goal));
175+
return;
176+
}
177+
150178
FormatterConfig config = getFormatterConfig();
151179
List<File> files = collectFiles(formatterFactory, config);
152180

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,18 @@
2020

2121
import org.apache.maven.plugin.MojoExecutionException;
2222
import org.apache.maven.plugins.annotations.Mojo;
23-
import org.apache.maven.plugins.annotations.Parameter;
2423

2524
import com.diffplug.spotless.Formatter;
2625
import com.diffplug.spotless.PaddedCell;
2726

2827
/**
2928
* Performs formatting of all source files according to configured formatters.
3029
*/
31-
@Mojo(name = "apply", threadSafe = true)
30+
@Mojo(name = AbstractSpotlessMojo.GOAL_APPLY, threadSafe = true)
3231
public class SpotlessApplyMojo extends AbstractSpotlessMojo {
33-
@Parameter(property = "spotless.apply.skip", defaultValue = "false")
34-
private boolean skip;
3532

3633
@Override
3734
protected void process(Iterable<File> files, Formatter formatter) throws MojoExecutionException {
38-
if (skip) {
39-
getLog().info("Spotless apply skipped");
40-
return;
41-
}
42-
4335
for (File file : files) {
4436
try {
4537
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.maven.plugin.MojoExecutionException;
2424
import org.apache.maven.plugins.annotations.LifecyclePhase;
2525
import org.apache.maven.plugins.annotations.Mojo;
26-
import org.apache.maven.plugins.annotations.Parameter;
2726

2827
import com.diffplug.spotless.Formatter;
2928
import com.diffplug.spotless.PaddedCell;
@@ -33,19 +32,11 @@
3332
* Performs code formatting analysis and prints all violations to the console.
3433
* Fails the build if violations are discovered.
3534
*/
36-
@Mojo(name = "check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
35+
@Mojo(name = AbstractSpotlessMojo.GOAL_CHECK, defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
3736
public class SpotlessCheckMojo extends AbstractSpotlessMojo {
3837

39-
@Parameter(property = "spotless.check.skip", defaultValue = "false")
40-
private boolean skip;
41-
4238
@Override
4339
protected void process(Iterable<File> files, Formatter formatter) throws MojoExecutionException {
44-
if (skip) {
45-
getLog().info("Spotless check skipped");
46-
return;
47-
}
48-
4940
List<File> problemFiles = new ArrayList<>();
5041
for (File file : files) {
5142
try {

0 commit comments

Comments
 (0)