From 1f0397c6976d02ffa034441900145ac526e651d8 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 1 Dec 2022 12:30:05 +0100 Subject: [PATCH 1/7] Add m2e support This closes #1413 --- plugin-maven/build.gradle | 1 + .../spotless/maven/AbstractSpotlessMojo.java | 10 ++++-- .../spotless/maven/SpotlessApplyMojo.java | 1 + .../maven/incremental/UpToDateChecker.java | 22 +++++++++++++ .../m2e/lifecycle-mapping-metadata.xml | 32 +++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 plugin-maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml diff --git a/plugin-maven/build.gradle b/plugin-maven/build.gradle index b7ac8a36c8..3ea73e6101 100644 --- a/plugin-maven/build.gradle +++ b/plugin-maven/build.gradle @@ -76,6 +76,7 @@ dependencies { implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}" implementation("org.codehaus.plexus:plexus-resources:${VER_PLEXUS_RESOURCES}") implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}" + implementation 'org.sonatype.plexus:plexus-build-api:0.0.7' testImplementation project(":testlib") testImplementation "org.junit.jupiter:junit-jupiter:${VER_JUNIT}" diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index f163fa2802..a0180e6d75 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -88,6 +88,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Component private ResourceManager resourceManager; + @Component + protected BuildContext buildContext; + @Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true) private String goal; @@ -344,10 +347,13 @@ private UpToDateChecker createUpToDateChecker(Iterable formatters) { Path targetDir = project.getBasedir().toPath().resolve(project.getBuild().getDirectory()); indexFile = targetDir.resolve(DEFAULT_INDEX_FILE_NAME); } + final UpToDateChecker checker; if (upToDateChecking != null && upToDateChecking.isEnabled()) { getLog().info("Up-to-date checking enabled"); - return UpToDateChecker.forProject(project, indexFile, formatters, getLog()); + checker = UpToDateChecker.forProject(project, indexFile, formatters, getLog()); + } else { + checker = UpToDateChecker.noop(project, indexFile, getLog()); } - return UpToDateChecker.noop(project, indexFile, getLog()); + return UpToDateChecker.wrapWithBuildContext(checker, buildContext); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java index 6ea67240ee..4f3fb64f5a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java @@ -45,6 +45,7 @@ protected void process(Iterable files, Formatter formatter, UpToDateChecke PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file); if (!dirtyState.isClean() && !dirtyState.didNotConverge()) { dirtyState.writeCanonicalTo(file); + buildContext.refresh(file); } } catch (IOException e) { throw new MojoExecutionException("Unable to format file " + file, e); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index 2fd2f3a1ea..6dd21728e3 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -19,6 +19,7 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; +import org.sonatype.plexus.build.incremental.BuildContext; import com.diffplug.spotless.Formatter; @@ -37,4 +38,25 @@ static UpToDateChecker noop(MavenProject project, Path indexFile, Log log) { static UpToDateChecker forProject(MavenProject project, Path indexFile, Iterable formatters, Log log) { return IndexBasedChecker.create(project, indexFile, formatters, log); } + + static UpToDateChecker wrapWithBuildContext(UpToDateChecker delegate, BuildContext buildContext) { + return new UpToDateChecker() { + + @Override + public void setUpToDate(Path file) { + delegate.setUpToDate(file); + } + + @Override + public boolean isUpToDate(Path file) { + return !buildContext.hasDelta(file.toFile()) || delegate.isUpToDate(file); + } + + @Override + public void close() { + delegate.close(); + } + }; + } + } diff --git a/plugin-maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml b/plugin-maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml new file mode 100644 index 0000000000..9000b83704 --- /dev/null +++ b/plugin-maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml @@ -0,0 +1,32 @@ + + + + + + + + apply + check + + + + + true + false + + + + + \ No newline at end of file From a7c0f67a76da52d80f575fdf43ac481906210999 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 1 Dec 2022 12:41:37 +0100 Subject: [PATCH 2/7] fix formatting --- .../java/com/diffplug/spotless/maven/SpotlessApplyMojo.java | 2 +- .../spotless/maven/incremental/UpToDateChecker.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java index 4f3fb64f5a..7a15d01b17 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index 6dd21728e3..f63b8b4dfc 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -38,10 +38,10 @@ static UpToDateChecker noop(MavenProject project, Path indexFile, Log log) { static UpToDateChecker forProject(MavenProject project, Path indexFile, Iterable formatters, Log log) { return IndexBasedChecker.create(project, indexFile, formatters, log); } - + static UpToDateChecker wrapWithBuildContext(UpToDateChecker delegate, BuildContext buildContext) { return new UpToDateChecker() { - + @Override public void setUpToDate(Path file) { delegate.setUpToDate(file); @@ -58,5 +58,5 @@ public void close() { } }; } - + } From fa232426a5ea26cee7a8e483b3a81c3677a1c1d1 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 1 Dec 2022 12:45:57 +0100 Subject: [PATCH 3/7] add missing import --- .../java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index a0180e6d75..e4fdc7dcd0 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -50,6 +50,7 @@ import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.repository.RemoteRepository; +import org.sonatype.plexus.build.incremental.BuildContext; import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; From b7b17a9831095685622934f34133585c55517c4e Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sat, 3 Dec 2022 09:37:48 +0100 Subject: [PATCH 4/7] replace OR by AND --- .../diffplug/spotless/maven/incremental/UpToDateChecker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index f63b8b4dfc..be971cb374 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -49,7 +49,7 @@ public void setUpToDate(Path file) { @Override public boolean isUpToDate(Path file) { - return !buildContext.hasDelta(file.toFile()) || delegate.isUpToDate(file); + return !buildContext.hasDelta(file.toFile()) && delegate.isUpToDate(file); } @Override From b22f54fe8367051e3a3bd969e1c7155713beb1e3 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sat, 3 Dec 2022 09:48:00 +0100 Subject: [PATCH 5/7] fix condition to only evaluate IndexBasedChecker when necessary --- .../diffplug/spotless/maven/incremental/UpToDateChecker.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index be971cb374..bda325d992 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -49,7 +49,10 @@ public void setUpToDate(Path file) { @Override public boolean isUpToDate(Path file) { - return !buildContext.hasDelta(file.toFile()) && delegate.isUpToDate(file); + if (buildContext.hasDelta(file)) { + return delegate.isUpToDate(file); + } + return true; } @Override From fc02156bdc8c265a20eb475e48438e93af783412 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 5 Dec 2022 09:53:10 +0100 Subject: [PATCH 6/7] fix compilation error and make field private --- .../java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java | 2 +- .../diffplug/spotless/maven/incremental/UpToDateChecker.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index e4fdc7dcd0..2f3f0e4ab2 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -90,7 +90,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { private ResourceManager resourceManager; @Component - protected BuildContext buildContext; + private BuildContext buildContext; @Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true) private String goal; diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java index bda325d992..40dc84c8c6 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecker.java @@ -49,7 +49,7 @@ public void setUpToDate(Path file) { @Override public boolean isUpToDate(Path file) { - if (buildContext.hasDelta(file)) { + if (buildContext.hasDelta(file.toFile())) { return delegate.isUpToDate(file); } return true; From 52ea9fb8e1694245ce74f1c03b3e3ffbdcddbdb1 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 5 Dec 2022 09:56:08 +0100 Subject: [PATCH 7/7] revert making field private, as accessed in subclasses --- .../java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 2f3f0e4ab2..e4fdc7dcd0 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -90,7 +90,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { private ResourceManager resourceManager; @Component - private BuildContext buildContext; + protected BuildContext buildContext; @Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true) private String goal;