View Javadoc

1   package org.apache.maven.plugins.enforcer;
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.List;
24  
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * This rule checks that some profiles are active.
34   *
35   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
36   */
37  public class RequireActiveProfile
38      extends AbstractNonCacheableEnforcerRule
39  {
40  
41      /** Comma separated list of profiles to check.
42       *  
43       * @deprecated the visibility will be reduced to private with the next major version
44       * @see {@link #setProfiles(String)}
45       * @see {@link #getProfiles()}
46       */
47      public String profiles = null;
48  
49      /** If all profiles must be active. If false, only one must be active
50       *
51       * @deprecated the visibility will be reduced to private with the next major version
52       * @see {@link #setAll(boolean)}
53       * @see {@link #isAll()}
54       */
55      public boolean all = true;
56      
57      public final String getProfiles()
58      {
59          return profiles;
60      }
61      
62      public final void setProfiles( String profiles )
63      {
64          this.profiles = profiles;
65      }
66      
67      public final boolean isAll()
68      {
69          return all;
70      }
71      
72      public final void setAll( boolean all )
73      {
74          this.all = all;
75      }
76  
77      /*
78       * (non-Javadoc)
79       *
80       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
81       */
82      public void execute( EnforcerRuleHelper theHelper )
83          throws EnforcerRuleException
84      {
85          List<String> missingProfiles = new ArrayList<String>();
86          try
87          {
88              MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
89              if ( StringUtils.isNotEmpty( profiles ) )
90              {
91                  String[] profs = profiles.split( "," );
92                  for ( String profile : profs )
93                  {
94                      if ( !isProfileActive( project, profile ) )
95                      {
96                          missingProfiles.add( profile );
97                      }
98                  }
99  
100                 boolean fail = false;
101                 if ( !missingProfiles.isEmpty() )
102                 {
103                     fail = true;
104                     // if (all && missingProfiles.size() != profs.length)
105                     // {
106                     // fail = true;
107                     // }
108                     // else
109                     // {
110                     // if (!all && missingProfiles.size() >= (profs.length -1))
111                     // {
112                     // fail = true;
113                     // }
114                     // }
115                 }
116 
117                 if ( fail )
118                 {
119                     String message = getMessage();
120                     StringBuilder buf = new StringBuilder();
121                     if ( message != null )
122                     {
123                         buf.append( message + "\n" );
124                     }
125 
126                     for ( String profile : missingProfiles )
127                     {
128                         buf.append( "Profile \"" + profile + "\" is not activated.\n" );
129                     }
130 
131                     throw new EnforcerRuleException( buf.toString() );
132                 }
133 
134             }
135 
136         }
137         catch ( ExpressionEvaluationException e )
138         {
139             throw new EnforcerRuleException( "Unable to retrieve the project.", e );
140         }
141 
142     }
143 
144     /**
145      * Checks if profile is active.
146      *
147      * @param project the project
148      * @param profileName the profile name
149      * @return <code>true</code> if profile is active, otherwise <code>false</code>
150      */
151     protected boolean isProfileActive( MavenProject project, String profileName )
152     {
153         @SuppressWarnings( "unchecked" )
154         List<Profile> activeProfiles = project.getActiveProfiles();
155         if ( activeProfiles != null && !activeProfiles.isEmpty() )
156         {
157             for ( Profile profile : activeProfiles )
158             {
159                 if ( profile.getId().equals( profileName ) )
160                 {
161                     return true;
162                 }
163             }
164         }
165 
166         return false;
167     }
168 }