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