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