olamy commented on code in PR #522:
URL: 
https://github.com/apache/maven-build-cache-extension/pull/522#discussion_r3753935890


##########
src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java:
##########
@@ -261,6 +280,48 @@ private Source getSource(List<MojoExecution> 
mojoExecutions) {
         return Source.LIFECYCLE;
     }
 
+    /**
+     * Decides whether a set of goals typed on the command line can be cached.
+     * <p>
+     * Every goal has to be non-aggregator and bound by default (via {@code 
@Mojo(defaultPhase=...)}) to a real
+     * phase after clean. This keeps single-goal caching to goals that produce 
the same output the matching phase
+     * would — e.g. {@code compiler:compile} is really the {@code compile} 
phase — and it naturally leaves out
+     * long-running goals like {@code jetty:run} or {@code exec:java} (no 
default phase) and clean-bound goals.
+     *
+     * @param mojoExecutions the goals requested on the command line
+     * @return true if the whole invocation can go through the normal 
phase-based caching
+     */
+    private boolean isCacheableCliInvocation(List<MojoExecution> 
mojoExecutions) {
+        if (mojoExecutions == null || mojoExecutions.isEmpty()) {
+            return false;
+        }
+        for (MojoExecution mojoExecution : mojoExecutions) {
+            if (mojoExecution.getMojoDescriptor() == null
+                    || mojoExecution.getMojoDescriptor().isAggregator()) {
+                return false;
+            }

Review Comment:
   great report. should be fixed now



##########
src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java:
##########
@@ -116,8 +116,23 @@ public void execute(
             // so forks should rerun too
             boolean forkedExecution = 
lifecyclePhasesHelper.isForkedProject(project);
             String projectName = getVersionlessProjectKey(project);
+            // Full caching (look up, restore and save) applies to a normal 
lifecycle build, or to goals typed
+            // on the command line when they all map to a real phase after 
clean (e.g. mvn compiler:compile).
+            boolean cacheEligible = !forkedExecution
+                    && (source == Source.LIFECYCLE
+                            || (source == Source.CLI
+                                    && cacheConfig.isCacheSingleGoal()
+                                    && 
isCacheableCliInvocation(mojoExecutions)));
+            // When a command-line goal forks a lifecycle (e.g. jetty:run 
forks test-compile via
+            // @Execute(phase=...)), let that fork restore from cache but 
never save. We skip forks that happen
+            // inside a normal build: that build already handles caching, and 
joining in would just cause an
+            // extra, pointless cache lookup (see ForkedExecutionsTest / 
ForkedExecutionCoreExtensionTest).
+            boolean forkedRestoreEligible = forkedExecution
+                    && cacheConfig.isRestoreForkedExecutions()
+                    && source == Source.LIFECYCLE
+                    && isCliOriginatedFork(project);

Review Comment:
   great report. should be fixed now



-- 
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