Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add m2e support #1414

Merged
merged 7 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin-maven/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +89,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Component
private ResourceManager resourceManager;

@Component
protected BuildContext buildContext;
lutovich marked this conversation as resolved.
Show resolved Hide resolved

@Parameter(defaultValue = "${mojoExecution.goal}", required = true, readonly = true)
private String goal;

Expand Down Expand Up @@ -344,10 +348,13 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -45,6 +45,7 @@ protected void process(Iterable<File> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,4 +38,25 @@ static UpToDateChecker noop(MavenProject project, Path indexFile, Log log) {
static UpToDateChecker forProject(MavenProject project, Path indexFile, Iterable<Formatter> 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be && instead of ||?

}

@Override
public void close() {
delegate.close();
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- metadata for m2e: https://www.eclipse.org/m2e/documentation/m2e-making-maven-plugins-compat.html -->
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>apply</goal>
<goal>check</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
<runOnConfiguration>false</runOnConfiguration>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>