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.List;
24
25 import org.apache.maven.api.di.Inject;
26 import org.apache.maven.api.di.Named;
27 import org.apache.maven.api.di.Singleton;
28 import org.apache.maven.api.model.Activation;
29 import org.apache.maven.api.model.Profile;
30 import org.apache.maven.api.services.BuilderProblem.Severity;
31 import org.apache.maven.api.services.ModelProblem.Version;
32 import org.apache.maven.api.services.ModelProblemCollector;
33 import org.apache.maven.api.services.model.ProfileActivationContext;
34 import org.apache.maven.api.services.model.ProfileActivator;
35 import org.apache.maven.api.services.model.ProfileSelector;
36
37
38
39
40
41 @Named
42 @Singleton
43 public class DefaultProfileSelector implements ProfileSelector {
44
45 private final List<ProfileActivator> activators;
46
47 public DefaultProfileSelector() {
48 this.activators = new ArrayList<>();
49 }
50
51 @Inject
52 public DefaultProfileSelector(List<ProfileActivator> activators) {
53 this.activators = new ArrayList<>(activators);
54 }
55
56 public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
57 if (profileActivator != null) {
58 activators.add(profileActivator);
59 }
60 return this;
61 }
62
63 @Override
64 public List<Profile> getActiveProfiles(
65 Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {
66 List<Profile> activeProfiles = new ArrayList<>(profiles.size());
67 List<Profile> activePomProfilesByDefault = new ArrayList<>();
68 boolean activatedPomProfileNotByDefault = false;
69
70 for (Profile profile : profiles) {
71 if (!context.isProfileInactive(profile.getId())) {
72 if (context.isProfileActive(profile.getId()) || isActive(profile, context, problems)) {
73 activeProfiles.add(profile);
74 if (Profile.SOURCE_POM.equals(profile.getSource())) {
75 activatedPomProfileNotByDefault = true;
76 }
77 } else if (isActiveByDefault(profile)) {
78 if (Profile.SOURCE_POM.equals(profile.getSource())) {
79 activePomProfilesByDefault.add(profile);
80 } else {
81 activeProfiles.add(profile);
82 }
83 }
84 }
85 }
86
87 if (!activatedPomProfileNotByDefault) {
88 activeProfiles.addAll(activePomProfilesByDefault);
89 }
90
91 return activeProfiles;
92 }
93
94 private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
95 boolean isActive = false;
96 for (ProfileActivator activator : activators) {
97 if (activator.presentInConfig(profile, context, problems)) {
98 isActive = true;
99 try {
100 if (!activator.isActive(profile, context, problems)) {
101 return false;
102 }
103 } catch (RuntimeException e) {
104 problems.add(
105 Severity.ERROR,
106 Version.BASE,
107 "Failed to determine activation for profile " + profile.getId() + ": " + e.getMessage(),
108 profile.getLocation(""),
109 e);
110 return false;
111 }
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 }