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 @Mojo( name = "system", requiresProject = false )
38 public class SystemMojo
39 extends AbstractHelpMojo
40 {
41
42 private static final int REPEAT = 25;
43
44
45
46
47
48
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 }