1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
40
41
42
43
44 @Mojo(name = "all-profiles", requiresProject = false)
45 public class AllProfilesMojo extends AbstractHelpMojo {
46
47
48
49
50
51
52
53 @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
54 private List<MavenProject> projects;
55
56
57
58
59 @Parameter(defaultValue = "${settings.profiles}", readonly = true, required = true)
60 private List<org.apache.maven.settings.Profile> settingsProfiles;
61
62
63
64
65
66
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
82 if (allProfilesByIds.isEmpty()) {
83 getLog().warn("No profiles detected!");
84 } else {
85
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
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
124
125
126
127
128
129 private void addProjectPomProfiles(
130 MavenProject project, Map<String, Profile> allProfiles, Map<String, Profile> activeProfiles) {
131 if (project == null) {
132
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
154
155
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 }