Skip to content

Commit

Permalink
merged PR #85
Browse files Browse the repository at this point in the history
renamed property from 'skipWithoutSourceChanges' to 'skipSourcesUnchanged'
put PR code in a new method 'isSourcesUnchanged( List<> allSources )'
  • Loading branch information
bsorrentino committed Sep 24, 2020
1 parent 15fc483 commit 2bf7bed
Showing 1 changed file with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
Expand Down Expand Up @@ -282,8 +281,8 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo
*
* @since 4.3
*/
@Parameter(defaultValue = "false", property = "skipAnnotationProcessingWithoutSourceChanges")
protected boolean skipWithoutSourceChanges;
@Parameter(defaultValue = "false", property = "skipSourcesUnchangedAnnotationProcessing")

This comment has been minimized.

Copy link
@dashorst

dashorst Sep 24, 2020

Contributor

property = "skipSourcesUnchangedAnnotationProcessing")
should probably also read as "skipSourcesUnchanged"

protected boolean skipSourcesUnchanged;

/**
* Maven Session
Expand Down Expand Up @@ -573,6 +572,33 @@ 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();

// use atomic long for effectively final wrapper around long variable
final AtomicLong maxOutputDate = new AtomicLong(Long.MIN_VALUE);

Files.walkFileTree(outputDirectory.toPath(), new SimpleFileVisitor<>() {
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException
{
if(Files.isRegularFile(file)) {
maxOutputDate.updateAndGet(t -> Math.max(t, file.toFile().lastModified()));
}
return FileVisitResult.CONTINUE;
}
});

if(getLog().isDebugEnabled())
{
getLog().debug("max source file date: " + maxSourceDate + ", max output date: " + maxOutputDate
.get());
}

return maxSourceDate <= maxOutputDate.get();

}

private void executeWithExceptionsHandled() throws Exception
{
if (outputDirectory == null)
Expand Down Expand Up @@ -771,29 +797,7 @@ private void executeWithExceptionsHandled() throws Exception
return;
}

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

// use atomic long for effectively final wrapper around long variable
AtomicLong maxOutputDate = new AtomicLong(Long.MIN_VALUE);

Files.walkFileTree(outputDirectory.toPath(), new SimpleFileVisitor<>() {
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException
{
if(Files.isRegularFile(file)) {
maxOutputDate.updateAndGet(t -> Math.max(t, file.toFile().lastModified()));
}
return FileVisitResult.CONTINUE;
}
});

if(getLog().isDebugEnabled())
{
getLog().debug("max source file date: " + maxSourceDate + ", max output date: " + maxOutputDate
.get());
}

if(skipWithoutSourceChanges && maxSourceDate <= maxOutputDate.get()) {
if(skipSourcesUnchanged && isSourcesUnchanged(allSources)) {
getLog().info( "no source file(s) change(s) detected! Processor task will be skipped");
return;
}
Expand Down

1 comment on commit 2bf7bed

@dashorst
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good to me.

Personal preference is to define (private) methods below the first method that uses them. However, it is not my project so do as you please 👍

Please sign in to comment.