Skip to content

Commit 1f0397c

Browse files
committed
Add m2e support
This closes #1413
1 parent 6dfa398 commit 1f0397c

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

plugin-maven/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ dependencies {
7676
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"
7777
implementation("org.codehaus.plexus:plexus-resources:${VER_PLEXUS_RESOURCES}")
7878
implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"
79+
implementation 'org.sonatype.plexus:plexus-build-api:0.0.7'
7980

8081
testImplementation project(":testlib")
8182
testImplementation "org.junit.jupiter:junit-jupiter:${VER_JUNIT}"

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
8888
@Component
8989
private ResourceManager resourceManager;
9090

91+
@Component
92+
protected BuildContext buildContext;
93+
9194
@Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true)
9295
private String goal;
9396

@@ -344,10 +347,13 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> formatters) {
344347
Path targetDir = project.getBasedir().toPath().resolve(project.getBuild().getDirectory());
345348
indexFile = targetDir.resolve(DEFAULT_INDEX_FILE_NAME);
346349
}
350+
final UpToDateChecker checker;
347351
if (upToDateChecking != null && upToDateChecking.isEnabled()) {
348352
getLog().info("Up-to-date checking enabled");
349-
return UpToDateChecker.forProject(project, indexFile, formatters, getLog());
353+
checker = UpToDateChecker.forProject(project, indexFile, formatters, getLog());
354+
} else {
355+
checker = UpToDateChecker.noop(project, indexFile, getLog());
350356
}
351-
return UpToDateChecker.noop(project, indexFile, getLog());
357+
return UpToDateChecker.wrapWithBuildContext(checker, buildContext);
352358
}
353359
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
4545
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
4646
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
4747
dirtyState.writeCanonicalTo(file);
48+
buildContext.refresh(file);
4849
}
4950
} catch (IOException e) {
5051
throw new MojoExecutionException("Unable to format file " + file, e);

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

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.maven.plugin.logging.Log;
2121
import org.apache.maven.project.MavenProject;
22+
import org.sonatype.plexus.build.incremental.BuildContext;
2223

2324
import com.diffplug.spotless.Formatter;
2425

@@ -37,4 +38,25 @@ static UpToDateChecker noop(MavenProject project, Path indexFile, Log log) {
3738
static UpToDateChecker forProject(MavenProject project, Path indexFile, Iterable<Formatter> formatters, Log log) {
3839
return IndexBasedChecker.create(project, indexFile, formatters, log);
3940
}
41+
42+
static UpToDateChecker wrapWithBuildContext(UpToDateChecker delegate, BuildContext buildContext) {
43+
return new UpToDateChecker() {
44+
45+
@Override
46+
public void setUpToDate(Path file) {
47+
delegate.setUpToDate(file);
48+
}
49+
50+
@Override
51+
public boolean isUpToDate(Path file) {
52+
return !buildContext.hasDelta(file.toFile()) || delegate.isUpToDate(file);
53+
}
54+
55+
@Override
56+
public void close() {
57+
delegate.close();
58+
}
59+
};
60+
}
61+
4062
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<!-- metadata for m2e: https://www.eclipse.org/m2e/documentation/m2e-making-maven-plugins-compat.html -->
15+
<lifecycleMappingMetadata>
16+
<pluginExecutions>
17+
<pluginExecution>
18+
<pluginExecutionFilter>
19+
<goals>
20+
<goal>apply</goal>
21+
<goal>check</goal>
22+
</goals>
23+
</pluginExecutionFilter>
24+
<action>
25+
<execute>
26+
<runOnIncremental>true</runOnIncremental>
27+
<runOnConfiguration>false</runOnConfiguration>
28+
</execute>
29+
</action>
30+
</pluginExecution>
31+
</pluginExecutions>
32+
</lifecycleMappingMetadata>

0 commit comments

Comments
 (0)