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.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   * Displays a list of the platform details like system properties and environment variables.
31   *
32   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
33   * @since 2.1
34   */
35  @Mojo(name = "system", requiresProject = false)
36  public class SystemMojo extends AbstractHelpMojo {
37      /** Magic number to beautify the output */
38      private static final int REPEAT = 25;
39  
40      // ----------------------------------------------------------------------
41      // Public methods
42      // ----------------------------------------------------------------------
43  
44      /** {@inheritDoc} */
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  }