View Javadoc
1   package org.apache.maven.plugins.help;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.codehaus.plexus.util.StringUtils;
25  import org.codehaus.plexus.util.cli.CommandLineUtils;
26  
27  import java.io.IOException;
28  import java.util.Properties;
29  
30  /**
31   * Displays a list of the platform details like system properties and environment variables.
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @since 2.1
35   */
36  @Mojo( name = "system", requiresProject = false )
37  public class SystemMojo
38      extends AbstractHelpMojo
39  {
40      /** Magic number to beautify the output */
41      private static final int REPEAT = 25;
42  
43      // ----------------------------------------------------------------------
44      // Public methods
45      // ----------------------------------------------------------------------
46  
47      /** {@inheritDoc} */
48      public void execute()
49          throws MojoExecutionException
50      {
51          StringBuilder message = new StringBuilder();
52  
53          message.append( LS );
54          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
55          message.append( StringUtils.repeat( "=", REPEAT ) );
56          message.append( " Platform Properties Details " );
57          message.append( StringUtils.repeat( "=", REPEAT ) ).append( LS );
58          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
59          message.append( LS );
60  
61          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
62          message.append( "System Properties" ).append( LS );
63          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
64  
65          Properties systemProperties = System.getProperties();
66          for ( String key : systemProperties.stringPropertyNames() )
67          {
68              message.append( LS );
69              message.append( key ).append( "=" ).append( systemProperties.getProperty( key ) );
70          }
71  
72          message.append( LS ).append( LS );
73          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
74          message.append( "Environment Variables" ).append( LS );
75          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
76          try
77          {
78              Properties envVars = CommandLineUtils.getSystemEnvVars();
79              for ( String key : envVars.stringPropertyNames() )
80              {
81                  message.append( LS );
82                  message.append( key ).append( "=" ).append( envVars.getProperty( key ) );
83              }
84          }
85          catch ( IOException e )
86          {
87              getLog().warn( "Unable to get the environment variables: " + e.getMessage() );
88          }
89  
90          message.append( LS );
91  
92          if ( output != null )
93          {
94              StringBuilder sb = new StringBuilder();
95              sb.append( "Generated by Maven Help Plugin" ).append( LS );
96              sb.append( "See: https://maven.apache.org/plugins/maven-help-plugin/" ).append( LS ).append( LS );
97              sb.append( message.toString() );
98  
99              try
100             {
101                 writeFile( output, sb );
102             }
103             catch ( IOException e )
104             {
105                 throw new MojoExecutionException( "Cannot write system report to output: " + output, e );
106             }
107 
108             getLog().info( "System report written to: " + output );
109         }
110         else
111         {
112             getLog().info( message );
113         }
114     }
115 }