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      public String profiles = null;
43  
44      /** If all profiles must be active. If false, only one must be active */
45      public boolean all = true;
46  
47      /*
48       * (non-Javadoc)
49       *
50       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
51       */
52      public void execute( EnforcerRuleHelper theHelper )
53          throws EnforcerRuleException
54      {
55          List<String> missingProfiles = new ArrayList<String>();
56          try
57          {
58              MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
59              if ( StringUtils.isNotEmpty( profiles ) )
60              {
61                  String[] profs = profiles.split( "," );
62                  for ( String profile : profs )
63                  {
64                      if ( !isProfileActive( project, profile ) )
65                      {
66                          missingProfiles.add( profile );
67                      }
68                  }
69  
70                  boolean fail = false;
71                  if ( !missingProfiles.isEmpty() )
72                  {
73                      fail = true;
74                      // if (all && missingProfiles.size() != profs.length)
75                      // {
76                      // fail = true;
77                      // }
78                      // else
79                      // {
80                      // if (!all && missingProfiles.size() >= (profs.length -1))
81                      // {
82                      // fail = true;
83                      // }
84                      // }
85                  }
86  
87                  if ( fail )
88                  {
89                      StringBuilder buf = new StringBuilder();
90                      if ( message != null )
91                      {
92                          buf.append( message + "\n" );
93                      }
94  
95                      for ( String profile : missingProfiles )
96                      {
97                          buf.append( "Profile \"" + profile + "\" is not activated.\n" );
98                      }
99  
100                     throw new EnforcerRuleException( buf.toString() );
101                 }
102 
103             }
104 
105         }
106         catch ( ExpressionEvaluationException e )
107         {
108             throw new EnforcerRuleException( "Unable to retrieve the project.", e );
109         }
110 
111     }
112 
113     /**
114      * Checks if profile is active.
115      *
116      * @param project the project
117      * @param profileName the profile name
118      * @return <code>true</code> if profile is active, otherwise <code>false</code>
119      */
120     protected boolean isProfileActive( MavenProject project, String profileName )
121     {
122         @SuppressWarnings( "unchecked" )
123         List<Profile> activeProfiles = project.getActiveProfiles();
124         if ( activeProfiles != null && !activeProfiles.isEmpty() )
125         {
126             for ( Profile profile : activeProfiles )
127             {
128                 if ( profile.getId().equals( profileName ) )
129                 {
130                     return true;
131                 }
132             }
133         }
134 
135         return false;
136     }
137 }