Skip to content

Commit

Permalink
Merge branch 'detect-removals-as-changes' of https://github.com/dasho…
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 1, 2020
2 parents 3628f6e + 1f6c708 commit fb81aea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin-parent</artifactId>
<packaging>pom</packaging>
<version>4.3</version>
<version>4.4-SNAPSHOT</version>
<name>MAVEN PROCESSOR PLUGIN PARENT</name>
<description>A maven plugin to process annotation for jdk6 at compile time

Expand Down
2 changes: 1 addition & 1 deletion processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This plugin could be considered the 'alter ego' of maven apt plugin http://mojo.
<parent>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin-parent</artifactId>
<version>4.3</version>
<version>4.4-SNAPSHOT</version>
</parent>

<properties>
Expand Down
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
2 changes: 1 addition & 1 deletion test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin-parent</artifactId>
<version>4.3</version>
<version>4.4-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<parent>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin-parent</artifactId>
<version>4.3</version>
<version>4.4-SNAPSHOT</version>
</parent>

<properties>
Expand Down

0 comments on commit fb81aea

Please sign in to comment.