jazdw opened a new issue, #471:
URL: https://github.com/apache/maven-build-cache-extension/issues/471

   ## Summary
   
   Running `mvn compile` followed by `mvn install` fails when the shade plugin 
is configured.
   The shade plugin reports "project main artifact does not exist" even though 
`jar:jar` ran
   successfully and created the jar file on disk.
   
   This is a regression introduced in **1.2.2** by the "Save attached outputs 
for compile-only
   cache entries" feature. Version **1.2.1** does not have this issue.
   
   **Minimal reproducer:** 
https://github.com/jazdw/maven-build-cache-compile-bug
   
   ## Reproduction
   
   Requires Maven 3.9+ and JDK 17+.
   
   ```bash
   # Clean any existing cache entries
   rm -rf ~/.m2/build-cache/*/com.example/
   
   # Step 1: compile (creates a compile-only cache entry)
   mvn compile
   
   # Step 2: install (fails)
   mvn install
   ```
   
   ### Expected behavior
   
   `mvn install` performs a partial restore (skipping compilation), then runs 
`jar:jar` and
   `shade:shade` successfully.
   
   ### Actual behavior
   
   `jar:jar` runs and creates the jar, but `shade:shade` fails:
   
   ```
   [INFO] --- jar:3.4.1:jar (default-jar) @ cache-compile-bug ---
   [INFO] Building jar: .../target/cache-compile-bug-1.0.0-SNAPSHOT.jar
   
   [INFO] --- shade:3.6.2:shade (default) @ cache-compile-bug ---
   [ERROR] The project main artifact does not exist.
   [ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-shade-plugin:3.6.2:shade
           (default) on project cache-compile-bug: Failed to create shaded 
artifact,
           project main artifact does not exist.
   ```
   
   ## Root cause
   
   In `CacheControllerImpl.restoreProjectArtifacts()`, the project artifact is 
replaced with a
   `RestoredArtifact` instance via 
`project.setArtifact(restoredProjectArtifact)`.
   
   `RestoredArtifact.getFile()` always passes the file through 
`restoreToDiskConsumer`:
   
   ```java
   // RestoredArtifact.java
   @Override
   public File getFile() {
       // ...
       File file = fileFuture.get();
       return restoreToDiskConsumer.apply(file);  // <-- always applied
   }
   
   @Override
   public void setFile(File destination) {
       this.fileFuture = CompletableFuture.completedFuture(destination);
       // restoreToDiskConsumer is NOT cleared
   }
   ```
   
   The `restoreToDiskConsumer` created in 
`CacheControllerImpl.createRestorationToDiskConsumer()`
   captures a `restorationPath` (e.g. `target/classes`) and **always returns 
it**, ignoring
   the actual file parameter:
   
   ```java
   // CacheControllerImpl.java, createRestorationToDiskConsumer()
   return file -> {
       if (restored.compareAndSet(false, true)) {
           // ... do restoration ...
       }
       return restorationPath.toFile();  // always returns target/classes
   };
   ```
   
   So the sequence is:
   
   1. Cache extension restores compile-only entry, sets project artifact to 
`RestoredArtifact`
      whose `getFile()` returns `target/classes` (a directory)
   2. `jar:jar` runs, creates the jar, calls `artifact.setFile(jarFile)` — this 
updates
      `fileFuture` but does NOT clear `restoreToDiskConsumer`
   3. `shade:shade` calls `artifact.getFile()` → gets `jarFile` from 
`fileFuture` → passes
      through `restoreToDiskConsumer` → **returns `target/classes`** (ignoring 
`jarFile`)
   4. Shade checks `getFile().isFile()` → false (it's a directory) → error
   
   ## Suggested fix
   
   In `RestoredArtifact.setFile()`, clear the `restoreToDiskConsumer` so that 
subsequent
   `getFile()` calls return the file directly:
   
   ```java
   @Override
   public void setFile(File destination) {
       this.fileFuture = CompletableFuture.completedFuture(destination);
       this.restoreToDiskConsumer = file -> file;  // bypass restoration after 
explicit set
   }
   ```
   
   ## Workaround
   
   Set `-Dmaven.build.cache.cacheCompile=false` to disable saving compile-only 
cache entries.
   This restores the v1.2.1 behavior where compile-only builds save only 
`buildinfo.xml`
   (no artifact), so subsequent install builds don't encounter the corrupted 
artifact state.
   
   ## Versions
   
   - maven-build-cache-extension: **1.2.2** (regression, works in 1.2.1)
   - Maven: 3.9.9
   - maven-shade-plugin: 3.6.2
   - maven-jar-plugin: 3.4.1
   - JDK: 25 (also reproduced with 17)
   - OS: Windows 11, also likely affects Linux/macOS


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to