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.List;
24  import java.util.Map;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * Displays a list of the profiles which are currently active for this build.
33   *
34   * @since 2.0
35   */
36  @Mojo( name = "active-profiles", aggregator = true )
37  public class ActiveProfilesMojo
38      extends AbstractHelpMojo
39  {
40      // ----------------------------------------------------------------------
41      // Mojo parameters
42      // ----------------------------------------------------------------------
43  
44      /**
45       * This is the list of projects currently slated to be built by Maven.
46       */
47      @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
48      private List<MavenProject> projects;
49  
50      // ----------------------------------------------------------------------
51      // Public methods
52      // ----------------------------------------------------------------------
53  
54      /** {@inheritDoc} */
55      public void execute()
56          throws MojoExecutionException
57      {
58          StringBuilder message = new StringBuilder();
59  
60          for ( MavenProject project : projects )
61          {
62              getActiveProfileStatement( project, message );
63  
64              message.append( LS ).append( LS );
65          }
66  
67          if ( output != null )
68          {
69              StringBuilder sb = new StringBuilder();
70              sb.append( "Generated by Maven Help Plugin" ).append( LS );
71              sb.append( "See: https://maven.apache.org/plugins/maven-help-plugin/" ).append( LS ).append( LS );
72              sb.append( message.toString() );
73  
74              try
75              {
76                  writeFile( output, sb );
77              }
78              catch ( IOException e )
79              {
80                  throw new MojoExecutionException( "Cannot write active profiles to output: " + output, e );
81              }
82  
83              getLog().info( "Active profile report written to: " + output );
84          }
85          else
86          {
87              getLog().info( message );
88          }
89      }
90  
91      // ----------------------------------------------------------------------
92      // Private methods
93      // ----------------------------------------------------------------------
94  
95      /**
96       * Method to get the active profiles for the project
97       *
98       * @param project   the current project
99       * @param message   the object where the information will be appended to
100      */
101     private void getActiveProfileStatement( MavenProject project, StringBuilder message )
102     {
103         Map<String, List<String>> activeProfileIds = project.getInjectedProfileIds();
104 
105         message.append( LS );
106         message.append( "Active Profiles for Project '" ).append( project.getId() ).append( "':" );
107         message.append( LS ).append( LS );
108 
109         if ( activeProfileIds.isEmpty() )
110         {
111             message.append( "There are no active profiles." );
112         }
113         else
114         {
115             message.append( "The following profiles are active:" ).append( LS );
116 
117             for ( Map.Entry<String, List<String>> entry : activeProfileIds.entrySet() )
118             {
119                 for ( String profileId : entry.getValue() )
120                 {
121                     message.append( LS ).append( " - " ).append( profileId );
122                     message.append( " (source: " ).append( entry.getKey() ).append( ")" );
123                 }
124             }
125         }
126 
127         message.append( LS );
128     }
129 
130 }