Skip to content

Commit

Permalink
Detects file additions/deletions for skipping
Browse files Browse the repository at this point in the history
When you remove a file from the source tree, the modification checker
doesn't take into account the removed file as an update. Because the
file no longer exists, it doesn't have a modification date, and it will
not count in determining whether the last generation was older than the
modification in the sources.

This commit detects source file modifications of the nature of
additions and deletions by using a tracking file in the output folder
(`.maven-processor-source-files.txt`).

This file contains a list of all the files in the source folders, which
is checked against the current list of source files. When the file
doesn't exist or if the sets of files don't match, it is treated as a
change. When they match exactly, the modification time check is still
run.

A sample run without additions/removals in the source files:

```
[DEBUG]   (f) skipSourcesUnchanged = true
[DEBUG]   (f) sourceDirectory = /Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java
[DEBUG] -- end configuration --
[DEBUG] Source directory: /Users/dashorst/IdeaProjects/iridium/common/entities/target/generated-sources/apt added
[DEBUG] processing source directory [/Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java]
[DEBUG] removed source files: []
[DEBUG] new source files: []
[DEBUG] max source file date: 1601210991895, max output date: 1601211873845
[INFO] no source file(s) change(s) detected! Processor task will be skipped
```

When a file was removed:

```
[DEBUG]   (f) skipSourcesUnchanged = true
[DEBUG]   (f) sourceDirectory = /Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java
[DEBUG] -- end configuration --
[DEBUG] Source directory: /Users/dashorst/IdeaProjects/iridium/common/entities/target/generated-sources/apt added
[DEBUG] processing source directory [/Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java]
[DEBUG] removed source files: [/Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java/nl/topicus/platinum/entities/Afdeling.java]
[DEBUG] new source files: []
[WARNING] No processors specified. Using default discovery mechanism.
[DEBUG] javac option: -cp
```

When the file is re-added:
```
[DEBUG]   (f) skipSourcesUnchanged = true
[DEBUG]   (f) sourceDirectory = /Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java
[DEBUG] -- end configuration --
[DEBUG] Source directory: /Users/dashorst/IdeaProjects/iridium/common/entities/target/generated-sources/apt added
[DEBUG] processing source directory [/Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java]
[DEBUG] removed source files: []
[DEBUG] new source files: [/Users/dashorst/IdeaProjects/iridium/common/entities/src/main/java/nl/topicus/platinum/entities/Afdeling.java]
[WARNING] No processors specified. Using default discovery mechanism.
[DEBUG] javac option: -cp
```

Fixes #86
  • Loading branch information
dashorst committed Sep 27, 2020
1 parent cb7191d commit 1f6c708
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -573,7 +574,13 @@ private List<String> prepareOptions( JavaCompiler compiler ) {
}

private boolean isSourcesUnchanged( List<JavaFileObject> allSources ) throws IOException {
long maxSourceDate = allSources.stream().map(JavaFileObject::getLastModified).max(Long::compare).get();
if (!areSourceFilesSameAsPreviousRun(allSources))
return false;

long maxSourceDate = allSources.stream()
.map(JavaFileObject::getLastModified)
.max(Long::compare)
.orElse(Long.MIN_VALUE);

// use atomic long for effectively final wrapper around long variable
final AtomicLong maxOutputDate = new AtomicLong(Long.MIN_VALUE);
Expand All @@ -596,7 +603,41 @@ private boolean isSourcesUnchanged( List<JavaFileObject> allSources ) throws IOE
}

return maxSourceDate <= maxOutputDate.get();
}

/**
* Checks the list of {@code allSources} against the stored list of source files in a previous run.
*
* @param allSources
* @return {@code true} when the filenames of the previous run matches exactly with the current run.
* @throws IOException
*/
private boolean areSourceFilesSameAsPreviousRun(List<JavaFileObject> allSources) throws IOException {
Path sourceFileList = outputDirectory.toPath().resolve(".maven-processor-source-files.txt");
try {
if (!Files.exists(sourceFileList)) {
getLog().debug("File with previous sources " + sourceFileList + " not found, treating as first run");
return false;
}

Set<String> previousSourceFiles = new HashSet<>(Files.readAllLines(sourceFileList));
Set<String> currentSourceFiles = allSources.stream().map(JavaFileObject::getName).collect(Collectors.toSet());
if (getLog().isDebugEnabled()) {
Set<String> removedSourceFiles = previousSourceFiles.stream()
.filter(f -> !currentSourceFiles.contains(f))
.collect(Collectors.toSet());
getLog().debug("removed source files: " + removedSourceFiles);

Set<String> newSourceFiles = currentSourceFiles.stream()
.filter(f -> !previousSourceFiles.contains(f))
.collect(Collectors.toSet());
getLog().debug("new source files: " + newSourceFiles);
}
return previousSourceFiles.equals(currentSourceFiles);
} finally {
outputDirectory.mkdirs();
Files.write(sourceFileList, allSources.stream().map(JavaFileObject::getName).collect(Collectors.toSet()));
}
}

private void executeWithExceptionsHandled() throws Exception
Expand Down

0 comments on commit 1f6c708

Please sign in to comment.