Skip to content

Commit

Permalink
Merge branch 'feature/PR85' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Sep 25, 2020
2 parents c6dbeb9 + 2bf7bed commit 1b7e24d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 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.2</version>
<version>4.3-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.2</version>
<version>4.3-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
Expand Down Expand Up @@ -269,6 +275,15 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo
@Parameter(defaultValue = "false", property = "fork")
protected boolean fork;

/**
* Set this to true to skip annotation processing when there are no changes in the source files
* compared to the generated files.
*
* @since 4.3
*/
@Parameter(defaultValue = "false", property = "skipSourcesUnchangedAnnotationProcessing")
protected boolean skipSourcesUnchanged;

/**
* Maven Session
*
Expand Down Expand Up @@ -557,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 @@ -755,6 +797,10 @@ private void executeWithExceptionsHandled() throws Exception
return;
}

if(skipSourcesUnchanged && isSourcesUnchanged(allSources)) {
getLog().info( "no source file(s) change(s) detected! Processor task will be skipped");
return;
}
final List<String> options = prepareOptions( compiler );

final CompilationTask task = compiler.getTask(
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.2</version>
<version>4.3-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.2</version>
<version>4.3-SNAPSHOT</version>
</parent>

<properties>
Expand Down

0 comments on commit 1b7e24d

Please sign in to comment.