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.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.settings.SettingsUtils;
33  
34  /**
35   * Displays a list of available profiles under the current project.
36   * <br>
37   * <b>Note</b>: it will list <b>all</b> profiles for a project. If a
38   * profile comes up with a status <b>inactive</b> then there might be a need to
39   * set profile activation switches/property.
40   *
41   * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
42   * @since 2.1
43   */
44  @Mojo(name = "all-profiles", requiresProject = false)
45  public class AllProfilesMojo extends AbstractHelpMojo {
46      // ----------------------------------------------------------------------
47      // Mojo parameters
48      // ----------------------------------------------------------------------
49  
50      /**
51       * This is the list of projects currently slated to be built by Maven.
52       */
53      @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
54      private List<MavenProject> projects;
55  
56      /**
57       * The list of profiles defined in the current Maven settings.
58       */
59      @Parameter(defaultValue = "${settings.profiles}", readonly = true, required = true)
60      private List<org.apache.maven.settings.Profile> settingsProfiles;
61  
62      // ----------------------------------------------------------------------
63      // Public methods
64      // ----------------------------------------------------------------------
65  
66      /** {@inheritDoc} */
67      public void execute() throws MojoExecutionException, MojoFailureException {
68          StringBuilder descriptionBuffer = new StringBuilder();
69  
70          for (MavenProject project : projects) {
71              descriptionBuffer
72                      .append("Listing Profiles for Project: ")
73                      .append(project.getId())
74                      .append(LS);
75  
76              Map<String, Profile> allProfilesByIds = new HashMap<>();
77              Map<String, Profile> activeProfilesByIds = new HashMap<>();
78              addSettingsProfiles(allProfilesByIds);
79              addProjectPomProfiles(project, allProfilesByIds, activeProfilesByIds);
80  
81              // now display
82              if (allProfilesByIds.isEmpty()) {
83                  getLog().warn("No profiles detected!");
84              } else {
85                  // active Profiles will be a subset of *all* profiles
86                  allProfilesByIds.keySet().removeAll(activeProfilesByIds.keySet());
87  
88                  writeProfilesDescription(descriptionBuffer, activeProfilesByIds, true);
89                  writeProfilesDescription(descriptionBuffer, allProfilesByIds, false);
90              }
91          }
92  
93          if (output != null) {
94              try {
95                  writeFile(output, descriptionBuffer);
96              } catch (IOException e) {
97                  throw new MojoExecutionException("Cannot write profiles description to output: " + output, e);
98              }
99  
100             getLog().info("Wrote descriptions to: " + output);
101         } else {
102             getLog().info(descriptionBuffer.toString());
103         }
104     }
105 
106     // ----------------------------------------------------------------------
107     // Private methods
108     // ----------------------------------------------------------------------
109 
110     private void writeProfilesDescription(StringBuilder sb, Map<String, Profile> profilesByIds, boolean active) {
111         for (Profile p : profilesByIds.values()) {
112             sb.append("  Profile Id: ").append(p.getId());
113             sb.append(" (Active: ")
114                     .append(active)
115                     .append(", Source: ")
116                     .append(p.getSource())
117                     .append(")");
118             sb.append(LS);
119         }
120     }
121 
122     /**
123      * Adds the profiles from <code>pom.xml</code> and all of its parents.
124      *
125      * @param project could be null
126      * @param allProfiles Map to add the profiles to.
127      * @param activeProfiles Map to add the active profiles to.
128      */
129     private void addProjectPomProfiles(
130             MavenProject project, Map<String, Profile> allProfiles, Map<String, Profile> activeProfiles) {
131         if (project == null) {
132             // shouldn't happen as this mojo requires a project
133             getLog().debug("No pom.xml found to read Profiles from.");
134             return;
135         }
136 
137         getLog().debug("Attempting to read profiles from pom.xml...");
138 
139         while (project != null) {
140             for (Profile profile : project.getModel().getProfiles()) {
141                 allProfiles.put(profile.getId(), profile);
142             }
143             if (project.getActiveProfiles() != null) {
144                 for (Profile profile : project.getActiveProfiles()) {
145                     activeProfiles.put(profile.getId(), profile);
146                 }
147             }
148             project = project.getParent();
149         }
150     }
151 
152     /**
153      * Adds the profiles from <code>settings.xml</code>.
154      *
155      * @param allProfiles Map to add the profiles to.
156      */
157     private void addSettingsProfiles(Map<String, Profile> allProfiles) {
158         getLog().debug("Attempting to read profiles from settings.xml...");
159         for (org.apache.maven.settings.Profile settingsProfile : settingsProfiles) {
160             Profile profile = SettingsUtils.convertFromSettingsProfile(settingsProfile);
161             allProfiles.put(profile.getId(), profile);
162         }
163     }
164 }