diff --git a/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java b/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java index 238d7ebe4..a87b539a3 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java +++ b/src/main/java/org/apache/maven/plugin/compiler/DependencyState.java @@ -137,6 +137,15 @@ private static Path addDependencies( * The directory digest covers each relevant file's relative path, size and modification time. */ private static ModificationState modificationState(Path dependency, ScanContext context) { + if (Files.notExists(dependency)) { + // A reactor module with no sources for this output kind (e.g. no main sources, so + // `target/classes` is never created) still appears on the classpath. Treat it like an + // empty directory instead of letting `readAttributes` below fail with a spurious + // "I/O error reading dependency state" warning and an "unreadable" state that never + // compares equal across builds, which defeated incremental compilation (forced a + // recompile on every run). + return new ModificationState(0 + ":" + toHexString(newDigest().digest()), null); + } if (!Files.isDirectory(dependency)) { try { BasicFileAttributes attributes = readAttributes(dependency); diff --git a/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java b/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java index b8c60581f..b4eebaa44 100644 --- a/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java +++ b/src/test/java/org/apache/maven/plugin/compiler/DependencyStateTest.java @@ -34,7 +34,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.contains; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class DependencyStateTest { @@ -113,6 +116,33 @@ void preservesRepeatedDependencyEntriesAndPathKinds() throws Exception { assertTrue(state.get(2).startsWith("modulepath:")); } + @Test + void treatsMissingDependencyDirectoryAsEmptyWithoutWarningOrForcedRebuild() throws Exception { + Path dependency = temporaryDirectory.resolve("classes-not-yet-compiled"); + IncrementalBuildHelper helper = mock(IncrementalBuildHelper.class); + when(helper.getMojoStatusDirectory()).thenReturn(temporaryDirectory.toFile()); + Log log = mock(Log.class); + + DependencyState.Configuration configuration = + new DependencyState.Configuration(Collections.emptySet(), BUILD_START, 0, log, false); + List classpath = Collections.singletonList(dependency.toString()); + + assertFalse(DependencyState.hasChanged( + helper, + temporaryDirectory.resolve("output").toFile(), + classpath, + Collections.emptyList(), + configuration)); + assertFalse(DependencyState.hasChanged( + helper, + temporaryDirectory.resolve("output").toFile(), + classpath, + Collections.emptyList(), + configuration)); + + verify(log, never()).warn(contains("I/O error")); + } + @Test void retainsCurrentBuildDetectionWhenStatusDirectoryIsUnavailable() throws Exception { Path dependency = dependency("dependency.jar", 3_000, 1);