24
24
25
25
import java .io .File ;
26
26
import java .nio .file .Path ;
27
+ import java .util .Collection ;
27
28
import java .util .HashSet ;
28
29
import java .util .List ;
29
30
import java .util .Map ;
30
31
import java .util .Set ;
32
+ import java .util .stream .Collectors ;
33
+ import java .util .stream .Stream ;
31
34
32
35
import org .apache .commons .lang3 .ArrayUtils ;
33
36
import org .apache .commons .lang3 .StringUtils ;
@@ -130,14 +133,14 @@ public void execute(
130
133
}
131
134
132
135
boolean restorable = result .isSuccess () || result .isPartialSuccess ();
133
- boolean restored = false ; // if partially restored need to save increment
136
+ CacheRestorationStatus restorationStatus =
137
+ CacheRestorationStatus .FAILURE ; // if partially restored need to save increment
134
138
if (restorable ) {
135
- CacheRestorationStatus cacheRestorationStatus =
136
- restoreProject (result , mojoExecutions , mojoExecutionRunner , cacheConfig );
137
- restored = CacheRestorationStatus .SUCCESS == cacheRestorationStatus ;
138
- executeExtraCleanPhaseIfNeeded (cacheRestorationStatus , cleanPhase , mojoExecutionRunner );
139
+ restorationStatus = restoreProject (result , mojoExecutions , mojoExecutionRunner , cacheConfig );
140
+ executeExtraCleanPhaseIfNeeded (restorationStatus , cleanPhase , mojoExecutionRunner );
139
141
}
140
- if (!restored ) {
142
+ if (restorationStatus != CacheRestorationStatus .SUCCESS
143
+ && restorationStatus != CacheRestorationStatus .INCREMENTAL_SUCCESS ) {
141
144
for (MojoExecution mojoExecution : mojoExecutions ) {
142
145
if (source == Source .CLI
143
146
|| mojoExecution .getLifecyclePhase () == null
@@ -147,7 +150,8 @@ public void execute(
147
150
}
148
151
}
149
152
150
- if (cacheState == INITIALIZED && (!result .isSuccess () || !restored )) {
153
+ if (cacheState == INITIALIZED
154
+ && (!result .isSuccess () || restorationStatus != CacheRestorationStatus .SUCCESS )) {
151
155
if (cacheConfig .isSkipSave ()) {
152
156
LOGGER .info ("Cache saving is disabled." );
153
157
} else if (cacheConfig .isMandatoryClean ()
@@ -228,20 +232,35 @@ private CacheRestorationStatus restoreProject(
228
232
// Verify cache consistency for cached mojos
229
233
LOGGER .debug ("Verify consistency on cached mojos" );
230
234
Set <MojoExecution > forcedExecutionMojos = new HashSet <>();
235
+ Set <MojoExecution > reconciliationExecutionMojos = new HashSet <>();
231
236
for (MojoExecution cacheCandidate : cachedSegment ) {
232
237
if (cacheController .isForcedExecution (project , cacheCandidate )) {
233
238
forcedExecutionMojos .add (cacheCandidate );
234
239
} else {
240
+ if (!reconciliationExecutionMojos .isEmpty ()) {
241
+ reconciliationExecutionMojos .add (cacheCandidate );
242
+ continue ;
243
+ }
235
244
if (!verifyCacheConsistency (
236
245
cacheCandidate , build , project , session , mojoExecutionRunner , cacheConfig )) {
237
- LOGGER .info ("A cached mojo is not consistent, continuing with non cached build" );
238
- return CacheRestorationStatus .FAILURE ;
246
+ if (!cacheConfig .isIncrementalReconciliationOnParameterMismatch ()) {
247
+ LOGGER .info ("A cached mojo is not consistent, continuing with non cached build" );
248
+ return CacheRestorationStatus .FAILURE ;
249
+ } else {
250
+ LOGGER .info ("A cached mojo is not consistent, will reconciliate from here" );
251
+ reconciliationExecutionMojos .add (cacheCandidate );
252
+ }
239
253
}
240
254
}
241
255
}
242
256
257
+ Set <MojoExecution > plannedExecutions = Stream .concat (
258
+ forcedExecutionMojos .stream (), reconciliationExecutionMojos .stream ())
259
+ .collect (Collectors .toSet ());
243
260
// Restore project artifacts
244
- ArtifactRestorationReport restorationReport = cacheController .restoreProjectArtifacts (cacheResult );
261
+ ArtifactRestorationReport restorationReport = cacheController .restoreProjectArtifacts (
262
+ cacheResult ,
263
+ !containsExecution (plannedExecutions , "org.apache.maven.plugins" , "maven-jar-plugin" , "jar" ));
245
264
if (!restorationReport .isSuccess ()) {
246
265
LOGGER .info ("Cannot restore project artifacts, continuing with non cached build" );
247
266
return restorationReport .isRestoredFilesInProjectDirectory ()
@@ -267,6 +286,12 @@ private CacheRestorationStatus restoreProject(
267
286
// mojoExecutionScope.seed(
268
287
// org.apache.maven.api.MojoExecution.class, new DefaultMojoExecution(cacheCandidate));
269
288
mojoExecutionRunner .run (cacheCandidate );
289
+ } else if (reconciliationExecutionMojos .contains (cacheCandidate )) {
290
+ LOGGER .info (
291
+ "Mojo execution is needed for reconciliation: {}" ,
292
+ cacheCandidate .getMojoDescriptor ().getFullGoalName ());
293
+ mojoExecutionScope .seed (MojoExecution .class , cacheCandidate );
294
+ mojoExecutionRunner .run (cacheCandidate );
270
295
} else {
271
296
LOGGER .info (
272
297
"Skipping plugin execution (cached): {}" ,
@@ -295,12 +320,34 @@ private CacheRestorationStatus restoreProject(
295
320
for (MojoExecution mojoExecution : postCachedSegment ) {
296
321
mojoExecutionRunner .run (mojoExecution );
297
322
}
298
- return CacheRestorationStatus .SUCCESS ;
323
+
324
+ if (reconciliationExecutionMojos .isEmpty ()) {
325
+ return CacheRestorationStatus .SUCCESS ;
326
+ } else {
327
+ return CacheRestorationStatus .INCREMENTAL_SUCCESS ;
328
+ }
299
329
} finally {
300
330
mojoExecutionScope .exit ();
301
331
}
302
332
}
303
333
334
+ private boolean containsExecution (
335
+ Collection <MojoExecution > executions , String groupId , String artifactId , String goal ) {
336
+ for (MojoExecution execution : executions ) {
337
+ if (!groupId .equals (execution .getGroupId ())) {
338
+ continue ;
339
+ }
340
+ if (!artifactId .equals (execution .getArtifactId ())) {
341
+ continue ;
342
+ }
343
+ if (!goal .equals (execution .getGoal ())) {
344
+ continue ;
345
+ }
346
+ return true ;
347
+ }
348
+ return false ;
349
+ }
350
+
304
351
private boolean verifyCacheConsistency (
305
352
MojoExecution cacheCandidate ,
306
353
Build cachedBuild ,
@@ -433,6 +480,7 @@ private static String normalizedPath(Path path, Path baseDirPath) {
433
480
434
481
private enum CacheRestorationStatus {
435
482
SUCCESS ,
483
+ INCREMENTAL_SUCCESS ,
436
484
FAILURE ,
437
485
FAILURE_NEEDS_CLEAN
438
486
}
0 commit comments