1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
25
26 import org.apache.maven.api.di.Inject;
27 import org.apache.maven.api.di.Named;
28 import org.apache.maven.api.di.Singleton;
29 import org.apache.maven.api.model.Activation;
30 import org.apache.maven.api.model.Profile;
31 import org.apache.maven.api.services.BuilderProblem.Severity;
32 import org.apache.maven.api.services.ModelProblem.Version;
33 import org.apache.maven.api.services.ModelProblemCollector;
34 import org.apache.maven.api.services.model.*;
35
36
37
38
39
40 @Named
41 @Singleton
42 public class DefaultProfileSelector implements ProfileSelector {
43
44 private final List<ProfileActivator> activators;
45
46 public DefaultProfileSelector() {
47 this.activators = new ArrayList<>();
48 }
49
50 @Inject
51 public DefaultProfileSelector(List<ProfileActivator> activators) {
52 this.activators = new ArrayList<>(activators);
53 }
54
55 public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
56 if (profileActivator != null) {
57 activators.add(profileActivator);
58 }
59 return this;
60 }
61
62 @Override
63 public List<Profile> getActiveProfiles(
64 Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {
65 Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
66 Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());
67
68 List<Profile> activeProfiles = new ArrayList<>(profiles.size());
69 List<Profile> activePomProfilesByDefault = new ArrayList<>();
70 boolean activatedPomProfileNotByDefault = false;
71
72 for (Profile profile : profiles) {
73 if (!deactivatedIds.contains(profile.getId())) {
74 if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
75 activeProfiles.add(profile);
76 if (Profile.SOURCE_POM.equals(profile.getSource())) {
77 activatedPomProfileNotByDefault = true;
78 }
79 } else if (isActiveByDefault(profile)) {
80 if (Profile.SOURCE_POM.equals(profile.getSource())) {
81 activePomProfilesByDefault.add(profile);
82 } else {
83 activeProfiles.add(profile);
84 }
85 }
86 }
87 }
88
89 if (!activatedPomProfileNotByDefault) {
90 activeProfiles.addAll(activePomProfilesByDefault);
91 }
92
93 return activeProfiles;
94 }
95
96 private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
97 boolean isActive = false;
98 for (ProfileActivator activator : activators) {
99 if (activator.presentInConfig(profile, context, problems)) {
100 isActive = true;
101 try {
102 if (!activator.isActive(profile, context, problems)) {
103 return false;
104 }
105 } catch (RuntimeException e) {
106 problems.add(
107 Severity.ERROR,
108 Version.BASE,
109 "Failed to determine activation for profile " + profile.getId(),
110 profile.getLocation(""),
111 e);
112 return false;
113 }
114 }
115 }
116 return isActive;
117 }
118
119 private boolean isActiveByDefault(Profile profile) {
120 Activation activation = profile.getActivation();
121 return activation != null && activation.isActiveByDefault();
122 }
123 }