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.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
32
33
34
35 @Mojo(name = "active-profiles", aggregator = true)
36 public class ActiveProfilesMojo extends AbstractHelpMojo {
37
38
39
40
41
42
43
44 @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
45 private List<MavenProject> projects;
46
47
48
49
50
51
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
83
84
85
86
87
88
89
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 }