forked from apache/maven-build-cache-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildCacheMojosExecutionStrategy.java
More file actions
441 lines (403 loc) · 20.6 KB
/
BuildCacheMojosExecutionStrategy.java
File metadata and controls
441 lines (403 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.buildcache;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.Strings;
import org.apache.maven.SessionScoped;
import org.apache.maven.buildcache.artifact.ArtifactRestorationReport;
import org.apache.maven.buildcache.checksum.MavenProjectInput;
import org.apache.maven.buildcache.xml.Build;
import org.apache.maven.buildcache.xml.CacheConfig;
import org.apache.maven.buildcache.xml.CacheState;
import org.apache.maven.buildcache.xml.DtoUtils;
import org.apache.maven.buildcache.xml.build.CompletedExecution;
import org.apache.maven.buildcache.xml.config.TrackedProperty;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.MojoExecutionEvent;
import org.apache.maven.execution.scope.internal.MojoExecutionScope;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecution.Source;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoExecutionRunner;
import org.apache.maven.plugin.MojosExecutionStrategy;
import org.apache.maven.plugin.PluginConfigurationException;
import org.apache.maven.plugin.PluginContainerException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.ReflectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.maven.buildcache.CacheUtils.mojoExecutionKey;
import static org.apache.maven.buildcache.checksum.KeyUtils.getVersionlessProjectKey;
import static org.apache.maven.buildcache.xml.CacheState.DISABLED;
import static org.apache.maven.buildcache.xml.CacheState.INITIALIZED;
/**
* Build cache-enabled version of the {@link MojosExecutionStrategy}.
*/
@SessionScoped
@Named
@Priority(10)
@SuppressWarnings("unused")
public class BuildCacheMojosExecutionStrategy implements MojosExecutionStrategy {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildCacheMojosExecutionStrategy.class);
private final CacheController cacheController;
private final CacheConfig cacheConfig;
private final MojoParametersListener mojoListener;
private final LifecyclePhasesHelper lifecyclePhasesHelper;
private final MavenPluginManager mavenPluginManager;
private final MojoExecutionScope mojoExecutionScope;
@Inject
public BuildCacheMojosExecutionStrategy(
CacheController cacheController,
CacheConfig cacheConfig,
MojoParametersListener mojoListener,
LifecyclePhasesHelper lifecyclePhasesHelper,
MavenPluginManager mavenPluginManager,
MojoExecutionScope mojoExecutionScope) {
this.cacheController = cacheController;
this.cacheConfig = cacheConfig;
this.mojoListener = mojoListener;
this.lifecyclePhasesHelper = lifecyclePhasesHelper;
this.mavenPluginManager = mavenPluginManager;
this.mojoExecutionScope = mojoExecutionScope;
}
public void execute(
List<MojoExecution> mojoExecutions, MavenSession session, MojoExecutionRunner mojoExecutionRunner)
throws LifecycleExecutionException {
try {
final MavenProject project = session.getCurrentProject();
final Source source = getSource(mojoExecutions);
// execute clean bound goals before restoring to not interfere/slowdown clean
CacheState cacheState = DISABLED;
CacheResult result = CacheResult.empty();
boolean skipCache = cacheConfig.isSkipCache() || MavenProjectInput.isSkipCache(project);
boolean cacheIsDisabled = MavenProjectInput.isCacheDisabled(project);
// Forked execution should be thought as a part of originating mojo internal implementation
// If forkedExecution is detected, it means that originating mojo is not cached so forks should rerun too
boolean forkedExecution = lifecyclePhasesHelper.isForkedProject(project);
String projectName = getVersionlessProjectKey(project);
List<MojoExecution> cleanPhase = null;
if (source == Source.LIFECYCLE && !forkedExecution) {
if (!cacheIsDisabled) {
cacheState = cacheConfig.initialize();
if (cacheState == INITIALIZED) {
// change mojoListener cacheState to INITIALIZED
mojoListener.setCacheState(cacheState);
}
LOGGER.info("Cache is {} on project level for {}", cacheState, projectName);
} else {
LOGGER.info("Cache is explicitly disabled on project level for {}", projectName);
}
cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
for (MojoExecution mojoExecution : cleanPhase) {
mojoExecutionRunner.run(mojoExecution);
}
if (cacheState == INITIALIZED || skipCache) {
result = cacheController.findCachedBuild(session, project, mojoExecutions, skipCache);
}
} else {
LOGGER.info("Cache is disabled on project level for {}", projectName);
}
boolean restorable = result.isSuccess() || result.isPartialSuccess();
boolean restored = false; // if partially restored need to save increment
if (restorable) {
CacheRestorationStatus cacheRestorationStatus =
restoreProject(result, mojoExecutions, mojoExecutionRunner, cacheConfig);
restored = CacheRestorationStatus.SUCCESS == cacheRestorationStatus;
executeExtraCleanPhaseIfNeeded(cacheRestorationStatus, cleanPhase, mojoExecutionRunner);
}
if (!restored) {
for (MojoExecution mojoExecution : mojoExecutions) {
if (source == Source.CLI
|| mojoExecution.getLifecyclePhase() == null
|| lifecyclePhasesHelper.isLaterPhaseThanClean(mojoExecution.getLifecyclePhase())) {
mojoExecutionRunner.run(mojoExecution);
}
}
}
if (cacheState == INITIALIZED && (!result.isSuccess() || !restored)) {
if (cacheConfig.isSkipSave()) {
LOGGER.info("Cache saving is disabled.");
} else if (cacheConfig.isMandatoryClean()
&& lifecyclePhasesHelper
.getCleanSegment(project, mojoExecutions)
.isEmpty()) {
LOGGER.info("Cache storing is skipped since there was no \"clean\" phase.");
} else {
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
cacheController.save(result, mojoExecutions, executionEvents);
}
}
if (cacheConfig.isFailFast() && !result.isSuccess() && !skipCache && !forkedExecution) {
throw new LifecycleExecutionException(
"Failed to restore project[" + projectName + "] from cache, failing build.", project);
}
} catch (MojoExecutionException e) {
throw new LifecycleExecutionException(e.getMessage(), e);
}
}
/**
* Cache configuration could demand to restore some files in the project directory (generated sources or even arbitrary content)
* If an error occurs during or after this kind of restoration AND a clean phase was required in the build :
* we execute an extra clean phase to remove any potential partially restored files
*
* @param cacheRestorationStatus the restoration status
* @param cleanPhase clean phase mojos
* @param mojoExecutionRunner mojo runner
* @throws LifecycleExecutionException
*/
private void executeExtraCleanPhaseIfNeeded(
final CacheRestorationStatus cacheRestorationStatus,
List<MojoExecution> cleanPhase,
MojoExecutionRunner mojoExecutionRunner)
throws LifecycleExecutionException {
if (CacheRestorationStatus.FAILURE_NEEDS_CLEAN == cacheRestorationStatus
&& cleanPhase != null
&& !cleanPhase.isEmpty()) {
LOGGER.info("Extra clean phase is executed as cache could be partially restored.");
for (MojoExecution mojoExecution : cleanPhase) {
mojoExecutionRunner.run(mojoExecution);
}
}
}
private Source getSource(List<MojoExecution> mojoExecutions) {
if (mojoExecutions == null || mojoExecutions.isEmpty()) {
return null;
}
for (MojoExecution mojoExecution : mojoExecutions) {
if (mojoExecution.getSource() == Source.CLI) {
return Source.CLI;
}
}
return Source.LIFECYCLE;
}
private CacheRestorationStatus restoreProject(
CacheResult cacheResult,
List<MojoExecution> mojoExecutions,
MojoExecutionRunner mojoExecutionRunner,
CacheConfig cacheConfig)
throws LifecycleExecutionException, MojoExecutionException {
final Build build = cacheResult.getBuildInfo();
final MavenProject project = cacheResult.getContext().getProject();
final MavenSession session = cacheResult.getContext().getSession();
final List<MojoExecution> cachedSegment =
lifecyclePhasesHelper.getCachedSegment(project, mojoExecutions, build);
// Verify cache consistency for cached mojos
LOGGER.debug("Verify consistency on cached mojos");
Set<MojoExecution> forcedExecutionMojos = new HashSet<>();
for (MojoExecution cacheCandidate : cachedSegment) {
if (cacheController.isForcedExecution(project, cacheCandidate)) {
forcedExecutionMojos.add(cacheCandidate);
} else {
if (!verifyCacheConsistency(
cacheCandidate, build, project, session, mojoExecutionRunner, cacheConfig)) {
LOGGER.info("A cached mojo is not consistent, continuing with non cached build");
return CacheRestorationStatus.FAILURE;
}
}
}
// Restore project artifacts
ArtifactRestorationReport restorationReport = cacheController.restoreProjectArtifacts(cacheResult);
if (!restorationReport.isSuccess()) {
LOGGER.info("Cannot restore project artifacts, continuing with non cached build");
return restorationReport.isRestoredFilesInProjectDirectory()
? CacheRestorationStatus.FAILURE_NEEDS_CLEAN
: CacheRestorationStatus.FAILURE;
}
// Execute mandatory mojos (forced by configuration)
LOGGER.debug("Execute mandatory mojos in the cache segment");
for (MojoExecution cacheCandidate : cachedSegment) {
if (forcedExecutionMojos.contains(cacheCandidate)) {
LOGGER.info(
"Mojo execution is forced by project property: {}",
cacheCandidate.getMojoDescriptor().getFullGoalName());
// need maven 4 as minumum
// mojoExecutionScope.seed(
// org.apache.maven.api.plugin.Log.class,
// new DefaultLog(LoggerFactory.getLogger(
// cacheCandidate.getMojoDescriptor().getFullGoalName())));
// mojoExecutionScope.seed(Project.class, ((DefaultSession)
// session.getSession()).getProject(project));
// mojoExecutionScope.seed(
// org.apache.maven.api.MojoExecution.class, new DefaultMojoExecution(cacheCandidate));
mojoExecutionRunner.run(cacheCandidate);
} else {
LOGGER.info(
"Skipping plugin execution (cached): {}",
cacheCandidate.getMojoDescriptor().getFullGoalName());
// Need to populate cached candidate executions for the build cache save result
Mojo mojo = null;
mojoExecutionScope.enter();
try {
mojoExecutionScope.seed(MavenProject.class, project);
mojoExecutionScope.seed(MojoExecution.class, cacheCandidate);
mojo = mavenPluginManager.getConfiguredMojo(Mojo.class, session, cacheCandidate);
MojoExecutionEvent mojoExecutionEvent =
new MojoExecutionEvent(session, project, cacheCandidate, mojo);
mojoListener.beforeMojoExecution(mojoExecutionEvent);
} catch (PluginConfigurationException | PluginContainerException e) {
throw new RuntimeException(e);
} finally {
mojoExecutionScope.exit();
if (mojo != null) {
mavenPluginManager.releaseMojo(mojo, cacheCandidate);
}
}
}
}
// Execute mojos after the cache segment
LOGGER.debug("Execute mojos post cache segment");
List<MojoExecution> postCachedSegment =
lifecyclePhasesHelper.getPostCachedSegment(project, mojoExecutions, build);
for (MojoExecution mojoExecution : postCachedSegment) {
mojoExecutionRunner.run(mojoExecution);
}
return CacheRestorationStatus.SUCCESS;
}
private boolean verifyCacheConsistency(
MojoExecution cacheCandidate,
Build cachedBuild,
MavenProject project,
MavenSession session,
MojoExecutionRunner mojoExecutionRunner,
CacheConfig cacheConfig)
throws LifecycleExecutionException {
long createdTimestamp = System.currentTimeMillis();
boolean consistent = true;
if (!cacheConfig.getTrackedProperties(cacheCandidate).isEmpty()) {
Mojo mojo = null;
try {
mojo = mavenPluginManager.getConfiguredMojo(Mojo.class, session, cacheCandidate);
final CompletedExecution completedExecution = cachedBuild.findMojoExecutionInfo(cacheCandidate);
final String fullGoalName = cacheCandidate.getMojoDescriptor().getFullGoalName();
if (completedExecution != null && !isParamsMatched(project, cacheCandidate, mojo, completedExecution)) {
LOGGER.info(
"Mojo cached parameters mismatch with actual, forcing full project build. Mojo: {}",
fullGoalName);
consistent = false;
}
if (consistent) {
long elapsed = System.currentTimeMillis() - createdTimestamp;
LOGGER.debug(
"Plugin execution will be skipped ({} : reconciled in {} millis)", elapsed, fullGoalName);
}
LOGGER.debug(
"Checked {}, resolved mojo: {}, cached params: {}", fullGoalName, mojo, completedExecution);
} catch (PluginContainerException | PluginConfigurationException e) {
throw new LifecycleExecutionException("Cannot get configured mojo", e);
} finally {
if (mojo != null) {
mavenPluginManager.releaseMojo(mojo, cacheCandidate);
}
}
} else {
LOGGER.debug(
"Plugin execution will be skipped ({} : cached)",
cacheCandidate.getMojoDescriptor().getFullGoalName());
}
return consistent;
}
boolean isParamsMatched(
MavenProject project, MojoExecution mojoExecution, Mojo mojo, CompletedExecution completedExecution) {
List<TrackedProperty> tracked = cacheConfig.getTrackedProperties(mojoExecution);
for (TrackedProperty trackedProperty : tracked) {
final String propertyName = trackedProperty.getPropertyName();
String expectedValue = DtoUtils.findPropertyValue(propertyName, completedExecution);
if (expectedValue == null) {
expectedValue = trackedProperty.getDefaultValue() != null ? trackedProperty.getDefaultValue() : "null";
}
final String currentValue;
try {
Object value = ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo);
if (value instanceof File) {
Path baseDirPath = project.getBasedir().toPath();
Path path = ((File) value).toPath();
currentValue = normalizedPath(path, baseDirPath);
} else if (value instanceof Path) {
Path baseDirPath = project.getBasedir().toPath();
currentValue = normalizedPath(((Path) value), baseDirPath);
} else if (value != null && value.getClass().isArray()) {
currentValue = ArrayFilterUtils.filterAndStringifyArray(value, trackedProperty.getIgnorePattern());
} else {
currentValue = String.valueOf(value);
}
} catch (IllegalAccessException e) {
LOGGER.error("Cannot extract plugin property {} from mojo {}", propertyName, mojo, e);
return false;
}
if (!Strings.CS.equals(currentValue, expectedValue)) {
if (!Strings.CS.equals(currentValue, trackedProperty.getSkipValue())) {
LOGGER.info(
"Plugin parameter mismatch found. Parameter: {}, expected: {}, actual: {}",
propertyName,
expectedValue,
currentValue);
return false;
} else {
LOGGER.warn(
"Cache contains plugin execution with skip flag and might be incomplete. "
+ "Property: {}, execution {}",
propertyName,
mojoExecutionKey(mojoExecution));
}
}
}
return true;
}
/**
* Best effort to normalize paths from Mojo fields.
* - all absolute paths under project root to be relativized for portability
* - redundant '..' and '.' to be removed to have consistent views on all paths
* - all relative paths are considered portable and should not be touched
* - absolute paths outside of project directory could not be deterministically relativized and not touched
*/
private static String normalizedPath(Path path, Path baseDirPath) {
boolean isProjectSubdir = path.isAbsolute() && path.startsWith(baseDirPath);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"normalizedPath isProjectSubdir {} path '{}' - baseDirPath '{}', path.isAbsolute() {}, path.startsWith(baseDirPath) {}",
isProjectSubdir,
path,
baseDirPath,
path.isAbsolute(),
path.startsWith(baseDirPath));
}
Path preparedPath = isProjectSubdir ? baseDirPath.relativize(path) : path;
String normalizedPath = preparedPath.normalize().toString();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("normalizedPath '{}' - {} return {}", path, baseDirPath, normalizedPath);
}
return normalizedPath;
}
private enum CacheRestorationStatus {
SUCCESS,
FAILURE,
FAILURE_NEEDS_CLEAN
}
}