Skip to content

Commit c50503e

Browse files
authored
Merge pull request #1461 from lutovich/improve-maven-plugin-fingerprint
Reduce spurious invalidations of the up-to-date index in Maven plugin
2 parents 1b2c449 + 016eecf commit c50503e

File tree

5 files changed

+29
-139
lines changed

5 files changed

+29
-139
lines changed

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Reduce spurious invalidations of the up-to-date index file ([#1461](https://github.com/diffplug/spotless/pull/1461))
68

79
## [2.29.0] - 2023-01-02
810
### Added

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 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.
@@ -76,7 +76,7 @@ static FileIndex read(FileIndexConfig config, Log log) {
7676
PluginFingerprint computedFingerprint = config.getPluginFingerprint();
7777
PluginFingerprint storedFingerprint = PluginFingerprint.from(firstLine);
7878
if (!computedFingerprint.equals(storedFingerprint)) {
79-
log.info("Fingerprint mismatch in the index file. Fallback to an empty index");
79+
log.info("Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index");
8080
return emptyIndexFallback(config);
8181
} else {
8282
Content content = readIndexContent(reader, config.getProjectDir(), log);

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

+9-12
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.
@@ -17,17 +17,21 @@
1717

1818
import java.io.IOException;
1919
import java.io.UncheckedIOException;
20-
import java.util.ArrayList;
2120
import java.util.Base64;
22-
import java.util.List;
2321
import java.util.Objects;
2422

25-
import org.apache.maven.model.Dependency;
2623
import org.apache.maven.model.Plugin;
2724
import org.apache.maven.project.MavenProject;
2825

2926
import com.diffplug.spotless.Formatter;
3027

28+
/**
29+
* Represents a particular Spotless Maven plugin setup using a Base64-encoded serialized form of:
30+
* <ol>
31+
* <li>Plugin version as configured in the POM</li>
32+
* <li>Formatter instances created according to the POM configuration</li>
33+
* </ol>
34+
*/
3135
class PluginFingerprint {
3236

3337
private static final String SPOTLESS_PLUGIN_KEY = "com.diffplug.spotless:spotless-maven-plugin";
@@ -83,22 +87,15 @@ public String toString() {
8387
}
8488

8589
private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
86-
// dependencies can be an unserializable org.apache.maven.model.merge.ModelMerger$MergingList
87-
// replace it with a serializable ArrayList
88-
List<Dependency> dependencies = plugin.getDependencies();
89-
plugin.setDependencies(new ArrayList<>(dependencies));
9090
try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) {
91-
out.writeObject(plugin);
91+
out.writeObject(plugin.getVersion());
9292
for (Formatter formatter : formatters) {
9393
out.writeObject(formatter);
9494
}
9595
out.flush();
9696
return out.digest();
9797
} catch (IOException e) {
9898
throw new UncheckedIOException("Unable to serialize plugin " + plugin, e);
99-
} finally {
100-
// reset the original list
101-
plugin.setDependencies(dependencies);
10299
}
103100
}
104101
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 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.
@@ -60,7 +60,7 @@ void readFallsBackToEmptyIndexOnFingerprintMismatch() throws Exception {
6060
FileIndex index = FileIndex.read(config, log);
6161

6262
assertThat(index.size()).isZero();
63-
verify(log).info("Fingerprint mismatch in the index file. Fallback to an empty index");
63+
verify(log).info("Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index");
6464
}
6565

6666
@Test

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

+14-123
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.
@@ -43,126 +43,22 @@ class PluginFingerprintTest extends MavenIntegrationHarness {
4343
private static final String VERSION_1 = "1.0.0";
4444
private static final String VERSION_2 = "2.0.0";
4545

46-
private static final String[] EXECUTION_1 = {
47-
"<execution>",
48-
" <id>check</id>",
49-
" <goals>",
50-
" <goal>check</goal>",
51-
" </goals>",
52-
"</execution>"
53-
};
54-
private static final String[] EXECUTION_2 = {};
55-
56-
private static final String[] CONFIGURATION_1 = {
57-
"<googleJavaFormat>",
58-
" <version>1.2</version>",
59-
"</googleJavaFormat>"
60-
};
61-
private static final String[] CONFIGURATION_2 = {
62-
"<googleJavaFormat>",
63-
" <version>1.8</version>",
64-
" <reflowLongStrings>true</reflowLongStrings>",
65-
"</googleJavaFormat>"
66-
};
67-
68-
private static final String[] DEPENDENCIES_1 = {
69-
"<dependencies>",
70-
" <dependency>",
71-
" <groupId>unknown</groupId>",
72-
" <artifactId>unknown</artifactId>",
73-
" <version>1.0</version>",
74-
" </dependency>",
75-
"</dependencies>"
76-
};
77-
private static final String[] DEPENDENCIES_2 = {
78-
"<dependencies>",
79-
" <dependency>",
80-
" <groupId>unknown</groupId>",
81-
" <artifactId>unknown</artifactId>",
82-
" <version>2.0</version>",
83-
" </dependency>",
84-
"</dependencies>"
85-
};
86-
8746
private static final List<Formatter> FORMATTERS = singletonList(formatter(formatterStep("default")));
8847

8948
@Test
90-
void sameFingerprint() throws Exception {
91-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
92-
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
49+
void sameFingerprintWhenVersionAndFormattersAreTheSame() throws Exception {
50+
MavenProject project = mavenProject(VERSION_1);
9351

94-
MavenProject project1 = mavenProject(xml1);
95-
MavenProject project2 = mavenProject(xml2);
96-
97-
PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
98-
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);
52+
PluginFingerprint fingerprint1 = PluginFingerprint.from(project, FORMATTERS);
53+
PluginFingerprint fingerprint2 = PluginFingerprint.from(project, FORMATTERS);
9954

10055
assertThat(fingerprint1).isEqualTo(fingerprint2);
10156
}
10257

10358
@Test
104-
void sameFingerprintWithDependencies() throws Exception {
105-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1);
106-
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1);
107-
108-
MavenProject project1 = mavenProject(xml1);
109-
MavenProject project2 = mavenProject(xml2);
110-
111-
PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
112-
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);
113-
114-
assertThat(fingerprint1).isEqualTo(fingerprint2);
115-
}
116-
117-
@Test
118-
void differentFingerprintForDifferentDependencies() throws Exception {
119-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1);
120-
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_2);
121-
122-
MavenProject project1 = mavenProject(xml1);
123-
MavenProject project2 = mavenProject(xml2);
124-
125-
PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
126-
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);
127-
128-
assertThat(fingerprint1).isNotEqualTo(fingerprint2);
129-
}
130-
131-
@Test
132-
void differentFingerprintForDifferentPluginVersion() throws Exception {
133-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
134-
String xml2 = createPomXmlContent(VERSION_2, EXECUTION_1, CONFIGURATION_1);
135-
136-
MavenProject project1 = mavenProject(xml1);
137-
MavenProject project2 = mavenProject(xml2);
138-
139-
PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
140-
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);
141-
142-
assertThat(fingerprint1).isNotEqualTo(fingerprint2);
143-
}
144-
145-
@Test
146-
void differentFingerprintForDifferentExecution() throws Exception {
147-
String xml1 = createPomXmlContent(VERSION_2, EXECUTION_1, CONFIGURATION_1);
148-
String xml2 = createPomXmlContent(VERSION_2, EXECUTION_2, CONFIGURATION_1);
149-
150-
MavenProject project1 = mavenProject(xml1);
151-
MavenProject project2 = mavenProject(xml2);
152-
153-
PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
154-
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);
155-
156-
assertThat(fingerprint1).isNotEqualTo(fingerprint2);
157-
}
158-
159-
@Test
160-
void differentFingerprintForDifferentConfiguration() throws Exception {
161-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_2, CONFIGURATION_2);
162-
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_2, CONFIGURATION_1);
163-
164-
MavenProject project1 = mavenProject(xml1);
165-
MavenProject project2 = mavenProject(xml2);
59+
void differentFingerprintForDifferentPluginVersions() throws Exception {
60+
MavenProject project1 = mavenProject(VERSION_1);
61+
MavenProject project2 = mavenProject(VERSION_2);
16662

16763
PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
16864
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);
@@ -172,11 +68,8 @@ void differentFingerprintForDifferentConfiguration() throws Exception {
17268

17369
@Test
17470
void differentFingerprintForFormattersWithDifferentSteps() throws Exception {
175-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
176-
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
177-
178-
MavenProject project1 = mavenProject(xml1);
179-
MavenProject project2 = mavenProject(xml2);
71+
MavenProject project1 = mavenProject(VERSION_1);
72+
MavenProject project2 = mavenProject(VERSION_1);
18073

18174
FormatterStep step1 = formatterStep("step1");
18275
FormatterStep step2 = formatterStep("step2");
@@ -192,11 +85,8 @@ void differentFingerprintForFormattersWithDifferentSteps() throws Exception {
19285

19386
@Test
19487
void differentFingerprintForFormattersWithDifferentLineEndings() throws Exception {
195-
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
196-
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
197-
198-
MavenProject project1 = mavenProject(xml1);
199-
MavenProject project2 = mavenProject(xml2);
88+
MavenProject project1 = mavenProject(VERSION_1);
89+
MavenProject project2 = mavenProject(VERSION_1);
20090

20191
FormatterStep step = formatterStep("step");
20292
List<Formatter> formatters1 = singletonList(formatter(LineEnding.UNIX, step));
@@ -224,7 +114,8 @@ void failsWhenProjectDoesNotContainSpotlessPlugin() {
224114
.hasMessageContaining("Spotless plugin absent from the project");
225115
}
226116

227-
private static MavenProject mavenProject(String xml) throws Exception {
117+
private MavenProject mavenProject(String spotlessVersion) throws Exception {
118+
String xml = createPomXmlContent(spotlessVersion, new String[0], new String[0]);
228119
return new MavenProject(readPom(xml));
229120
}
230121

0 commit comments

Comments
 (0)