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.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  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          StringBuffer descriptionBuffer = new StringBuffer();
90  
91          for ( Iterator iter = projects.iterator(); iter.hasNext(); )
92          {
93              MavenProject project = (MavenProject) iter.next();
94  
95              descriptionBuffer.append( "Listing Profiles for Project: " ).append( project.getId() ).append( "\n" );
96  
97              DefaultProfileManager pm =
98                  new DefaultProfileManager( session.getContainer(), session.getExecutionProperties() );
99  
100             // Obtain Profiles from external profiles.xml
101             try
102             {
103                 loadProjectExternalProfiles( pm, project.getBasedir() );
104             }
105             catch ( ProfileActivationException e )
106             {
107                 throw new MojoExecutionException( "Error obtaining external Profiles:" + e.getMessage(), e );
108             }
109 
110             // Attempt to obtain settings profiles
111             loadSettingsProfiles( pm, session.getSettings() );
112 
113             // Attempt to obtain profiles from pom.xml
114             loadProjectPomProfiles( pm, project );
115 
116             // now display
117             if ( null == pm.getExplicitlyActivatedIds() || pm.getExplicitlyActivatedIds().size() == 0 )
118             {
119                 if ( getLog().isWarnEnabled() )
120                 {
121                     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                 Map allProfilesByIds = pm.getProfilesById();
129 
130                 // active Profiles will be a subset of *all* profiles
131                 List activeProfiles = project.getActiveProfiles();
132                 for ( Iterator itr = activeProfiles.iterator(); itr.hasNext(); )
133                 {
134                     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                     allProfilesByIds.remove( activeProfile.getId() );
139                 }
140 
141                 // display active profiles
142                 for ( Iterator it = activeProfiles.iterator(); it.hasNext(); )
143                 {
144                     Profile p = (Profile) it.next();
145 
146                     descriptionBuffer.append( "  Profile Id: " ).append( p.getId() );
147                     descriptionBuffer.append( " (Active: true , Source: " ).append( p.getSource() ).append( ")\n" );
148                 }
149 
150                 // display inactive profiles
151                 Iterator it = allProfilesByIds.keySet().iterator();
152                 while ( it.hasNext() )
153                 {
154                     Profile p = (Profile) allProfilesByIds.get( (String) it.next() );
155 
156                     descriptionBuffer.append( "  Profile Id: " ).append( p.getId() );
157                     descriptionBuffer.append( " (Active: false , Source: " ).append( p.getSource() ).append( ")\n" );
158                 }
159             }
160         }
161 
162         if ( output != null )
163         {
164             try
165             {
166                 writeFile( output, descriptionBuffer );
167             }
168             catch ( IOException e )
169             {
170                 throw new MojoExecutionException( "Cannot write profiles description to output: " + output, e );
171             }
172 
173             if ( getLog().isInfoEnabled() )
174             {
175                 getLog().info( "Wrote descriptions to: " + output );
176             }
177         }
178         else
179         {
180             if ( getLog().isInfoEnabled() )
181             {
182                 getLog().info( descriptionBuffer.toString() );
183             }
184         }
185     }
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         if ( projectDir == null )
203         {
204             return;
205         }
206 
207         if ( getLog().isDebugEnabled() )
208         {
209             getLog().debug( "Attempting to read profiles from external profiles.xml..." );
210         }
211 
212         try
213         {
214             DefaultMavenProfilesBuilder profilesBuilder = new DefaultMavenProfilesBuilder();
215             ProfilesRoot root = profilesBuilder.buildProfiles( projectDir );
216             if ( root != null )
217             {
218                 for ( Iterator it = root.getProfiles().iterator(); it.hasNext(); )
219                 {
220                     org.apache.maven.profiles.Profile rawProfile = (org.apache.maven.profiles.Profile) it.next();
221 
222                     Profile converted = ProfilesConversionUtils.convertFromProfileXmlProfile( rawProfile );
223                     profileManager.addProfile( converted );
224                     profileManager.explicitlyActivate( converted.getId() );
225                 }
226             }
227             else if ( getLog().isDebugEnabled() )
228             {
229                 getLog().debug( "ProfilesRoot was found to be NULL" );
230             }
231         }
232         catch ( IOException e )
233         {
234             throw new ProfileActivationException( "Cannot read profiles.xml resource from directory: "
235                 + projectDir, e );
236         }
237         catch ( XmlPullParserException e )
238         {
239             throw new ProfileActivationException( "Cannot parse profiles.xml resource from directory: "
240                 + projectDir, e );
241         }
242     }
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         if ( project == null )
253         {
254             // shouldn't happen as this mojo requires a project
255             if ( getLog().isDebugEnabled() )
256             {
257                 getLog().debug( "No pom.xml found to read Profiles from." );
258             }
259 
260             return;
261         }
262 
263         if ( getLog().isDebugEnabled() )
264         {
265             getLog().debug( "Attempting to read profiles from pom.xml..." );
266         }
267 
268         // Attempt to obtain the list of profiles from pom.xml
269         Iterator it = project.getModel().getProfiles().iterator();
270         while ( it.hasNext() )
271         {
272             Profile profile = (Profile) it.next();
273 
274             profilesManager.addProfile( profile );
275             profilesManager.explicitlyActivate( profile.getId() );
276         }
277 
278         MavenProject parent = project.getParent();
279         while( parent != null )
280         {
281             Iterator it2 = parent.getModel().getProfiles().iterator();
282             while ( it2.hasNext() )
283             {
284                 Profile profile = (Profile) it2.next();
285 
286                 profilesManager.addProfile( profile );
287                 profilesManager.explicitlyActivate( profile.getId() );
288             }
289 
290             parent = parent.getParent();
291         }
292     }
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         if ( settings == null )
303         {
304             if ( getLog().isDebugEnabled() )
305             {
306                 getLog().debug( "No settings.xml detected." );
307             }
308 
309             return;
310         }
311 
312         if ( getLog().isDebugEnabled() )
313         {
314             getLog().debug( "Attempting to read profiles from settings.xml..." );
315         }
316 
317         Iterator it = settings.getProfiles().iterator();
318         while ( it.hasNext() )
319         {
320             org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next();
321 
322             Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
323             profileManager.addProfile( profile );
324             profileManager.explicitlyActivate( profile.getId() );
325         }
326     }
327 }