|
| 1 | +<!-- |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +or more contributor license agreements. See the NOTICE file |
| 4 | +distributed with this work for additional information |
| 5 | +regarding copyright ownership. The ASF licenses this file |
| 6 | +to you under the Apache License, Version 2.0 (the |
| 7 | +"License"); you may not use this file except in compliance |
| 8 | +with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, |
| 13 | +software distributed under the License is distributed on an |
| 14 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +KIND, either express or implied. See the License for the |
| 16 | +specific language governing permissions and limitations |
| 17 | +under the License. |
| 18 | +--> |
| 19 | + |
| 20 | +# Implementation Plan: Fix #375 - Validation-Time Property Capture |
| 21 | + |
| 22 | +## Problem |
| 23 | +Maven 4 automatically injects `--module-version ${project.version}` into compiler arguments during execution, but this happens AFTER the cache validation phase. This creates a timing mismatch: |
| 24 | + |
| 25 | +- **First build**: Properties captured during execution (WITH injection) |
| 26 | +- **Second build**: Properties captured during validation (WITHOUT injection yet) |
| 27 | +- **Result**: Cache invalidation due to parameter mismatch |
| 28 | + |
| 29 | +## Root Cause |
| 30 | +Properties are currently captured at different lifecycle points: |
| 31 | +- **Validation phase**: Uses `getConfiguredMojo()` to read properties (no injection yet) |
| 32 | +- **Execution phase**: Maven injects properties before `beforeMojoExecution()` fires |
| 33 | +- **Storage phase**: Reads from execution-time events (possibly with injection) |
| 34 | + |
| 35 | +## Solution |
| 36 | +Capture properties at **validation time** for ALL builds (not just when cache is found). This ensures consistent reading at the same lifecycle point. |
| 37 | + |
| 38 | +### Key Changes |
| 39 | + |
| 40 | +#### 1. Modified CacheResult.java |
| 41 | +- Added `validationTimeEvents` field to store mojo events captured during validation |
| 42 | +- Added overloaded factory methods to accept validation-time events |
| 43 | +- Added `getValidationTimeEvents()` getter |
| 44 | + |
| 45 | +#### 2. Modify BuildCacheMojosExecutionStrategy.java |
| 46 | +After calling `findCachedBuild()`, capture validation-time properties for all mojos: |
| 47 | + |
| 48 | +```java |
| 49 | +// After line 133: result = cacheController.findCachedBuild(...) |
| 50 | +Map<String, MojoExecutionEvent> validationTimeEvents = captureValidationTimeProperties( |
| 51 | + session, project, mojoExecutions |
| 52 | +); |
| 53 | +// Store in result using CacheResult.rebuilded() or new factory methods |
| 54 | +``` |
| 55 | + |
| 56 | +Add method: |
| 57 | +```java |
| 58 | +private Map<String, MojoExecutionEvent> captureValidationTimeProperties( |
| 59 | + MavenSession session, MavenProject project, List<MojoExecution> mojoExecutions |
| 60 | +) { |
| 61 | + Map<String, MojoExecutionEvent> events = new HashMap<>(); |
| 62 | + for (MojoExecution mojoExecution : mojoExecutions) { |
| 63 | + try { |
| 64 | + mojoExecutionScope.enter(); |
| 65 | + mojoExecutionScope.seed(MavenProject.class, project); |
| 66 | + mojoExecutionScope.seed(MojoExecution.class, mojoExecution); |
| 67 | + |
| 68 | + Mojo mojo = mavenPluginManager.getConfiguredMojo(Mojo.class, session, mojoExecution); |
| 69 | + MojoExecutionEvent event = new MojoExecutionEvent(session, project, mojoExecution, mojo); |
| 70 | + events.put(mojoExecutionKey(mojoExecution), event); |
| 71 | + |
| 72 | + mavenPluginManager.releaseMojo(mojo, mojoExecution); |
| 73 | + } catch (Exception e) { |
| 74 | + LOGGER.warn("Cannot capture validation-time properties for {}: {}", |
| 75 | + mojoExecution.getGoal(), e.getMessage()); |
| 76 | + } finally { |
| 77 | + mojoExecutionScope.exit(); |
| 78 | + } |
| 79 | + } |
| 80 | + return events; |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +#### 3. Modify BuildCacheMojosExecutionStrategy.execute() |
| 85 | +Pass validation-time events to save(): |
| 86 | + |
| 87 | +```java |
| 88 | +// Line 167: Change from: |
| 89 | +cacheController.save(result, mojoExecutions, executionEvents); |
| 90 | + |
| 91 | +// To: |
| 92 | +Map<String, MojoExecutionEvent> propertyEvents = result.getValidationTimeEvents() != null |
| 93 | + ? result.getValidationTimeEvents() |
| 94 | + : executionEvents; |
| 95 | +cacheController.save(result, mojoExecutions, propertyEvents); |
| 96 | +``` |
| 97 | + |
| 98 | +### Why This Works |
| 99 | + |
| 100 | +1. **First Build** (no cache): |
| 101 | + - `findCachedBuild()` returns empty result |
| 102 | + - Capture validation-time properties |
| 103 | + - Mojos execute (Maven 4 may inject properties) |
| 104 | + - `save()` uses validation-time properties (NO injection) |
| 105 | + |
| 106 | +2. **Second Build** (cache found): |
| 107 | + - `findCachedBuild()` validates using validation-time properties (NO injection) |
| 108 | + - Capture validation-time properties |
| 109 | + - Compare to stored values (BOTH without injection) |
| 110 | + - **Match!** Cache restored |
| 111 | + |
| 112 | +### Benefits |
| 113 | + |
| 114 | +- ✅ Eliminates timing mismatch |
| 115 | +- ✅ No need for `ignorePattern` workaround |
| 116 | +- ✅ Consistent property reading for all builds |
| 117 | +- ✅ Solves root cause instead of treating symptoms |
| 118 | +- ✅ No configuration required |
| 119 | +- ✅ Works for any Maven 4 auto-injected properties |
| 120 | + |
| 121 | +### Testing |
| 122 | +Will create integration tests for: |
| 123 | +1. JPMS module without explicit moduleVersion (Maven 4 auto-injects) |
| 124 | +2. JPMS module with empty moduleVersion |
| 125 | +3. JPMS module with null moduleVersion |
| 126 | +4. JPMS module with explicit moduleVersion |
| 127 | + |
| 128 | +All tests should show cache restoration on second build WITHOUT needing `ignorePattern`. |
| 129 | + |
| 130 | +## Comparison with PR #391 |
| 131 | + |
| 132 | +**PR #391 (ignorePattern approach)**: |
| 133 | +- Treats symptom: filters out mismatched values |
| 134 | +- Requires configuration |
| 135 | +- Pattern-based (fragile, version-format dependent) |
| 136 | +- Works around the timing problem |
| 137 | + |
| 138 | +**This PR (validation-time capture)**: |
| 139 | +- Fixes root cause: eliminates timing mismatch |
| 140 | +- No configuration needed |
| 141 | +- Format-agnostic |
| 142 | +- Solves the timing problem |
| 143 | + |
| 144 | +## Implementation Status |
| 145 | + |
| 146 | +- [x] Design solution |
| 147 | +- [x] Modify CacheResult.java |
| 148 | +- [ ] Modify BuildCacheMojosExecutionStrategy.java (captureValidationTimeProperties) |
| 149 | +- [ ] Modify BuildCacheMojosExecutionStrategy.execute() (pass validation events to save) |
| 150 | +- [ ] Run existing tests |
| 151 | +- [ ] Create integration tests |
| 152 | +- [ ] Create PR |
0 commit comments