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.ArrayList;
24  import java.util.Date;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.maven.model.Profile;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Displays a list of the profiles which are currently active for this build.
34   *
35   * @version $Id: ActiveProfilesMojo.java 683226 2008-08-06 11:17:38Z vsiveton $
36   * @since 2.0
37   * @goal active-profiles
38   * @aggregator
39   */
40  public class ActiveProfilesMojo
41      extends AbstractHelpMojo
42  {
43      // ----------------------------------------------------------------------
44      // Mojo parameters
45      // ----------------------------------------------------------------------
46  
47      /**
48       * This is the list of projects currently slated to be built by Maven.
49       *
50       * @parameter expression="${reactorProjects}"
51       * @required
52       * @readonly
53       */
54      private List projects;
55  
56      // ----------------------------------------------------------------------
57      // Public methods
58      // ----------------------------------------------------------------------
59  
60      /** {@inheritDoc} */
61      public void execute()
62          throws MojoExecutionException
63      {
64          StringBuffer message = new StringBuffer();
65  
66          for ( Iterator it = projects.iterator(); it.hasNext(); )
67          {
68              MavenProject project = (MavenProject) it.next();
69  
70              getActiveProfileStatement( project, message );
71  
72              message.append( "\n\n" );
73          }
74  
75          if ( output != null )
76          {
77              StringBuffer sb = new StringBuffer();
78              sb.append( "Created by: " + getClass().getName() ).append( "\n" );
79              sb.append( "Created on: " + new Date() ).append( "\n" ).append( "\n" );
80              sb.append( message.toString() );
81  
82              try
83              {
84                  writeFile( output, sb );
85              }
86              catch ( IOException e )
87              {
88                  throw new MojoExecutionException( "Cannot write active profiles to output: " + output, e );
89              }
90  
91              if ( getLog().isInfoEnabled() )
92              {
93                  getLog().info( "Active profile report written to: " + output );
94              }
95          }
96          else
97          {
98              if ( getLog().isInfoEnabled() )
99              {
100                 getLog().info( message );
101             }
102         }
103     }
104 
105     // ----------------------------------------------------------------------
106     // Private methods
107     // ----------------------------------------------------------------------
108 
109     /**
110      * Method to get the active profiles for the project
111      *
112      * @param project   the current project
113      * @param message   the object where the information will be appended to
114      */
115     private void getActiveProfileStatement( MavenProject project, StringBuffer message )
116     {
117         // Get active profiles into our own list,
118         // since we'll be modifying it, further below
119         List profiles = new ArrayList( project.getActiveProfiles() );
120 
121         message.append( "\n" );
122 
123         message.append( "Active Profiles for Project \'" + project.getId() + "\': \n\n" );
124 
125         if ( profiles == null || profiles.isEmpty() )
126         {
127             message.append( "There are no active profiles." );
128         }
129         else
130         {
131             message.append( "The following profiles are active:\n" );
132 
133             for ( Iterator it = profiles.iterator(); it.hasNext(); )
134             {
135                 Profile profile = (Profile) it.next();
136 
137                 message.append( "\n - " ).append( profile.getId() );
138                 message.append( " (source: " ).append( profile.getSource() ).append( ")" );
139             }
140 
141         }
142 
143         message.append( "\n" );
144     }
145 }