1 package org.apache.maven.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.PrintStream;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Locale;
29 import java.util.Properties;
30 import java.util.TimeZone;
31
32 import org.codehaus.plexus.logging.Logger;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.plexus.util.Os;
35
36
37
38
39
40
41
42 public final class CLIReportingUtils
43 {
44
45 public static final long MB = 1024 * 1024;
46
47 public static final int MS_PER_SEC = 1000;
48
49 public static final int SEC_PER_MIN = 60;
50
51 public static void showVersion( PrintStream stdout )
52 {
53 Properties properties = getBuildProperties();
54
55 String timestamp = reduce( properties.getProperty( "timestamp" ) );
56 String version = reduce( properties.getProperty( "version" ) );
57 String rev = reduce( properties.getProperty( "buildNumber" ) );
58
59 String msg = "Apache Maven ";
60 msg += ( version != null ? version : "<version unknown>" );
61 if ( rev != null || timestamp != null )
62 {
63 msg += " (";
64 msg += ( rev != null ? "r" + rev : "" );
65 if ( timestamp != null )
66 {
67 SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" );
68 String ts = fmt.format( new Date( Long.valueOf( timestamp ).longValue() ) );
69 msg += ( rev != null ? "; " : "" ) + ts;
70 }
71 msg += ")";
72 }
73
74 stdout.println( msg );
75
76 stdout.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) );
77
78 stdout.println( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) );
79
80 stdout.println( "Default locale: " + Locale.getDefault() + ", platform encoding: "
81 + System.getProperty( "file.encoding", "<unknown encoding>" ) );
82
83 stdout.println( "OS name: \"" + Os.OS_NAME + "\" version: \"" + Os.OS_VERSION + "\" arch: \"" + Os.OS_ARCH
84 + "\" Family: \"" + Os.OS_FAMILY + "\"" );
85 }
86
87 private static String reduce( String s )
88 {
89 return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
90 }
91
92
93 private static void stats( Date start, Logger logger )
94 {
95 Date finish = new Date();
96
97 long time = finish.getTime() - start.getTime();
98
99 logger.info( "Total time: " + formatTime( time ) );
100
101 logger.info( "Finished at: " + finish );
102
103
104 System.gc();
105
106 Runtime r = Runtime.getRuntime();
107
108 logger.info( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / MB + "M/" + r.totalMemory() / MB + "M" );
109 }
110
111 private static String formatTime( long ms )
112 {
113 long secs = ms / MS_PER_SEC;
114
115 long min = secs / SEC_PER_MIN;
116
117 secs = secs % SEC_PER_MIN;
118
119 String msg = "";
120
121 if ( min > 1 )
122 {
123 msg = min + " minutes ";
124 }
125 else if ( min == 1 )
126 {
127 msg = "1 minute ";
128 }
129
130 if ( secs > 1 )
131 {
132 msg += secs + " seconds";
133 }
134 else if ( secs == 1 )
135 {
136 msg += "1 second";
137 }
138 else if ( min == 0 )
139 {
140 msg += "< 1 second";
141 }
142 return msg;
143 }
144
145 private static String getFormattedTime( long time )
146 {
147 String pattern = "s.SSS's'";
148 if ( time / 60000L > 0 )
149 {
150 pattern = "m:s" + pattern;
151 if ( time / 3600000L > 0 )
152 {
153 pattern = "H:m" + pattern;
154 }
155 }
156 DateFormat fmt = new SimpleDateFormat( pattern );
157 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
158 return fmt.format( new Date( time ) );
159 }
160
161 static Properties getBuildProperties()
162 {
163 Properties properties = new Properties();
164 InputStream resourceAsStream = null;
165 try
166 {
167 resourceAsStream =
168 MavenCli.class.getClassLoader().getResourceAsStream( "org/apache/maven/messages/build.properties" );
169
170 if ( resourceAsStream != null )
171 {
172 properties.load( resourceAsStream );
173 }
174 }
175 catch ( IOException e )
176 {
177 System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
178 }
179 finally
180 {
181 IOUtil.close( resourceAsStream );
182 }
183
184 return properties;
185 }
186
187 public static void showError( Logger logger, String message, Exception e, boolean showStackTrace )
188 {
189 if ( showStackTrace )
190 {
191 logger.error( message, e );
192 }
193 else
194 {
195 logger.error( message );
196
197 if ( e != null )
198 {
199 logger.error( e.getMessage() );
200
201 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
202 {
203 logger.error( "Caused by: " + cause.getMessage() );
204 }
205 }
206 }
207 }
208
209 }