Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.maven.plugins.help;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;

Displays a list of available profiles under the current project.
Note: it will list all profiles for a project. If a profile comes up with a status inactive then there might be a need to set profile activation switches/property.

Author(s):
Rahul Thakur
Version:
$Id: AllProfilesMojo.java 1446806 2013-02-15 23:07:28Z rfscholte $
Since:
2.1
 
 @Mojo( name = "all-profiles", requiresProject = false )
 public class AllProfilesMojo
     extends AbstractHelpMojo
 {
     // ----------------------------------------------------------------------
     // Mojo parameters
     // ----------------------------------------------------------------------
 
    
This is the list of projects currently slated to be built by Maven.
 
     @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
     private List<MavenProjectprojects;

    
The current build session instance. This is used for plugin manager API calls.
 
     @Component
     private MavenSession session;
 
     // ----------------------------------------------------------------------
     // Public methods
     // ----------------------------------------------------------------------
 
    
 
     public void execute()
     {
         StringBuilder descriptionBuffer = new StringBuilder();
 
         for ( MavenProject project :  )
         {
             descriptionBuffer.append"Listing Profiles for Project: " ).appendproject.getId() ).append"\n" );
 
             DefaultProfileManager pm =
                 new DefaultProfileManager.getContainer(), .getExecutionProperties() );
 
             // Obtain Profiles from external profiles.xml
             try
             {
                 loadProjectExternalProfilespmproject.getBasedir() );
             }
             catch ( ProfileActivationException e )
             {
                throw new MojoExecutionException"Error obtaining external Profiles:" + e.getMessage(), e );
            }
            // Attempt to obtain settings profiles
            loadSettingsProfilespm.getSettings() );
            // Attempt to obtain profiles from pom.xml
            loadProjectPomProfilespmproject );
            // now display
            if ( null == pm.getExplicitlyActivatedIds() || pm.getExplicitlyActivatedIds().size() == 0 )
            {
                if ( getLog().isWarnEnabled() )
                {
                    getLog().warn"No profiles detected!" );
                }
            }
            else
            {
                // This feels more like a hack to filter out inactive profiles, there is no 'direct'
                // way to query activation status on a Profile instance.
                @SuppressWarnings"unchecked" )
                Map<StringProfileallProfilesByIds = pm.getProfilesById();
                // active Profiles will be a subset of *all* profiles
                @SuppressWarnings"unchecked" )
                List<ProfileactiveProfiles = project.getActiveProfiles();
                for ( Profile activeProfile : activeProfiles )
                {
                    // we already have the active profiles for the project, so remove them from the list of all
                    // profiles.
                    allProfilesByIds.removeactiveProfile.getId() );
                }
                // display active profiles
                for ( Profile p : activeProfiles )
                {
                    descriptionBuffer.append"  Profile Id: " ).appendp.getId() );
                    descriptionBuffer.append" (Active: true , Source: " ).appendp.getSource() ).append")\n" );
                }
                // display inactive profiles
                for ( Profile p : allProfilesByIds.values() )
                {
                    descriptionBuffer.append"  Profile Id: " ).appendp.getId() );
                    descriptionBuffer.append" (Active: false , Source: " ).appendp.getSource() ).append")\n" );
                }
            }
        }
        if (  != null )
        {
            try
            {
                writeFiledescriptionBuffer );
            }
            catch ( IOException e )
            {
                throw new MojoExecutionException"Cannot write profiles description to output: " + e );
            }
            if ( getLog().isInfoEnabled() )
            {
                getLog().info"Wrote descriptions to: " +  );
            }
        }
        else
        {
            if ( getLog().isInfoEnabled() )
            {
                getLog().infodescriptionBuffer.toString() );
            }
        }
    }
    // ----------------------------------------------------------------------
    // Private methods
    // ----------------------------------------------------------------------

    
Loads up external Profiles using profiles.xml (if any) located in the current project's ${basedir}.

Parameters:
profileManager ProfileManager instance to use to load profiles from external Profiles.
projectDir location of the current project, could be null.
Throws:
org.apache.maven.profiles.activation.ProfileActivationException, if there was an error loading profiles.
    private void loadProjectExternalProfilesProfileManager profileManagerFile projectDir )
        throws ProfileActivationException
    {
        if ( projectDir == null )
        {
            return;
        }
        if ( getLog().isDebugEnabled() )
        {
            getLog().debug"Attempting to read profiles from external profiles.xml..." );
        }
        try
        {
            DefaultMavenProfilesBuilder profilesBuilder = new DefaultMavenProfilesBuilder();
            ProfilesRoot root = profilesBuilder.buildProfilesprojectDir );
            if ( root != null )
            {
                List<org.apache.maven.profiles.Profileprofiles = root.getProfiles(); 
                for ( org.apache.maven.profiles.Profile rawProfile : profiles )
                {
                    Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfilerawProfile );
                    profileManager.addProfileconverted );
                    profileManager.explicitlyActivateconverted.getId() );
                }
            }
            else if ( getLog().isDebugEnabled() )
            {
                getLog().debug"ProfilesRoot was found to be NULL" );
            }
        }
        catch ( IOException e )
        {
            throw new ProfileActivationException"Cannot read profiles.xml resource from directory: "
                + projectDire );
        }
        catch ( XmlPullParserException e )
        {
            throw new ProfileActivationException"Cannot parse profiles.xml resource from directory: "
                + projectDire );
        }
    }

    
Load profiles from pom.xml.

Parameters:
profilesManager not null
project could be null
    private void loadProjectPomProfilesProfileManager profilesManagerMavenProject project )
    {
        if ( project == null )
        {
            // shouldn't happen as this mojo requires a project
            if ( getLog().isDebugEnabled() )
            {
                getLog().debug"No pom.xml found to read Profiles from." );
            }
            return;
        }
        if ( getLog().isDebugEnabled() )
        {
            getLog().debug"Attempting to read profiles from pom.xml..." );
        }
        // Attempt to obtain the list of profiles from pom.xml
        List<Profileprofiles = project.getModel().getProfiles();
        for ( Profile profile : profiles )
        {
            profilesManager.addProfileprofile );
            profilesManager.explicitlyActivateprofile.getId() );
        }
        MavenProject parent = project.getParent();
        while ( parent != null )
        {
            List<Profileprofiles2 = parent.getModel().getProfiles();
            for ( Profile profile : profiles2 )
            {
                profilesManager.addProfileprofile );
                profilesManager.explicitlyActivateprofile.getId() );
            }
            parent = parent.getParent();
        }
    }

    
Load profiles from settings.xml.

Parameters:
profileManager not null
settings could be null
    private void loadSettingsProfilesProfileManager profileManagerSettings settings )
    {
        if ( settings == null )
        {
            if ( getLog().isDebugEnabled() )
            {
                getLog().debug"No settings.xml detected." );
            }
            return;
        }
        if ( getLog().isDebugEnabled() )
        {
            getLog().debug"Attempting to read profiles from settings.xml..." );
        }
        List<org.apache.maven.settings.Profileprofiles = settings.getProfiles();
        for ( org.apache.maven.settings.Profile rawProfile : profiles )
        {
            Profile profile = SettingsUtils.convertFromSettingsProfilerawProfile );
            profileManager.addProfileprofile );
            profileManager.explicitlyActivateprofile.getId() );
        }
    }
New to GrepCode? Check out our FAQ X