Recompile when dependencies change - #1102
Conversation
3d01630 to
665f6cf
Compare
Persist ordered classpath and module-path dependency state between plugin invocations. Compare file metadata and stable directory fingerprints while preserving current-build timestamp detection in the same scan, so removed, reordered, relocated, and same-path replaced dependencies trigger recompilation without duplicate directory traversal. Fixes apache#1087
665f6cf to
096cfb4
Compare
| } | ||
| if (changedDependency != null) { | ||
| if (configuration.log.isDebugEnabled() || configuration.showChanges) { | ||
| configuration.log.info("\tNew dependency detected: " + changedDependency.toAbsolutePath()); |
There was a problem hiding this comment.
I wouldn't log this above debug level
There was a problem hiding this comment.
The log.info is there because it is possible to get the line printed with user supplied maven.compiler.showCompilationChanges property. If it were log.debug it would not print if the user asked via the property. I can change it to debug for configuration.log.isDebugEnabled() and keep the info for configuration.showChanges. But that seems unnecessarily nuanced.
| } | ||
| } | ||
|
|
||
| List<Path> files; |
There was a problem hiding this comment.
This belongs inside the try, as does all the code that depends on it. Don't split variable declaration and initialization
| changedDependency = file; | ||
| } | ||
| } catch (IOException e) { | ||
| if (!unreadable) { |
There was a problem hiding this comment.
should you just break out of the loop in this case?
There was a problem hiding this comment.
Fixed by merging the two try/catch regions.
| } | ||
|
|
||
| private static String fileMetadata(BasicFileAttributes attributes) { | ||
| return attributes.size() + ":" + attributes.lastModifiedTime().toMillis(); |
There was a problem hiding this comment.
Seems wonky to be using a string where this method is called instead of the raw attributes.
There was a problem hiding this comment.
It fits into how it is stored and used. This way the checksum of files in a dir is only computed via checksum of their metadata checksums/strings, there is only one update function. Doing anything else would complicate things unnecessarily.
| digest.update((byte) 0); | ||
| } | ||
|
|
||
| private static String toHexString(byte[] bytes) { |
There was a problem hiding this comment.
This method isn't very risky, but I wouldn't be surprised if there's a utility method to do this somewhere in the classpath already, and if so we should use it. I think there's one in the JDK circa Java 17 but we might not depend on that version yet.
There was a problem hiding this comment.
I can't see any function that would be widely available.
|
|
||
| private static MessageDigest newDigest() { | ||
| try { | ||
| return MessageDigest.getInstance("SHA-256"); |
There was a problem hiding this comment.
unusual choice. Most of the time it would be SHA-1 for speed or SHA-512 for security. I'm not sure securtiy matters here. Does it?
There was a problem hiding this comment.
Why unusual? It seems to be used a lot everywhere. It is AFAIK what Git has chosen as successor of SHA-1. It is a middle ground between the speed and security.
| log.info("\tDependency path (+): " + dependencyAdded); | ||
| } | ||
| for (String dependencyRemoved : pathChanges.getRemoved()) { | ||
| log.info("\tDependency path (-): " + dependencyRemoved); |
There was a problem hiding this comment.
feels like this should all be debug level
There was a problem hiding this comment.
It is already gated by maven.compiler.showCompilationChanges property.
|
|
||
| if (hasPreviousState && !oldState.equals(newState)) { | ||
| if (configuration.log.isDebugEnabled() || configuration.showChanges) { | ||
| logChanges(oldState, newState, configuration.log); |
There was a problem hiding this comment.
checking debug here is good, but it's info level in the logChanges method. logChanges should log at debug level
There was a problem hiding this comment.
It is already gated by maven.compiler.showCompilationChanges property.
Summary
dependencies.lststate filechanged dependencyrebuild reason when dependencies are added, removed, reordered, relocated, or replaced at the same pathWhy
The 3.x incremental check only considered dependency timestamps relative to the current Maven session. It could therefore miss a dependency removed between invocations or a JAR replaced at the same path before the next invocation, leaving stale application bytecode.
The persisted comparison and the existing session-relative fallback are now consolidated. A missing baseline still detects dependencies rebuilt during the current Maven invocation, while unchanged builds no longer walk exploded dependencies once for persisted state and again for the timestamp fallback.
Validation
JAVA_HOME=/opt/jdks/latest-8 /opt/maven/latest/bin/mvn clean verify— 36 tests passedJAVA_HOME=/opt/jdks/latest-21 /opt/maven/latest/bin/mvn clean verify— 36 tests passedJAVA_HOME=/opt/jdks/latest-21 /opt/maven/latest/bin/mvn -Prun-its clean verify— 78 builds passed, 5 environment-specific skipsFixes #1087
Following this checklist to help us incorporate your
contribution quickly and easily:
[MCOMPILER-XXX]convention is not applicable because this is tracked as GitHub issue No recompilation if the dependencies have changed #1087.mvn clean verify.mvn -Prun-its clean verify.If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.
To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.
I hereby declare this contribution to be licenced under the Apache License Version 2.0, January 2004
In any other case, please file an Apache Individual Contributor License Agreement.