1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37
38 @Mojo( name = "system", requiresProject = false )
39 public class SystemMojo
40 extends AbstractHelpMojo
41 {
42
43 private static final int REPEAT = 25;
44
45
46
47
48
49
50 public void execute()
51 throws MojoExecutionException
52 {
53 StringBuilder message = new StringBuilder();
54
55 message.append( LS );
56 message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
57 message.append( StringUtils.repeat( "=", REPEAT ) );
58 message.append( " Platform Properties Details " );
59 message.append( StringUtils.repeat( "=", REPEAT ) ).append( LS );
60 message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
61 message.append( LS );
62
63 message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
64 message.append( "System Properties" ).append( LS );
65 message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
66
67 Properties systemProperties = System.getProperties();
68 for ( String key : systemProperties.stringPropertyNames() )
69 {
70 message.append( LS );
71 message.append( key ).append( "=" ).append( systemProperties.getProperty( key ) );
72 }
73
74 message.append( LS ).append( LS );
75 message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
76 message.append( "Environment Variables" ).append( LS );
77 message.append( StringUtils.repeat( "=", LINE_LENGTH ) ).append( LS );
78 try
79 {
80 Properties envVars = CommandLineUtils.getSystemEnvVars();
81 for ( String key : envVars.stringPropertyNames() )
82 {
83 message.append( LS );
84 message.append( key ).append( "=" ).append( envVars.getProperty( key ) );
85 }
86 }
87 catch ( IOException e )
88 {
89 getLog().warn( "Unable to get the environment variables: " + e.getMessage() );
90 }
91
92 message.append( LS );
93
94 if ( output != null )
95 {
96 String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
97 .format( System.currentTimeMillis() );
98 StringBuilder sb = new StringBuilder();
99 sb.append( "Created by: " ).append( getClass().getName() ).append( LS );
100 sb.append( "Created on: " ).append( formattedDateTime ).append( LS ).append( LS );
101 sb.append( message.toString() );
102
103 try
104 {
105 writeFile( output, sb );
106 }
107 catch ( IOException e )
108 {
109 throw new MojoExecutionException( "Cannot write system report to output: " + output, e );
110 }
111
112 getLog().info( "System report written to: " + output );
113 }
114 else
115 {
116 getLog().info( message );
117 }
118 }
119 }