View Javadoc
1   package org.apache.maven.model.profile;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Calculates the active profiles among a given collection of profiles.
39   *
40   * @author Benjamin Bentmann
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 }