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.commons.lang3.time.DateFormatUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.codehaus.plexus.util.StringUtils;
26  import org.codehaus.plexus.util.cli.CommandLineUtils;
27  
28  import java.io.IOException;
29  import java.util.Properties;
30  
31  /**
32   * Displays a list of the platform details like system properties and environment variables.
33   *
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @since 2.1
36   */
37  @Mojo( name = "system", requiresProject = false )
38  public class SystemMojo
39      extends AbstractHelpMojo
40  {
41      /** Magic number to beautify the output */
42      private static final int REPEAT = 25;
43  
44      // ----------------------------------------------------------------------
45      // Public methods
46      // ----------------------------------------------------------------------
47  
48      /** {@inheritDoc} */
49      public void execute()
50          throws MojoExecutionException
51      {
52          StringBuilder message = new StringBuilder();
53  
54          message.append( LS );
55          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
56          message.append( StringUtils.repeat( "=", REPEAT ) );
57          message.append( " Platform Properties Details " );
58          message.append( StringUtils.repeat( "=", REPEAT ) ).append( LS );
59          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
60          message.append( LS );
61  
62          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
63          message.append( "System Properties" ).append( LS );
64          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
65  
66          Properties systemProperties = System.getProperties();
67          for ( String key : systemProperties.stringPropertyNames() )
68          {
69              message.append( LS );
70              message.append( key ).append( "=" ).append( systemProperties.getProperty( key ) );
71          }
72  
73          message.append( LS ).append( LS );
74          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
75          message.append( "Environment Variables" ).append( LS );
76          message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
77          try
78          {
79              Properties envVars = CommandLineUtils.getSystemEnvVars();
80              for ( String key : envVars.stringPropertyNames() )
81              {
82                  message.append( LS );
83                  message.append( key ).append( "=" ).append( envVars.getProperty( key ) );
84              }
85          }
86          catch ( IOException e )
87          {
88              getLog().warn( "Unable to get the environment variables: " + e.getMessage() );
89          }
90  
91          message.append( LS );
92  
93          if ( output != null )
94          {
95              String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
96                  .format( System.currentTimeMillis() );
97              StringBuilder sb = new StringBuilder();
98              sb.append( "Created by: " ).append( getClass().getName() ).append( LS );
99              sb.append( "Created on: " ).append( formattedDateTime ).append( LS ).append( LS );
100             sb.append( message.toString() );
101 
102             try
103             {
104                 writeFile( output, sb );
105             }
106             catch ( IOException e )
107             {
108                 throw new MojoExecutionException( "Cannot write system report to output: " + output, e );
109             }
110 
111             getLog().info( "System report written to: " + output );
112         }
113         else
114         {
115             getLog().info( message );
116         }
117     }
118 }