Skip to content

Commit 3853efb

Browse files
authored
Add m2e support (#1414)
2 parents 6dfa398 + 52ea9fb commit 3853efb

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

plugin-maven/build.gradle

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.eclipse.aether.RepositorySystem;
5151
import org.eclipse.aether.RepositorySystemSession;
5252
import org.eclipse.aether.repository.RemoteRepository;
53+
import org.sonatype.plexus.build.incremental.BuildContext;
5354

5455
import com.diffplug.spotless.Formatter;
5556
import com.diffplug.spotless.LineEnding;
@@ -88,6 +89,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
8889
@Component
8990
private ResourceManager resourceManager;
9091

92+
@Component
93+
protected BuildContext buildContext;
94+
9195
@Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true)
9296
private String goal;
9397

@@ -344,10 +348,13 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> formatters) {
344348
Path targetDir = project.getBasedir().toPath().resolve(project.getBuild().getDirectory());
345349
indexFile = targetDir.resolve(DEFAULT_INDEX_FILE_NAME);
346350
}
351+
final UpToDateChecker checker;
347352
if (upToDateChecking != null && upToDateChecking.isEnabled()) {
348353
getLog().info("Up-to-date checking enabled");
349-
return UpToDateChecker.forProject(project, indexFile, formatters, getLog());
354+
checker = UpToDateChecker.forProject(project, indexFile, formatters, getLog());
355+
} else {
356+
checker = UpToDateChecker.noop(project, indexFile, getLog());
350357
}
351-
return UpToDateChecker.noop(project, indexFile, getLog());
358+
return UpToDateChecker.wrapWithBuildContext(checker, buildContext);
352359
}
353360
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 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.
@@ -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

Lines changed: 25 additions & 0 deletions
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,28 @@ 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+
if (buildContext.hasDelta(file.toFile())) {
53+
return delegate.isUpToDate(file);
54+
}
55+
return true;
56+
}
57+
58+
@Override
59+
public void close() {
60+
delegate.close();
61+
}
62+
};
63+
}
64+
4065
}
Lines changed: 32 additions & 0 deletions
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)