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.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.LinkedHashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.maven.model.Profile;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.project.MavenProject;
36  
37  /**
38   * Displays a list of the profiles which are currently active for this build.
39   *
40   * @version $Id: ActiveProfilesMojo.java 1446806 2013-02-15 23:07:28Z rfscholte $
41   * @since 2.0
42   */
43  @Mojo( name = "active-profiles", aggregator = true )
44  public class ActiveProfilesMojo
45      extends AbstractHelpMojo
46  {
47      // ----------------------------------------------------------------------
48      // Mojo parameters
49      // ----------------------------------------------------------------------
50  
51      /**
52       * This is the list of projects currently slated to be built by Maven.
53       */
54      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
55      private List<MavenProject> projects;
56  
57      // ----------------------------------------------------------------------
58      // Public methods
59      // ----------------------------------------------------------------------
60  
61      /** {@inheritDoc} */
62      public void execute()
63          throws MojoExecutionException
64      {
65          StringBuilder message = new StringBuilder();
66  
67          for ( MavenProject project : projects )
68          {
69              getActiveProfileStatement( project, message );
70  
71              message.append( "\n\n" );
72          }
73  
74          if ( output != null )
75          {
76              StringBuilder sb = new StringBuilder();
77              sb.append( "Created by: " + getClass().getName() ).append( "\n" );
78              sb.append( "Created on: " + new Date() ).append( "\n" ).append( "\n" );
79              sb.append( message.toString() );
80  
81              try
82              {
83                  writeFile( output, sb );
84              }
85              catch ( IOException e )
86              {
87                  throw new MojoExecutionException( "Cannot write active profiles to output: " + output, e );
88              }
89  
90              if ( getLog().isInfoEnabled() )
91              {
92                  getLog().info( "Active profile report written to: " + output );
93              }
94          }
95          else
96          {
97              if ( getLog().isInfoEnabled() )
98              {
99                  getLog().info( message );
100             }
101         }
102     }
103 
104     // ----------------------------------------------------------------------
105     // Private methods
106     // ----------------------------------------------------------------------
107 
108     /**
109      * Method to get the active profiles for the project
110      *
111      * @param project   the current project
112      * @param message   the object where the information will be appended to
113      */
114     private void getActiveProfileStatement( MavenProject project, StringBuilder message )
115     {
116         Map<String, List<String>> activeProfileIds = new LinkedHashMap<String, List<String>>();
117         try 
118         {
119             activeProfileIds.putAll( getInjectedProfileIds( project ) );
120         }
121         catch ( UnsupportedOperationException uoe )
122         {
123             // Fall back to M2 approach
124             @SuppressWarnings( "unchecked" )
125             List<Profile> profiles = new ArrayList<Profile>( project.getActiveProfiles() );
126             
127             for ( Profile profile : profiles )
128             {
129                 List<String> profileIds = activeProfileIds.get( profile.getSource() );
130                 if ( profileIds == null )
131                 {
132                     profileIds = new ArrayList<String>();
133                     activeProfileIds.put( profile.getSource(), profileIds );
134                 }
135                 profileIds.add( profile.getId() );
136             }
137         }
138         
139 
140         message.append( "\n" );
141 
142         message.append( "Active Profiles for Project \'" + project.getId() + "\': \n\n" );
143 
144         if ( activeProfileIds.isEmpty() )
145         {
146             message.append( "There are no active profiles." );
147         }
148         else
149         {
150             message.append( "The following profiles are active:\n" );
151 
152             for ( Map.Entry<String, List<String>> entry : activeProfileIds.entrySet() )
153             {
154                 for ( String profileId : entry.getValue() )
155                 {
156                     message.append( "\n - " ).append( profileId );
157                     message.append( " (source: " ).append( entry.getKey() ).append( ")" );
158                 }
159             }
160         }
161 
162         message.append( "\n" );
163     }
164 
165     @SuppressWarnings( "unchecked" )
166     private Map<String, List<String>> getInjectedProfileIds( MavenProject project ) throws UnsupportedOperationException
167     {
168         try
169         {
170             // This method was introduced with M3
171             Method getInjectedProfileIdsMethod = MavenProject.class.getMethod( "getInjectedProfileIds" );
172             return (Map<String, List<String>>) getInjectedProfileIdsMethod.invoke( project );
173         }
174         catch ( SecurityException e )
175         {
176             throw new UnsupportedOperationException( e.getMessage(), e );
177         }
178         catch ( NoSuchMethodException e )
179         {
180             throw new UnsupportedOperationException( e.getMessage(), e );
181         }
182         catch ( IllegalArgumentException e )
183         {
184             throw new UnsupportedOperationException( e.getMessage(), e );
185         }
186         catch ( IllegalAccessException e )
187         {
188             throw new UnsupportedOperationException( e.getMessage(), e );
189         }
190         catch ( InvocationTargetException e )
191         {
192             throw new UnsupportedOperationException( e.getMessage(), e );
193         }
194     }
195 }