Fix compile cache artifact escalation - #498
Conversation
861f590 to
8b4e4a7
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes a lifecycle phase escalation bug where a mvn compile cache hit could “poison” subsequent mvn package/mvn install runs by restoring target/classes as the project’s main artifact. The change ensures compile outputs are cached/restored as attached output archives instead of being serialized as the main Maven artifact, and adds a compatibility guard for legacy cache entries that stored a directory-backed main artifact.
Changes:
- Cache compile outputs (
target/classes,target/test-classes) as attached outputs (zipped) rather than as the main project artifact. - Add restoration-time guards/reset logic to prevent legacy directory-backed main artifacts from satisfying
package-or-later requests. - Add/extend smoke ITs to assert buildinfo contents for
compile -> packageandcompile -> installescalation scenarios.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java | Stops treating directory-backed artifacts as main artifacts for compile-only cache; attaches compile outputs; adjusts save/restore/staging behavior accordingly. |
| src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java | Adds legacy-cache guarding and artifact reset logic when escalating to package or later phases. |
| src/test/java/org/apache/maven/buildcache/its/lifecyclephases/CompileThenPackageEscalationTest.java | Strengthens IT by asserting buildinfo.xml does not store a directory-backed main artifact and that package-level entries point to the JAR. |
| src/test/java/org/apache/maven/buildcache/its/lifecyclephases/CompileThenInstallEscalationTest.java | New IT covering compile -> install escalation with the same buildinfo assertions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // 2. Compile outputs and configured attached outputs (if cacheCompile enabled) | ||
| if (cacheConfig.isCacheCompile()) { | ||
| addExistingOutputPath(paths, project.getBuild().getOutputDirectory()); | ||
| addExistingOutputPath(paths, project.getBuild().getTestOutputDirectory()); | ||
| List<DirName> attachedDirs = cacheConfig.getAttachedOutputs(); |
| /** | ||
| * Saves a directory artifact by zipping it first, then saving the zip to cache. | ||
| */ | ||
| private void saveDirectoryArtifact( |
There was a problem hiding this comment.
[explanation] #Why saveDirectoryArtifact(...) is removed:
The method made a directory-backed Maven project artifact saveable by zipping it and storing it through the main artifact path. That preserved the invalid metadata shape that caused #449: target/classes is a compile output directory, not the project's packaged main artifact.
With this change, saveProjectArtifact(...) only saves regular files produced by package-or-later mojos. Compile outputs are still cached, but through attachCompileOutputs(...) / attached output archives, so jar modules keep compile-cache benefits without letting target/classes poison later package or install executions.
Store compile outputs as attached cache outputs instead of serializing target/classes as the main project artifact. This lets package/install builds restore compiled classes, run packaging mojos, and save the packaged archive as the main artifact. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
8b4e4a7 to
b534e3c
Compare
|
Please rebase against latest master. |
Summary
Fix compile-only cache entries so
target/classesis cached as an attached output archive instead of being serialized as the Maven project's main artifact.This prevents a
mvn compilecache hit from poisoning latermvn package/mvn installruns with a directory-backed main artifact while keeping compile output caching enabled for jar modules. The change also keeps a compatibility guard for legacy cache entries that already contain a directory-backed main artifact.A zipped standalone reproducer is included under
src/test/projects/maven-cache-install-after-compile-repro.zip.Tests
mvn testcompile -> packageandcompile -> installmaven-cache-install-after-compile-repro.zip