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