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