|
26 | 26 | import java.io.IOException; |
27 | 27 | import java.util.ArrayList; |
28 | 28 | import java.util.Collection; |
| 29 | +import java.util.Collections; |
29 | 30 | import java.util.HashMap; |
30 | 31 | import java.util.Iterator; |
31 | 32 | import java.util.LinkedHashSet; |
32 | 33 | import java.util.List; |
33 | 34 | import java.util.Map; |
34 | 35 | import java.util.Objects; |
| 36 | +import java.util.Optional; |
35 | 37 | import java.util.Properties; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.function.Consumer; |
| 40 | +import java.util.stream.Collectors; |
36 | 41 |
|
37 | 42 | import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
38 | 43 | import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; |
39 | 44 | import org.apache.maven.artifact.versioning.VersionRange; |
40 | 45 | import org.apache.maven.model.Activation; |
41 | | -import org.apache.maven.model.ActivationFile; |
42 | 46 | import org.apache.maven.model.Build; |
43 | 47 | import org.apache.maven.model.Dependency; |
44 | 48 | import org.apache.maven.model.DependencyManagement; |
45 | 49 | import org.apache.maven.model.InputLocation; |
| 50 | +import org.apache.maven.model.InputLocationTracker; |
46 | 51 | import org.apache.maven.model.InputSource; |
47 | 52 | import org.apache.maven.model.Model; |
48 | 53 | import org.apache.maven.model.Parent; |
|
78 | 83 | import org.apache.maven.model.validation.ModelValidator; |
79 | 84 | import org.codehaus.plexus.interpolation.InterpolationException; |
80 | 85 | import org.codehaus.plexus.interpolation.MapBasedValueSource; |
| 86 | +import org.codehaus.plexus.interpolation.RegexBasedInterpolator; |
81 | 87 | import org.codehaus.plexus.interpolation.StringSearchInterpolator; |
82 | 88 | import org.codehaus.plexus.util.StringUtils; |
83 | 89 | import org.eclipse.sisu.Nullable; |
@@ -299,14 +305,19 @@ protected ModelBuildingResult build(ModelBuildingRequest request, Collection<Str |
299 | 305 |
|
300 | 306 | profileActivationContext.setProjectProperties(tmpModel.getProperties()); |
301 | 307 |
|
302 | | - List<Profile> activePomProfiles = |
303 | | - profileSelector.getActiveProfiles(rawModel.getProfiles(), profileActivationContext, problems); |
304 | | - currentData.setActiveProfiles(activePomProfiles); |
305 | | - |
306 | 308 | Map<String, Activation> interpolatedActivations = |
307 | 309 | getInterpolatedActivations(rawModel, profileActivationContext, problems); |
308 | 310 | injectProfileActivations(tmpModel, interpolatedActivations); |
309 | 311 |
|
| 312 | + List<Profile> activePomProfiles = |
| 313 | + profileSelector.getActiveProfiles(tmpModel.getProfiles(), profileActivationContext, problems); |
| 314 | + |
| 315 | + Set<String> activeProfileIds = |
| 316 | + activePomProfiles.stream().map(Profile::getId).collect(Collectors.toSet()); |
| 317 | + currentData.setActiveProfiles(rawModel.getProfiles().stream() |
| 318 | + .filter(p -> activeProfileIds.contains(p.getId())) |
| 319 | + .collect(Collectors.toList())); |
| 320 | + |
310 | 321 | // profile injection |
311 | 322 | for (Profile activeProfile : activePomProfiles) { |
312 | 323 | profileInjector.injectProfile(tmpModel, activeProfile, request, problems); |
@@ -413,40 +424,72 @@ protected ModelBuildingResult build(ModelBuildingRequest request, Collection<Str |
413 | 424 | return result; |
414 | 425 | } |
415 | 426 |
|
| 427 | + @FunctionalInterface |
| 428 | + private interface InterpolateString { |
| 429 | + String apply(String s) throws InterpolationException; |
| 430 | + } |
| 431 | + |
416 | 432 | private Map<String, Activation> getInterpolatedActivations( |
417 | 433 | Model rawModel, DefaultProfileActivationContext context, DefaultModelProblemCollector problems) { |
418 | 434 | Map<String, Activation> interpolatedActivations = getProfileActivations(rawModel, true); |
419 | | - for (Activation activation : interpolatedActivations.values()) { |
420 | | - if (activation.getFile() != null) { |
421 | | - replaceWithInterpolatedValue(activation.getFile(), context, problems); |
422 | | - } |
| 435 | + |
| 436 | + if (interpolatedActivations.isEmpty()) { |
| 437 | + return Collections.emptyMap(); |
423 | 438 | } |
424 | | - return interpolatedActivations; |
425 | | - } |
| 439 | + RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); |
426 | 440 |
|
427 | | - private void replaceWithInterpolatedValue( |
428 | | - ActivationFile activationFile, ProfileActivationContext context, DefaultModelProblemCollector problems) { |
429 | | - try { |
430 | | - if (StringUtils.isNotEmpty(activationFile.getExists())) { |
431 | | - String path = activationFile.getExists(); |
432 | | - String absolutePath = profileActivationFilePathInterpolator.interpolate(path, context); |
433 | | - activationFile.setExists(absolutePath); |
434 | | - } else if (StringUtils.isNotEmpty(activationFile.getMissing())) { |
435 | | - String path = activationFile.getMissing(); |
436 | | - String absolutePath = profileActivationFilePathInterpolator.interpolate(path, context); |
437 | | - activationFile.setMissing(absolutePath); |
| 441 | + interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties())); |
| 442 | + interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties())); |
| 443 | + interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties())); |
| 444 | + |
| 445 | + class Interpolation { |
| 446 | + final InputLocationTracker target; |
| 447 | + |
| 448 | + final InterpolateString impl; |
| 449 | + |
| 450 | + Interpolation(InputLocationTracker target, InterpolateString impl) { |
| 451 | + this.target = target; |
| 452 | + this.impl = impl; |
| 453 | + } |
| 454 | + |
| 455 | + void performFor(String value, String locationKey, Consumer<String> mutator) { |
| 456 | + if (StringUtils.isEmpty(value)) { |
| 457 | + return; |
| 458 | + } |
| 459 | + try { |
| 460 | + mutator.accept(impl.apply(value)); |
| 461 | + } catch (InterpolationException e) { |
| 462 | + problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) |
| 463 | + .setMessage("Failed to interpolate value " + value + ": " + e.getMessage()) |
| 464 | + .setLocation(target.getLocation(locationKey)) |
| 465 | + .setException(e)); |
| 466 | + } |
438 | 467 | } |
439 | | - } catch (InterpolationException e) { |
440 | | - String path = StringUtils.isNotEmpty(activationFile.getExists()) |
441 | | - ? activationFile.getExists() |
442 | | - : activationFile.getMissing(); |
443 | | - |
444 | | - problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE) |
445 | | - .setMessage("Failed to interpolate file location " + path + ": " + e.getMessage()) |
446 | | - .setLocation(activationFile.getLocation( |
447 | | - StringUtils.isNotEmpty(activationFile.getExists()) ? "exists" : "missing")) |
448 | | - .setException(e)); |
449 | 468 | } |
| 469 | + for (Activation activation : interpolatedActivations.values()) { |
| 470 | + Optional<Activation> a = Optional.of(activation); |
| 471 | + a.map(Activation::getFile).ifPresent(fa -> { |
| 472 | + Interpolation nt = |
| 473 | + new Interpolation(fa, s -> profileActivationFilePathInterpolator.interpolate(s, context)); |
| 474 | + nt.performFor(fa.getExists(), "exists", fa::setExists); |
| 475 | + nt.performFor(fa.getMissing(), "missing", fa::setMissing); |
| 476 | + }); |
| 477 | + a.map(Activation::getOs).ifPresent(oa -> { |
| 478 | + Interpolation nt = new Interpolation(oa, interpolator::interpolate); |
| 479 | + nt.performFor(oa.getArch(), "arch", oa::setArch); |
| 480 | + nt.performFor(oa.getFamily(), "family", oa::setFamily); |
| 481 | + nt.performFor(oa.getName(), "name", oa::setName); |
| 482 | + nt.performFor(oa.getVersion(), "version", oa::setVersion); |
| 483 | + }); |
| 484 | + a.map(Activation::getProperty).ifPresent(pa -> { |
| 485 | + Interpolation nt = new Interpolation(pa, interpolator::interpolate); |
| 486 | + nt.performFor(pa.getName(), "name", pa::setName); |
| 487 | + nt.performFor(pa.getValue(), "value", pa::setValue); |
| 488 | + }); |
| 489 | + a.map(Activation::getJdk).ifPresent(ja -> new Interpolation(activation, interpolator::interpolate) |
| 490 | + .performFor(ja, "jdk", activation::setJdk)); |
| 491 | + } |
| 492 | + return interpolatedActivations; |
450 | 493 | } |
451 | 494 |
|
452 | 495 | @Override |
|
0 commit comments