diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 6c95cadc35..4347c8117e 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Support custom index files for incremental up-to-date checking ([#1055](https://github.com/diffplug/spotless/pull/1055)). ### Fixed * Remove Java files from default Maven Groovy formatting ([#1051](https://github.com/diffplug/spotless/pull/1051)). * Before this release, the default target of groovy was diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 316291d38a..fd379c8fa8 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -936,6 +936,7 @@ enable incremental up-to-date checking with the following configuration: true + ${project.basedir}/custom-index-file @@ -946,7 +947,7 @@ The index file contains source file paths and corresponding last modified timest It allows Spotless to skip already formatted files that have not changed. **Note:** the index file is located in the `target` directory. Executing `mvn clean` will delete -the index file, and Spotless will need to check/format all the source files. +the index file, and Spotless will need to check/format all the source files. You can override the default index file location with the `indexFile` configuration parameter. Spotless will remove the index file when up-to-date checking is explicitly turned off with the following configuration: @@ -955,6 +956,7 @@ following configuration: false + ${project.basedir}/custom-index-file 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 c28b4e7186..1b5abe9f0c 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 @@ -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. @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -71,7 +72,7 @@ import com.diffplug.spotless.maven.typescript.Typescript; public abstract class AbstractSpotlessMojo extends AbstractMojo { - + private static final String DEFAULT_INDEX_FILE_NAME = "spotless-index"; private static final String DEFAULT_ENCODING = "UTF-8"; private static final String DEFAULT_LINE_ENDINGS = "GIT_ATTRIBUTES"; static final String GOAL_CHECK = "check"; @@ -324,10 +325,15 @@ private List getFormatterStepFactories() { } private UpToDateChecker createUpToDateChecker(Iterable formatters) { + Path indexFile = upToDateChecking == null ? null : upToDateChecking.getIndexFile(); + if (indexFile == null) { + Path targetDir = project.getBasedir().toPath().resolve(project.getBuild().getDirectory()); + indexFile = targetDir.resolve(DEFAULT_INDEX_FILE_NAME); + } if (upToDateChecking != null && upToDateChecking.isEnabled()) { getLog().info("Up-to-date checking enabled"); - return UpToDateChecker.forProject(project, formatters, getLog()); + return UpToDateChecker.forProject(project, indexFile, formatters, getLog()); } - return UpToDateChecker.noop(project, getLog()); + return UpToDateChecker.noop(project, indexFile, getLog()); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java index 5ea1dcb8b5..9875602fb2 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndexConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,18 +20,17 @@ import org.apache.maven.project.MavenProject; class FileIndexConfig { - - private static final String INDEX_FILE_NAME = "spotless-index"; - private final MavenProject project; private final PluginFingerprint pluginFingerprint; + private final Path indexFile; - FileIndexConfig(MavenProject project) { - this(project, PluginFingerprint.empty()); + FileIndexConfig(MavenProject project, Path indexFile) { + this(project, indexFile, PluginFingerprint.empty()); } - FileIndexConfig(MavenProject project, PluginFingerprint pluginFingerprint) { + FileIndexConfig(MavenProject project, Path indexFile, PluginFingerprint pluginFingerprint) { this.project = project; + this.indexFile = indexFile; this.pluginFingerprint = pluginFingerprint; } @@ -40,8 +39,7 @@ Path getProjectDir() { } Path getIndexFile() { - Path targetDir = getProjectDir().resolve(project.getBuild().getDirectory()); - return targetDir.resolve(INDEX_FILE_NAME); + return indexFile; } PluginFingerprint getPluginFingerprint() { diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java index e58c479f2e..1ae32a4b60 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/IndexBasedChecker.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,9 +39,9 @@ class IndexBasedChecker implements UpToDateChecker { this.log = log; } - static IndexBasedChecker create(MavenProject project, Iterable formatters, Log log) { + static IndexBasedChecker create(MavenProject project, Path indexFile, Iterable formatters, Log log) { PluginFingerprint pluginFingerprint = PluginFingerprint.from(project, formatters); - FileIndexConfig indexConfig = new FileIndexConfig(project, pluginFingerprint); + FileIndexConfig indexConfig = new FileIndexConfig(project, indexFile, pluginFingerprint); FileIndex fileIndex = FileIndex.read(indexConfig, log); return new IndexBasedChecker(fileIndex, log); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java index 0814938f5f..8d57c0be4a 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/NoopChecker.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,8 +24,8 @@ class NoopChecker implements UpToDateChecker { private NoopChecker() {} - static NoopChecker create(MavenProject project, Log log) { - FileIndexConfig indexConfig = new FileIndexConfig(project); + static NoopChecker create(MavenProject project, Path indexFile, Log log) { + FileIndexConfig indexConfig = new FileIndexConfig(project, indexFile); FileIndex.delete(indexConfig, log); return new NoopChecker(); } 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 0851679707..2fd2f3a1ea 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 @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,11 +30,11 @@ public interface UpToDateChecker extends AutoCloseable { void close(); - static UpToDateChecker noop(MavenProject project, Log log) { - return NoopChecker.create(project, log); + static UpToDateChecker noop(MavenProject project, Path indexFile, Log log) { + return NoopChecker.create(project, indexFile, log); } - static UpToDateChecker forProject(MavenProject project, Iterable formatters, Log log) { - return IndexBasedChecker.create(project, formatters, log); + static UpToDateChecker forProject(MavenProject project, Path indexFile, Iterable formatters, Log log) { + return IndexBasedChecker.create(project, indexFile, formatters, log); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java index 098d24fb81..d5eb6aa941 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/UpToDateChecking.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,11 @@ */ package com.diffplug.spotless.maven.incremental; +import java.io.File; +import java.nio.file.Path; + +import javax.annotation.Nullable; + import org.apache.maven.plugins.annotations.Parameter; public class UpToDateChecking { @@ -22,7 +27,15 @@ public class UpToDateChecking { @Parameter private boolean enabled; + @Parameter + private String indexFile; + public boolean isEnabled() { return enabled; } + + @Nullable + public Path getIndexFile() { + return indexFile == null ? null : new File(indexFile).toPath(); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java index 17b3a338ba..4d167bc18f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexConfigTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.nio.file.Path; import java.nio.file.Paths; import org.apache.maven.model.Build; @@ -28,14 +29,16 @@ class FileIndexConfigTest { @Test void returnsCorrectProjectDir() { - FileIndexConfig config = new FileIndexConfig(mavenProject(), PluginFingerprint.from("foo")); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project), PluginFingerprint.from("foo")); assertThat(config.getProjectDir()).isEqualTo(Paths.get("projectDir")); } @Test void returnsCorrectIndexFile() { - FileIndexConfig config = new FileIndexConfig(mavenProject(), PluginFingerprint.from("foo")); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project), PluginFingerprint.from("foo")); assertThat(config.getIndexFile()) .isEqualTo(Paths.get("projectDir", "target", "spotless-index")); @@ -43,14 +46,16 @@ void returnsCorrectIndexFile() { @Test void returnsCorrectPluginFingerprint() { - FileIndexConfig config = new FileIndexConfig(mavenProject(), PluginFingerprint.from("foo")); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project), PluginFingerprint.from("foo")); assertThat(config.getPluginFingerprint()).isEqualTo(PluginFingerprint.from("foo")); } @Test void returnsEmptyPluginFingerprint() { - FileIndexConfig config = new FileIndexConfig(mavenProject()); + MavenProject project = mavenProject(); + FileIndexConfig config = new FileIndexConfig(project, getIndexFile(project)); assertThat(config.getPluginFingerprint()).isEqualTo(PluginFingerprint.from("")); } @@ -63,4 +68,8 @@ private static MavenProject mavenProject() { project.setBuild(build); return project; } + + private static Path getIndexFile(MavenProject project) { + return project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index"); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java index ae719273e1..97482f6f9f 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/NoopCheckerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,7 +52,7 @@ class NoopCheckerTest extends ResourceHarness { @BeforeEach void beforeEach() throws Exception { project = buildMavenProject(); - indexFile = new FileIndexConfig(project).getIndexFile(); + indexFile = project.getBasedir().toPath().resolve(project.getBuild().getDirectory()).resolve("spotless-index"); existingSourceFile = project.getBasedir().toPath().resolve("existing.txt"); Files.write(existingSourceFile, "foo".getBytes(UTF_8), CREATE_NEW); nonExistingSourceFile = project.getBasedir().toPath().resolve("non-existing.txt"); @@ -61,12 +61,12 @@ void beforeEach() throws Exception { @Test void deletesExistingIndexFileWhenCreated() { Log log = mock(Log.class); - try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, singletonList(dummyFormatter()), log)) { + try (UpToDateChecker realChecker = UpToDateChecker.forProject(project, indexFile, singletonList(dummyFormatter()), log)) { realChecker.setUpToDate(existingSourceFile); } assertThat(indexFile).exists(); - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, log)) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, log)) { assertThat(noopChecker).isNotNull(); } assertThat(indexFile).doesNotExist(); @@ -78,7 +78,7 @@ void doesNothingWhenIndexFileDoesNotExist() { assertThat(indexFile).doesNotExist(); Log log = mock(Log.class); - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, log)) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, log)) { assertThat(noopChecker).isNotNull(); } assertThat(indexFile).doesNotExist(); @@ -87,7 +87,7 @@ void doesNothingWhenIndexFileDoesNotExist() { @Test void neverUpToDate() { - try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, mock(Log.class))) { + try (UpToDateChecker noopChecker = UpToDateChecker.noop(project, indexFile, mock(Log.class))) { assertThat(noopChecker.isUpToDate(existingSourceFile)).isFalse(); assertThat(noopChecker.isUpToDate(nonExistingSourceFile)).isFalse(); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java index ffcd87c1fe..c0e4ac2495 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/UpToDateCheckingTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * Copyright 2021-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -65,6 +67,42 @@ void disableUpToDateChecking() throws Exception { assertFormatted(files); } + @Test + void enableUpToDateCheckingCustomIndexFile() throws Exception { + Path tempDirectory = newFolder("index-files").toPath(); + Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index"); + assertThat(indexFile.getParent()).doesNotExist(); + assertThat(indexFile).doesNotExist(); + writePomWithUpToDateCheckingEnabledIndexFile(true, tempDirectory + "/${project.groupId}/${project.artifactId}.index"); + + List files = writeUnformattedFiles(1); + String output = runSpotlessApply(); + + assertThat(output).contains("Up-to-date checking enabled"); + assertFormatted(files); + assertThat(indexFile.getParent()).exists(); + assertThat(indexFile).exists(); + } + + @Test + void disableUpToDateCheckingCustomIndexFile() throws Exception { + Path tempDirectory = newFolder("index-files").toPath(); + Path indexFile = tempDirectory.resolve("com.diffplug.spotless/spotless-maven-plugin-tests.index"); + Files.createDirectories(indexFile.getParent()); + Files.createFile(indexFile); + assertThat(indexFile.getParent()).exists(); + assertThat(indexFile).exists(); + writePomWithUpToDateCheckingEnabledIndexFile(false, tempDirectory + "/${project.groupId}/${project.artifactId}.index"); + + List files = writeUnformattedFiles(1); + String output = runSpotlessApply(); + + assertThat(output).doesNotContain("Up-to-date checking enabled"); + assertFormatted(files); + assertThat(indexFile.getParent()).exists(); + assertThat(indexFile).doesNotExist(); + } + @Test void spotlessApplyRecordsCorrectlyFormattedFiles() throws Exception { writePomWithUpToDateCheckingEnabled(true); @@ -141,6 +179,17 @@ private void writePomWithUpToDateCheckingEnabled(boolean enabled) throws IOExcep ""); } + private void writePomWithUpToDateCheckingEnabledIndexFile(boolean enabled, String indexFile) throws IOException { + writePom( + "", + " ", + "", + "", + " " + enabled + "", + " " + indexFile + "", + ""); + } + private List writeFormattedFiles(int count) throws IOException { return writeFiles("java/googlejavaformat/JavaCodeFormatted18.test", "formatted", count); }