View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.help;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * Displays a list of the profiles which are currently active for this build.
32   *
33   * @since 2.0
34   */
35  @Mojo(name = "active-profiles", aggregator = true)
36  public class ActiveProfilesMojo extends AbstractHelpMojo {
37      // ----------------------------------------------------------------------
38      // Mojo parameters
39      // ----------------------------------------------------------------------
40  
41      /**
42       * This is the list of projects currently slated to be built by Maven.
43       */
44      @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
45      private List<MavenProject> projects;
46  
47      // ----------------------------------------------------------------------
48      // Public methods
49      // ----------------------------------------------------------------------
50  
51      /** {@inheritDoc} */
52      public void execute() throws MojoExecutionException {
53          StringBuilder message = new StringBuilder();
54  
55          for (MavenProject project : projects) {
56              getActiveProfileStatement(project, message);
57  
58              message.append(LS).append(LS);
59          }
60  
61          if (output != null) {
62              StringBuilder sb = new StringBuilder();
63              sb.append("Generated by Maven Help Plugin").append(LS);
64              sb.append("See: https://maven.apache.org/plugins/maven-help-plugin/")
65                      .append(LS)
66                      .append(LS);
67              sb.append(message.toString());
68  
69              try {
70                  writeFile(output, sb);
71              } catch (IOException e) {
72                  throw new MojoExecutionException("Cannot write active profiles to output: " + output, e);
73              }
74  
75              getLog().info("Active profile report written to: " + output);
76          } else {
77              getLog().info(message);
78          }
79      }
80  
81      // ----------------------------------------------------------------------
82      // Private methods
83      // ----------------------------------------------------------------------
84  
85      /**
86       * Method to get the active profiles for the project
87       *
88       * @param project   the current project
89       * @param message   the object where the information will be appended to
90       */
91      private void getActiveProfileStatement(MavenProject project, StringBuilder message) {
92          Map<String, List<String>> activeProfileIds = project.getInjectedProfileIds();
93  
94          message.append(LS);
95          message.append("Active Profiles for Project '").append(project.getId()).append("':");
96          message.append(LS).append(LS);
97  
98          if (activeProfileIds.isEmpty()) {
99              message.append("There are no active profiles.");
100         } else {
101             message.append("The following profiles are active:").append(LS);
102 
103             for (Map.Entry<String, List<String>> entry : activeProfileIds.entrySet()) {
104                 for (String profileId : entry.getValue()) {
105                     message.append(LS).append(" - ").append(profileId);
106                     message.append(" (source: ").append(entry.getKey()).append(")");
107                 }
108             }
109         }
110 
111         message.append(LS);
112     }
113 }