View Javadoc
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 org.apache.maven.model.Profile;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.settings.SettingsUtils;
29  
30  import java.io.IOException;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * Displays a list of available profiles under the current project.
37   * <br>
38   * <b>Note</b>: it will list <b>all</b> profiles for a project. If a
39   * profile comes up with a status <b>inactive</b> then there might be a need to
40   * set profile activation switches/property.
41   *
42   * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
43   * @version $Id$
44   * @since 2.1
45   */
46  @Mojo( name = "all-profiles", requiresProject = false )
47  public class AllProfilesMojo
48      extends AbstractHelpMojo
49  {
50      // ----------------------------------------------------------------------
51      // Mojo parameters
52      // ----------------------------------------------------------------------
53  
54      /**
55       * This is the list of projects currently slated to be built by Maven.
56       */
57      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
58      private List<MavenProject> projects;
59  
60      /**
61       * The list of profiles defined in the current Maven settings.
62       */
63      @Parameter( defaultValue = "${settings.profiles}", readonly = true, required = true )
64      private List<org.apache.maven.settings.Profile> settingsProfiles;
65  
66      // ----------------------------------------------------------------------
67      // Public methods
68      // ----------------------------------------------------------------------
69  
70      /** {@inheritDoc} */
71      public void execute()
72          throws MojoExecutionException, MojoFailureException
73      {
74          StringBuilder descriptionBuffer = new StringBuilder();
75  
76          for ( MavenProject project : projects )
77          {
78              descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( LS );
79              
80              Map<String, Profile> allProfilesByIds = new HashMap<String, Profile>();
81              Map<String, Profile> activeProfilesByIds = new HashMap<String, Profile>();
82              addSettingsProfiles( allProfilesByIds );
83              addProjectPomProfiles( project, allProfilesByIds, activeProfilesByIds );
84  
85              // now display
86              if ( allProfilesByIds.isEmpty() )
87              {
88                  getLog().warn( "No profiles detected!" );
89              }
90              else
91              {
92                  // active Profiles will be a subset of *all* profiles
93                  allProfilesByIds.keySet().removeAll( activeProfilesByIds.keySet() );
94  
95                  writeProfilesDescription( descriptionBuffer, activeProfilesByIds, true );
96                  writeProfilesDescription( descriptionBuffer, allProfilesByIds, false );
97              }
98          }
99  
100         if ( output != null )
101         {
102             try
103             {
104                 writeFile( output, descriptionBuffer );
105             }
106             catch ( IOException e )
107             {
108                 throw new MojoExecutionException( "Cannot write profiles description to output: " + output, e );
109             }
110 
111             getLog().info( "Wrote descriptions to: " + output );
112         }
113         else
114         {
115             getLog().info( descriptionBuffer.toString() );
116         }
117     }
118 
119     // ----------------------------------------------------------------------
120     // Private methods
121     // ----------------------------------------------------------------------
122 
123     private void writeProfilesDescription( StringBuilder sb, Map<String, Profile> profilesByIds, boolean active )
124     {
125         for ( Profile p : profilesByIds.values() )
126         {
127             sb.append( "  Profile Id: " ).append( p.getId() );
128             sb.append( " (Active: " + active + " , Source: " ).append( p.getSource() ).append( ")" );
129             sb.append( LS );
130         }
131     }
132 
133     /**
134      * Adds the profiles from <code>pom.xml</code> and all of its parents.
135      *
136      * @param project could be null
137      * @param allProfiles Map to add the profiles to.
138      * @param activeProfiles Map to add the active profiles to.
139      */
140     private void addProjectPomProfiles( MavenProject project, Map<String, Profile> allProfiles,
141                                         Map<String, Profile> activeProfiles )
142     {
143         if ( project == null )
144         {
145             // shouldn't happen as this mojo requires a project
146             getLog().debug( "No pom.xml found to read Profiles from." );
147             return;
148         }
149 
150         getLog().debug( "Attempting to read profiles from pom.xml..." );
151 
152         while ( project != null )
153         {
154             for ( Profile profile : project.getModel().getProfiles() )
155             {
156                 allProfiles.put( profile.getId(), profile );
157             }
158             if ( project.getActiveProfiles() != null )
159             {
160                 for ( Profile profile : project.getActiveProfiles() )
161                 {
162                     activeProfiles.put( profile.getId(), profile );
163                 }
164             }
165             project = project.getParent();
166         }
167     }
168 
169     /**
170      * Adds the profiles from <code>settings.xml</code>.
171      *
172      * @param allProfiles Map to add the profiles to.
173      */
174     private void addSettingsProfiles( Map<String, Profile> allProfiles )
175     {
176         getLog().debug( "Attempting to read profiles from settings.xml..." );
177         for ( org.apache.maven.settings.Profile settingsProfile : settingsProfiles )
178         {
179             Profile profile = SettingsUtils.convertFromSettingsProfile( settingsProfile );
180             allProfiles.put( profile.getId(), profile );
181         }
182     }
183 }