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.Properties;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugins.annotations.Mojo;
26 import org.codehaus.plexus.util.StringUtils;
27 import org.codehaus.plexus.util.cli.CommandLineUtils;
28
29
30
31
32
33
34
35 @Mojo(name = "system", requiresProject = false)
36 public class SystemMojo extends AbstractHelpMojo {
37
38 private static final int REPEAT = 25;
39
40
41
42
43
44
45 public void execute() throws MojoExecutionException {
46 StringBuilder message = new StringBuilder();
47
48 message.append(LS);
49 message.append(StringUtils.repeat("=", LINE_LENGTH)).append(LS);
50 message.append(StringUtils.repeat("=", REPEAT));
51 message.append(" Platform Properties Details ");
52 message.append(StringUtils.repeat("=", REPEAT)).append(LS);
53 message.append(StringUtils.repeat("=", LINE_LENGTH)).append(LS);
54 message.append(LS);
55
56 message.append(StringUtils.repeat("=", LINE_LENGTH)).append(LS);
57 message.append("System Properties").append(LS);
58 message.append(StringUtils.repeat("=", LINE_LENGTH)).append(LS);
59
60 Properties systemProperties = System.getProperties();
61 for (String key : systemProperties.stringPropertyNames()) {
62 message.append(LS);
63 message.append(key).append("=").append(systemProperties.getProperty(key));
64 }
65
66 message.append(LS).append(LS);
67 message.append(StringUtils.repeat("=", LINE_LENGTH)).append(LS);
68 message.append("Environment Variables").append(LS);
69 message.append(StringUtils.repeat("=", LINE_LENGTH)).append(LS);
70 Properties envVars = CommandLineUtils.getSystemEnvVars();
71 for (String key : envVars.stringPropertyNames()) {
72 message.append(LS);
73 message.append(key).append("=").append(envVars.getProperty(key));
74 }
75
76 message.append(LS);
77
78 if (output != null) {
79 StringBuilder sb = new StringBuilder();
80 sb.append("Generated by Maven Help Plugin").append(LS);
81 sb.append("See: https://maven.apache.org/plugins/maven-help-plugin/")
82 .append(LS)
83 .append(LS);
84 sb.append(message.toString());
85
86 try {
87 writeFile(output, sb);
88 } catch (IOException e) {
89 throw new MojoExecutionException("Cannot write system report to output: " + output, e);
90 }
91
92 getLog().info("System report written to: " + output);
93 } else {
94 getLog().info(message);
95 }
96 }
97 }