Skip to content

Commit 18de088

Browse files
authored
Enable up-to-date checking in Maven plugin by default (#1621)
2 parents 7477651 + 24abb54 commit 18de088

File tree

8 files changed

+121
-64
lines changed

8 files changed

+121
-64
lines changed

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147))
88
### Fixed
99
* `licenseHeader` default pattern for Java files is updated to `(package|import|public|class|module) `. ([#1614](https://github.com/diffplug/spotless/pull/1614))
10+
### Changes
11+
* Enable incremental up-to-date checking by default. ([#1621](https://github.com/diffplug/spotless/pull/1621))
1012

1113
## [2.34.0] - 2023-02-27
1214
### Added

plugin-maven/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ To define what lines to skip at the beginning of such files, fill the `skipLines
12621262

12631263
## Incremental up-to-date checking and formatting
12641264

1265-
**This feature is turned off by default.**
1265+
**This feature is enabled by default starting from version 2.35.0.**
12661266

12671267
Execution of `spotless:check` and `spotless:apply` for large projects can take time.
12681268
By default, Spotless Maven plugin needs to read and format each source file.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
187187
private String setLicenseHeaderYearsFromGitHistory;
188188

189189
@Parameter
190-
private UpToDateChecking upToDateChecking;
190+
private UpToDateChecking upToDateChecking = UpToDateChecking.enabled();
191191

192192
protected abstract void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException;
193193

@@ -373,9 +373,9 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> formatters) {
373373
}
374374
final UpToDateChecker checker;
375375
if (upToDateChecking != null && upToDateChecking.isEnabled()) {
376-
getLog().info("Up-to-date checking enabled");
377376
checker = UpToDateChecker.forProject(project, indexFile, formatters, getLog());
378377
} else {
378+
getLog().info("Up-to-date checking disabled");
379379
checker = UpToDateChecker.noop(project, indexFile, getLog());
380380
}
381381
return UpToDateChecker.wrapWithBuildContext(checker, buildContext);

plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Objects;
2222

2323
import org.apache.maven.model.Plugin;
24+
import org.apache.maven.model.PluginManagement;
2425
import org.apache.maven.project.MavenProject;
2526

2627
import com.diffplug.spotless.Formatter;
@@ -43,10 +44,7 @@ private PluginFingerprint(String value) {
4344
}
4445

4546
static PluginFingerprint from(MavenProject project, Iterable<Formatter> formatters) {
46-
Plugin spotlessPlugin = project.getPlugin(SPOTLESS_PLUGIN_KEY);
47-
if (spotlessPlugin == null) {
48-
throw new IllegalArgumentException("Spotless plugin absent from the project: " + project);
49-
}
47+
Plugin spotlessPlugin = findSpotlessPlugin(project);
5048
byte[] digest = digest(spotlessPlugin, formatters);
5149
String value = Base64.getEncoder().encodeToString(digest);
5250
return new PluginFingerprint(value);
@@ -86,6 +84,24 @@ public String toString() {
8684
return "PluginFingerprint[" + value + "]";
8785
}
8886

87+
private static Plugin findSpotlessPlugin(MavenProject project) {
88+
// Try to find the plugin instance from <build><plugins><plugin> XML element
89+
Plugin plugin = project.getPlugin(SPOTLESS_PLUGIN_KEY);
90+
if (plugin == null) {
91+
// Try to find the plugin instance from <build><pluginManagement><plugins><plugin> XML element. Useful when
92+
// the current module is a parent of a multimodule project
93+
PluginManagement pluginManagement = project.getPluginManagement();
94+
if (pluginManagement != null) {
95+
plugin = pluginManagement.getPluginsAsMap().get(SPOTLESS_PLUGIN_KEY);
96+
}
97+
}
98+
99+
if (plugin == null) {
100+
throw new IllegalArgumentException("Spotless plugin absent from the project: " + project);
101+
}
102+
return plugin;
103+
}
104+
89105
private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
90106
try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) {
91107
out.writeObject(plugin.getVersion());

plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 DiffPlug
2+
* Copyright 2021-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.
@@ -38,4 +38,10 @@ public boolean isEnabled() {
3838
public Path getIndexFile() {
3939
return indexFile == null ? null : new File(indexFile).toPath();
4040
}
41+
42+
public static UpToDateChecking enabled() {
43+
UpToDateChecking upToDateChecking = new UpToDateChecking();
44+
upToDateChecking.enabled = true;
45+
return upToDateChecking;
46+
}
4147
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,31 @@ class MultiModuleProjectTest extends MavenIntegrationHarness {
3232
@Test
3333
void testConfigurationDependency() throws Exception {
3434
/*
35-
create a multi-module project with the following stucture:
35+
create a multi-module project with the following structure:
3636
3737
/junit-tmp-dir
3838
├── config
39-
   ├── pom.xml
40-
   └── src/main/resources/configs
41-
   ├── eclipse-formatter.xml
42-
   └── scalafmt.conf
39+
├── pom.xml
40+
└── src/main/resources/configs
41+
├── eclipse-formatter.xml
42+
└── scalafmt.conf
4343
├── mvnw
4444
├── mvnw.cmd
4545
├── one
46-
   ├── pom.xml
47-
   └── src
48-
   ├── main/java/test1.java
49-
   └── test/java/test2.java
46+
├── pom.xml
47+
└── src
48+
├── main/java/test1.java
49+
└── test/java/test2.java
5050
├── two
51-
   ├── pom.xml
52-
   └── src
53-
   ├── main/java/test1.java
54-
   └── test/java/test2.java
51+
├── pom.xml
52+
└── src
53+
├── main/java/test1.java
54+
└── test/java/test2.java
5555
├── three
56-
   ├── pom.xml
57-
   └── src
58-
   ├── main/scala/test1.scala
59-
   └── test/scala/test2.scala
56+
├── pom.xml
57+
└── src
58+
├── main/scala/test1.scala
59+
└── test/scala/test2.scala
6060
├── pom.xml
6161
├── .mvn
6262
├── mvnw

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
import java.io.ByteArrayInputStream;
2424
import java.nio.file.Paths;
2525
import java.util.Arrays;
26+
import java.util.Collections;
2627
import java.util.List;
2728

2829
import org.apache.maven.model.Model;
30+
import org.apache.maven.model.Plugin;
31+
import org.apache.maven.model.PluginManagement;
2932
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
3033
import org.apache.maven.project.MavenProject;
3134
import org.codehaus.plexus.util.ReaderFactory;
@@ -106,14 +109,45 @@ void emptyFingerprint() {
106109
}
107110

108111
@Test
109-
void failsWhenProjectDoesNotContainSpotlessPlugin() {
112+
void failsForProjectWithoutSpotlessPlugin() {
110113
MavenProject projectWithoutSpotless = new MavenProject();
111114

112115
assertThatThrownBy(() -> PluginFingerprint.from(projectWithoutSpotless, FORMATTERS))
113116
.isInstanceOf(IllegalArgumentException.class)
114117
.hasMessageContaining("Spotless plugin absent from the project");
115118
}
116119

120+
@Test
121+
void buildsFingerprintForProjectWithSpotlessPluginInBuildPlugins() {
122+
MavenProject project = new MavenProject();
123+
Plugin spotlessPlugin = new Plugin();
124+
spotlessPlugin.setGroupId("com.diffplug.spotless");
125+
spotlessPlugin.setArtifactId("spotless-maven-plugin");
126+
spotlessPlugin.setVersion("1.2.3");
127+
project.getBuild().addPlugin(spotlessPlugin);
128+
129+
PluginFingerprint fingerprint = PluginFingerprint.from(project, Collections.emptyList());
130+
131+
assertThat(fingerprint).isNotNull();
132+
}
133+
134+
@Test
135+
void buildsFingerprintForProjectWithSpotlessPluginInPluginManagement() {
136+
MavenProject project = new MavenProject();
137+
Plugin spotlessPlugin = new Plugin();
138+
spotlessPlugin.setGroupId("com.diffplug.spotless");
139+
spotlessPlugin.setArtifactId("spotless-maven-plugin");
140+
spotlessPlugin.setVersion("1.2.3");
141+
project.getBuild().addPlugin(spotlessPlugin);
142+
PluginManagement pluginManagement = new PluginManagement();
143+
pluginManagement.addPlugin(spotlessPlugin);
144+
project.getBuild().setPluginManagement(pluginManagement);
145+
146+
PluginFingerprint fingerprint = PluginFingerprint.from(project, Collections.emptyList());
147+
148+
assertThat(fingerprint).isNotNull();
149+
}
150+
117151
private MavenProject mavenProject(String spotlessVersion) throws Exception {
118152
String xml = createPomXmlContent(spotlessVersion, new String[0], new String[0]);
119153
return new MavenProject(readPom(xml));

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131

3232
class UpToDateCheckingTest extends MavenIntegrationHarness {
3333

34+
private static final String DISABLED_MESSAGE = "Up-to-date checking disabled";
35+
3436
@Test
35-
void upToDateCheckingDisabledByDefault() throws Exception {
37+
void upToDateCheckingEnabledByDefault() throws Exception {
3638
writePom(
3739
"<java>",
3840
" <googleJavaFormat/>",
@@ -41,75 +43,53 @@ void upToDateCheckingDisabledByDefault() throws Exception {
4143
List<File> files = writeUnformattedFiles(1);
4244
String output = runSpotlessApply();
4345

44-
assertThat(output).doesNotContain("Up-to-date checking enabled");
46+
assertThat(output).doesNotContain(DISABLED_MESSAGE);
4547
assertFormatted(files);
4648
}
4749

4850
@Test
49-
void enableUpToDateChecking() throws Exception {
51+
void explicitlyEnableUpToDateChecking() throws Exception {
5052
writePomWithUpToDateCheckingEnabled(true);
5153

5254
List<File> files = writeUnformattedFiles(1);
5355
String output = runSpotlessApply();
5456

55-
assertThat(output).contains("Up-to-date checking enabled");
57+
assertThat(output).doesNotContain(DISABLED_MESSAGE);
5658
assertFormatted(files);
5759
}
5860

5961
@Test
60-
void enableUpToDateCheckingWithPluginDependencies() throws Exception {
61-
writePomWithPluginManagementAndDependency();
62+
void explicitlyDisableUpToDateChecking() throws Exception {
63+
writePomWithUpToDateCheckingEnabled(false);
6264

6365
List<File> files = writeUnformattedFiles(1);
6466
String output = runSpotlessApply();
6567

66-
assertThat(output).contains("Up-to-date checking enabled");
68+
assertThat(output).contains(DISABLED_MESSAGE);
6769
assertFormatted(files);
6870
}
6971

7072
@Test
71-
void enableUpToDateCheckingWithPluginDependenciesMaven3_6_3() throws Exception {
73+
void enableUpToDateCheckingWithPluginDependencies() throws Exception {
7274
writePomWithPluginManagementAndDependency();
7375

74-
setFile(".mvn/wrapper/maven-wrapper.properties").toContent("distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip\n");
75-
7676
List<File> files = writeUnformattedFiles(1);
7777
String output = runSpotlessApply();
7878

79-
assertThat(output).contains("Up-to-date checking enabled");
79+
assertThat(output).doesNotContain(DISABLED_MESSAGE);
8080
assertFormatted(files);
8181
}
8282

83-
private void writePomWithPluginManagementAndDependency() throws IOException {
84-
setFile("pom.xml").toContent(createPomXmlContent("/pom-test-management.xml.mustache",
85-
null,
86-
null,
87-
new String[]{
88-
"<java>",
89-
" <googleJavaFormat/>",
90-
"</java>",
91-
"<upToDateChecking>",
92-
" <enabled>true</enabled>",
93-
"</upToDateChecking>"},
94-
new String[]{
95-
"<dependencies>",
96-
" <dependency>",
97-
" <groupId>javax.inject</groupId>",
98-
" <artifactId>javax.inject</artifactId>",
99-
" <version>1</version>",
100-
" </dependency>",
101-
"</dependencies>"},
102-
null));
103-
}
104-
10583
@Test
106-
void disableUpToDateChecking() throws Exception {
107-
writePomWithUpToDateCheckingEnabled(false);
84+
void enableUpToDateCheckingWithPluginDependenciesMaven3_6_3() throws Exception {
85+
writePomWithPluginManagementAndDependency();
86+
87+
setFile(".mvn/wrapper/maven-wrapper.properties").toContent("distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip\n");
10888

10989
List<File> files = writeUnformattedFiles(1);
11090
String output = runSpotlessApply();
11191

112-
assertThat(output).doesNotContain("Up-to-date checking enabled");
92+
assertThat(output).doesNotContain(DISABLED_MESSAGE);
11393
assertFormatted(files);
11494
}
11595

@@ -124,7 +104,7 @@ void enableUpToDateCheckingCustomIndexFile() throws Exception {
124104
List<File> files = writeUnformattedFiles(1);
125105
String output = runSpotlessApply();
126106

127-
assertThat(output).contains("Up-to-date checking enabled");
107+
assertThat(output).doesNotContain(DISABLED_MESSAGE);
128108
assertFormatted(files);
129109
assertThat(indexFile.getParent()).exists();
130110
assertThat(indexFile).exists();
@@ -143,7 +123,7 @@ void disableUpToDateCheckingCustomIndexFile() throws Exception {
143123
List<File> files = writeUnformattedFiles(1);
144124
String output = runSpotlessApply();
145125

146-
assertThat(output).doesNotContain("Up-to-date checking enabled");
126+
assertThat(output).contains(DISABLED_MESSAGE);
147127
assertFormatted(files);
148128
assertThat(indexFile.getParent()).exists();
149129
assertThat(indexFile).doesNotExist();
@@ -215,6 +195,25 @@ void spotlessCheckRecordsUnformattedFiles() throws Exception {
215195
assertSpotlessCheckSkipped(files, checkOutput3);
216196
}
217197

198+
private void writePomWithPluginManagementAndDependency() throws IOException {
199+
setFile("pom.xml").toContent(createPomXmlContent("/pom-test-management.xml.mustache",
200+
null,
201+
null,
202+
new String[]{
203+
"<java>",
204+
" <googleJavaFormat/>",
205+
"</java>"},
206+
new String[]{
207+
"<dependencies>",
208+
" <dependency>",
209+
" <groupId>javax.inject</groupId>",
210+
" <artifactId>javax.inject</artifactId>",
211+
" <version>1</version>",
212+
" </dependency>",
213+
"</dependencies>"},
214+
null));
215+
}
216+
218217
private void writePomWithUpToDateCheckingEnabled(boolean enabled) throws IOException {
219218
writePom(
220219
"<java>",

0 commit comments

Comments
 (0)