Skip to content

Recompile when dependencies change - #1102

Open
wilx wants to merge 4 commits into
apache:maven-compiler-plugin-3.xfrom
wilx:issue-1087-dependency-state
Open

Recompile when dependencies change#1102
wilx wants to merge 4 commits into
apache:maven-compiler-plugin-3.xfrom
wilx:issue-1087-dependency-state

Conversation

@wilx

@wilx wilx commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

  • persist the ordered class path and module path in the plugin-local dependencies.lst state file
  • collect persisted metadata and the current-build timestamp fallback in one cached scan per normalized dependency path, removing the previous duplicate traversal
  • record regular dependency files as size and modification time, and exploded dependency directories as a stable metadata fingerprint
  • trigger the existing changed dependency rebuild reason when dependencies are added, removed, reordered, relocated, or replaced at the same path
  • add unit coverage for the consolidated detector and integration coverage for dependency removal and same-path SNAPSHOT replacement

Why

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 passed
  • JAVA_HOME=/opt/jdks/latest-21 /opt/maven/latest/bin/mvn clean verify — 36 tests passed
  • focused MCOMPILER-349, MCOMPILER-474, dependency-removal, and same-path-replacement integration tests passed
  • JAVA_HOME=/opt/jdks/latest-21 /opt/maven/latest/bin/mvn -Prun-its clean verify — 78 builds passed, 5 environment-specific skips

Fixes #1087


Following this checklist to help us incorporate your
contribution quickly and easily:

  • An issue is filed for this change: No recompilation if the dependencies have changed #1087. This pull request addresses only that issue.
  • Each commit in the pull request has a meaningful subject line and body.
  • The pull request title describes the issue. The template's [MCOMPILER-XXX] convention is not applicable because this is tracked as GitHub issue No recompilation if the dependencies have changed #1087.
  • The pull request description explains what the pull request does, how, and why.
  • Ran mvn clean verify.
  • Ran the integration tests successfully with 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.

@wilx
wilx marked this pull request as ready for review July 31, 2026 21:48
@wilx
wilx marked this pull request as draft July 31, 2026 22:19
@wilx
wilx force-pushed the issue-1087-dependency-state branch from 3d01630 to 665f6cf Compare July 31, 2026 22:42
@wilx
wilx marked this pull request as ready for review July 31, 2026 22:43
@wilx
wilx force-pushed the issue-1087-dependency-state branch from 665f6cf to 096cfb4 Compare July 31, 2026 23:25
}
if (changedDependency != null) {
if (configuration.log.isDebugEnabled() || configuration.showChanges) {
configuration.log.info("\tNew dependency detected: " + changedDependency.toAbsolutePath());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wouldn't log this above debug level

@wilx wilx Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I only see one call of this method?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree here with @wilx not needed to do more complicated

}
}

List<Path> files;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This belongs inside the try, as does all the code that depends on it. Don't split variable declaration and initialization

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

changedDependency = file;
}
} catch (IOException e) {
if (!unreadable) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should you just break out of the loop in this case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed by merging the two try/catch regions.

Comment thread src/main/java/org/apache/maven/plugin/compiler/DependencyState.java
Comment thread src/main/java/org/apache/maven/plugin/compiler/DependencyState.java
Comment thread src/main/java/org/apache/maven/plugin/compiler/DependencyState.java
Comment thread src/main/java/org/apache/maven/plugin/compiler/DependencyState.java

if (hasPreviousState && !oldState.equals(newState)) {
if (configuration.log.isDebugEnabled() || configuration.showChanges) {
logChanges(oldState, newState, configuration.log);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

checking debug here is good, but it's info level in the logChanges method. logChanges should log at debug level

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is already gated by maven.compiler.showCompilationChanges property.

@elharo
elharo requested a review from gnodet August 7, 2026 11:21
}
if (changedDependency != null) {
if (configuration.log.isDebugEnabled() || configuration.showChanges) {
configuration.log.info("\tNew dependency detected: " + changedDependency.toAbsolutePath());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I only see one call of this method?

*
* @return {@code true} if at least one single dependency has changed.
*/
protected boolean isDependencyChanged() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This method should be retained with protected access, even if it's implementation changes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ping

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure if we need to preserve all plugin internal methods.
Maven plugin is not library so should be not used as standard dependencies.

Even more we only recommended use as plugin not dependencies
https://maven.apache.org/plugins/maven-compiler-plugin/dependency-info.html

Comment thread src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java Outdated
@desruisseaux desruisseaux added enhancement New feature or request 3.x labels Aug 24, 2026
@slawekjaranowski

Copy link
Copy Markdown
Member

@wilx please rebase

wilx added 3 commits August 26, 2026 18:48
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
@wilx
wilx force-pushed the issue-1087-dependency-state branch from e237a3a to 12013c6 Compare August 26, 2026 16:52
@wilx

wilx commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

@wilx please rebase

Done.

@slawekjaranowski slawekjaranowski linked an issue Aug 26, 2026 that may be closed by this pull request
@slawekjaranowski

Copy link
Copy Markdown
Member

@elharo does your comments resolved?

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

some of my original comments are answered, a couple are not

*
* @return {@code true} if at least one single dependency has changed.
*/
protected boolean isDependencyChanged() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ping

StringBuilder buffer = new StringBuilder(bytes.length * 2);
for (byte value : bytes) {
int unsigned = value & 0xFF;
buffer.append(Character.forDigit(unsigned >>> 4, 16));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd just use Integer.toHexString here

Preserve the fixed two-character representation by prepending a zero when the converted byte has only one hexadecimal digit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No recompilation if the dependencies have changed

4 participants