Coverage Report - org.apache.maven.plugins.help.AllProfilesMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AllProfilesMojo
0%
0/101
0%
0/50
9.75
 
 1  
 package org.apache.maven.plugins.help;
 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.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import org.apache.maven.execution.MavenSession;
 29  
 import org.apache.maven.model.Profile;
 30  
 import org.apache.maven.plugin.MojoExecutionException;
 31  
 import org.apache.maven.plugin.MojoFailureException;
 32  
 import org.apache.maven.profiles.DefaultMavenProfilesBuilder;
 33  
 import org.apache.maven.profiles.DefaultProfileManager;
 34  
 import org.apache.maven.profiles.ProfileManager;
 35  
 import org.apache.maven.profiles.ProfilesConversionUtils;
 36  
 import org.apache.maven.profiles.ProfilesRoot;
 37  
 import org.apache.maven.profiles.activation.ProfileActivationException;
 38  
 import org.apache.maven.project.MavenProject;
 39  
 import org.apache.maven.settings.Settings;
 40  
 import org.apache.maven.settings.SettingsUtils;
 41  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 42  
 
 43  
 /**
 44  
  * Displays a list of available profiles under the current project.
 45  
  * <br/>
 46  
  * <b>Note</b>: it will list <b>all</b> profiles for a project. If a
 47  
  * profile comes up with a status <b>inactive</b> then there might be a need to
 48  
  * set profile activation switches/property.
 49  
  *
 50  
  * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 51  
  * @version $Id: AllProfilesMojo.java 690512 2008-08-30 14:20:30Z vsiveton $
 52  
  * @since 2.1
 53  
  * @goal all-profiles
 54  
  * @requiresProject false
 55  
  */
 56  0
 public class AllProfilesMojo
 57  
     extends AbstractHelpMojo
 58  
 {
 59  
     // ----------------------------------------------------------------------
 60  
     // Mojo parameters
 61  
     // ----------------------------------------------------------------------
 62  
 
 63  
     /**
 64  
      * This is the list of projects currently slated to be built by Maven.
 65  
      *
 66  
      * @parameter expression="${reactorProjects}"
 67  
      * @required
 68  
      * @readonly
 69  
      */
 70  
     private List projects;
 71  
 
 72  
     /**
 73  
      * The current build session instance. This is used for plugin manager API calls.
 74  
      *
 75  
      * @parameter expression="${session}"
 76  
      * @required
 77  
      * @readonly
 78  
      */
 79  
     private MavenSession session;
 80  
 
 81  
     // ----------------------------------------------------------------------
 82  
     // Public methods
 83  
     // ----------------------------------------------------------------------
 84  
 
 85  
     /** {@inheritDoc} */
 86  
     public void execute()
 87  
         throws MojoExecutionException, MojoFailureException
 88  
     {
 89  0
         StringBuffer descriptionBuffer = new StringBuffer();
 90  
 
 91  0
         for ( Iterator iter = projects.iterator(); iter.hasNext(); )
 92  
         {
 93  0
             MavenProject project = (MavenProject) iter.next();
 94  
 
 95  0
             descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( "\n" );
 96  
 
 97  0
             DefaultProfileManager pm =
 98  
                 new DefaultProfileManager( session.getContainer(), session.getExecutionProperties() );
 99  
 
 100  
             // Obtain Profiles from external profiles.xml
 101  
             try
 102  
             {
 103  0
                 loadProjectExternalProfiles( pm, project.getBasedir() );
 104  
             }
 105  0
             catch ( ProfileActivationException e )
 106  
             {
 107  0
                 throw new MojoExecutionException( "Error obtaining external Profiles:" + e.getMessage(), e );
 108  0
             }
 109  
 
 110  
             // Attempt to obtain settings profiles
 111  0
             loadSettingsProfiles( pm, session.getSettings() );
 112  
 
 113  
             // Attempt to obtain profiles from pom.xml
 114  0
             loadProjectPomProfiles( pm, project );
 115  
 
 116  
             // now display
 117  0
             if ( null == pm.getExplicitlyActivatedIds() || pm.getExplicitlyActivatedIds().size() == 0 )
 118  
             {
 119  0
                 if ( getLog().isWarnEnabled() )
 120  
                 {
 121  0
                     getLog().warn( "No profiles detected!" );
 122  
                 }
 123  
             }
 124  
             else
 125  
             {
 126  
                 // This feels more like a hack to filter out inactive profiles, there is no 'direct'
 127  
                 // way to query activation status on a Profile instance.
 128  0
                 Map allProfilesByIds = pm.getProfilesById();
 129  
 
 130  
                 // active Profiles will be a subset of *all* profiles
 131  0
                 List activeProfiles = project.getActiveProfiles();
 132  0
                 for ( Iterator itr = activeProfiles.iterator(); itr.hasNext(); )
 133  
                 {
 134  0
                     Profile activeProfile = (Profile) itr.next();
 135  
 
 136  
                     // we already have the active profiles for the project, so remove them from the list of all
 137  
                     // profiles.
 138  0
                     allProfilesByIds.remove( activeProfile.getId() );
 139  0
                 }
 140  
 
 141  
                 // display active profiles
 142  0
                 for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
 143  
                 {
 144  0
                     Profile p = (Profile) it.next();
 145  
 
 146  0
                     descriptionBuffer.append( "  Profile Id: " ).append( p.getId() );
 147  0
                     descriptionBuffer.append( " (Active: true , Source: " ).append( p.getSource() ).append( ")\n" );
 148  0
                 }
 149  
 
 150  
                 // display inactive profiles
 151  0
                 Iterator it = allProfilesByIds.keySet().iterator();
 152  0
                 while ( it.hasNext() )
 153  
                 {
 154  0
                     Profile p = (Profile) allProfilesByIds.get( (String) it.next() );
 155  
 
 156  0
                     descriptionBuffer.append( "  Profile Id: " ).append( p.getId() );
 157  0
                     descriptionBuffer.append( " (Active: false , Source: " ).append( p.getSource() ).append( ")\n" );
 158  0
                 }
 159  
             }
 160  0
         }
 161  
 
 162  0
         if ( output != null )
 163  
         {
 164  
             try
 165  
             {
 166  0
                 writeFile( output, descriptionBuffer );
 167  
             }
 168  0
             catch ( IOException e )
 169  
             {
 170  0
                 throw new MojoExecutionException( "Cannot write profiles description to output: " + output, e );
 171  0
             }
 172  
 
 173  0
             if ( getLog().isInfoEnabled() )
 174  
             {
 175  0
                 getLog().info( "Wrote descriptions to: " + output );
 176  
             }
 177  
         }
 178  
         else
 179  
         {
 180  0
             if ( getLog().isInfoEnabled() )
 181  
             {
 182  0
                 getLog().info( descriptionBuffer.toString() );
 183  
             }
 184  
         }
 185  0
     }
 186  
 
 187  
     // ----------------------------------------------------------------------
 188  
     // Private methods
 189  
     // ----------------------------------------------------------------------
 190  
 
 191  
     /**
 192  
      * Loads up external Profiles using <code>profiles.xml</code> (if any) located in the current
 193  
      * project's <code>${basedir}</code>.
 194  
      *
 195  
      * @param profileManager ProfileManager instance to use to load profiles from external Profiles.
 196  
      * @param projectDir location of the current project, could be null.
 197  
      * @throws ProfileActivationException, if there was an error loading profiles.
 198  
      */
 199  
     private void loadProjectExternalProfiles( ProfileManager profileManager, File projectDir )
 200  
         throws ProfileActivationException
 201  
     {
 202  0
         if ( projectDir == null )
 203  
         {
 204  0
             return;
 205  
         }
 206  
 
 207  0
         if ( getLog().isDebugEnabled() )
 208  
         {
 209  0
             getLog().debug( "Attempting to read profiles from external profiles.xml..." );
 210  
         }
 211  
 
 212  
         try
 213  
         {
 214  0
             DefaultMavenProfilesBuilder profilesBuilder = new DefaultMavenProfilesBuilder();
 215  0
             ProfilesRoot root = profilesBuilder.buildProfiles( projectDir );
 216  0
             if ( root != null )
 217  
             {
 218  0
                 for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
 219  
                 {
 220  0
                     org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
 221  
 
 222  0
                     Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile );
 223  0
                     profileManager.addProfile( converted );
 224  0
                     profileManager.explicitlyActivate( converted.getId() );
 225  0
                 }
 226  
             }
 227  0
             else if ( getLog().isDebugEnabled() )
 228  
             {
 229  0
                 getLog().debug( "ProfilesRoot was found to be NULL" );
 230  
             }
 231  
         }
 232  0
         catch ( IOException e )
 233  
         {
 234  0
             throw new ProfileActivationException( "Cannot read profiles.xml resource from directory: "
 235  
                 + projectDir, e );
 236  
         }
 237  0
         catch ( XmlPullParserException e )
 238  
         {
 239  0
             throw new ProfileActivationException( "Cannot parse profiles.xml resource from directory: "
 240  
                 + projectDir, e );
 241  0
         }
 242  0
     }
 243  
 
 244  
     /**
 245  
      * Load profiles from <code>pom.xml</code>.
 246  
      *
 247  
      * @param profilesManager not null
 248  
      * @param project could be null
 249  
      */
 250  
     private void loadProjectPomProfiles( ProfileManager profilesManager, MavenProject project )
 251  
     {
 252  0
         if ( project == null )
 253  
         {
 254  
             // shouldn't happen as this mojo requires a project
 255  0
             if ( getLog().isDebugEnabled() )
 256  
             {
 257  0
                 getLog().debug( "No pom.xml found to read Profiles from." );
 258  
             }
 259  
 
 260  0
             return;
 261  
         }
 262  
 
 263  0
         if ( getLog().isDebugEnabled() )
 264  
         {
 265  0
             getLog().debug( "Attempting to read profiles from pom.xml..." );
 266  
         }
 267  
 
 268  
         // Attempt to obtain the list of profiles from pom.xml
 269  0
         Iterator it = project.getModel().getProfiles().iterator();
 270  0
         while ( it.hasNext() )
 271  
         {
 272  0
             Profile profile = (Profile) it.next();
 273  
 
 274  0
             profilesManager.addProfile( profile );
 275  0
             profilesManager.explicitlyActivate( profile.getId() );
 276  0
         }
 277  
 
 278  0
         MavenProject parent = project.getParent();
 279  0
         while( parent != null )
 280  
         {
 281  0
             Iterator it2 = parent.getModel().getProfiles().iterator();
 282  0
             while ( it2.hasNext() )
 283  
             {
 284  0
                 Profile profile = (Profile) it2.next();
 285  
 
 286  0
                 profilesManager.addProfile( profile );
 287  0
                 profilesManager.explicitlyActivate( profile.getId() );
 288  0
             }
 289  
 
 290  0
             parent = parent.getParent();
 291  0
         }
 292  0
     }
 293  
 
 294  
     /**
 295  
      * Load profiles from <code>settings.xml</code>.
 296  
      *
 297  
      * @param profileManager not null
 298  
      * @param settings could be null
 299  
      */
 300  
     private void loadSettingsProfiles( ProfileManager profileManager, Settings settings )
 301  
     {
 302  0
         if ( settings == null )
 303  
         {
 304  0
             if ( getLog().isDebugEnabled() )
 305  
             {
 306  0
                 getLog().debug( "No settings.xml detected." );
 307  
             }
 308  
 
 309  0
             return;
 310  
         }
 311  
 
 312  0
         if ( getLog().isDebugEnabled() )
 313  
         {
 314  0
             getLog().debug( "Attempting to read profiles from settings.xml..." );
 315  
         }
 316  
 
 317  0
         Iterator it = settings.getProfiles().iterator();
 318  0
         while ( it.hasNext() )
 319  
         {
 320  0
             org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next();
 321  
 
 322  0
             Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
 323  0
             profileManager.addProfile( profile );
 324  0
             profileManager.explicitlyActivate( profile.getId() );
 325  0
         }
 326  0
     }
 327  
 }