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