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