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
43 @Named
44 @Singleton
45 @Deprecated(since = "4.0.0")
46 public class DefaultProfileSelector implements ProfileSelector {
47
48 @Inject
49 private List<ProfileActivator> activators = new ArrayList<>();
50
51 public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
52 if (profileActivator != null) {
53 activators.add(profileActivator);
54 }
55 return this;
56 }
57
58 @Override
59 public List<Profile> getActiveProfiles(
60 Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {
61 Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
62 Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());
63
64 List<Profile> activeProfiles = new ArrayList<>(profiles.size());
65 List<Profile> activePomProfilesByDefault = new ArrayList<>();
66 boolean activatedPomProfileNotByDefault = false;
67
68 for (Profile profile : profiles) {
69 if (!deactivatedIds.contains(profile.getId())) {
70 if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
71 activeProfiles.add(profile);
72
73 if (Profile.SOURCE_POM.equals(profile.getSource())) {
74 activatedPomProfileNotByDefault = true;
75 }
76 } else if (isActiveByDefault(profile)) {
77 if (Profile.SOURCE_POM.equals(profile.getSource())) {
78 activePomProfilesByDefault.add(profile);
79 } else {
80 activeProfiles.add(profile);
81 }
82 }
83 }
84 }
85
86 if (!activatedPomProfileNotByDefault) {
87 activeProfiles.addAll(activePomProfilesByDefault);
88 }
89
90 return activeProfiles;
91 }
92
93 private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
94 boolean isActive = false;
95 for (ProfileActivator activator : activators) {
96 if (activator.presentInConfig(profile, context, problems)) {
97 isActive = true;
98 }
99 }
100 for (ProfileActivator activator : activators) {
101 try {
102 if (activator.presentInConfig(profile, context, problems)) {
103 isActive &= activator.isActive(profile, context, problems);
104 }
105 } catch (RuntimeException e) {
106 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
107 .setMessage(
108 "Failed to determine activation for profile " + profile.getId() + ": " + e.getMessage())
109 .setLocation(profile.getLocation(""))
110 .setException(e));
111 return false;
112 }
113 }
114 return isActive;
115 }
116
117 private boolean isActiveByDefault(Profile profile) {
118 Activation activation = profile.getActivation();
119 return activation != null && activation.isActiveByDefault();
120 }
121 }