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 final String BUILD_VERSION_PROPERTY = "version";
52
53 public static void showVersion( PrintStream stdout )
54 {
55 Properties properties = getBuildProperties();
56 stdout.println( createMavenVersionString( properties ) );
57 String shortName = reduce( properties.getProperty( "distributionShortName" ) );
58
59 stdout.println( shortName + " home: " + System.getProperty( "maven.home", "<unknown maven home>" ) );
60
61 stdout.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" )
62 + ", vendor: " + System.getProperty( "java.vendor", "<unknown vendor>" ) );
63
64 stdout.println( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) );
65
66 stdout.println( "Default locale: " + Locale.getDefault() + ", platform encoding: "
67 + System.getProperty( "file.encoding", "<unknown encoding>" ) );
68
69 stdout.println( "OS name: \"" + Os.OS_NAME + "\", version: \"" + Os.OS_VERSION + "\", arch: \"" + Os.OS_ARCH
70 + "\", family: \"" + Os.OS_FAMILY + "\"" );
71 }
72
73
74
75
76
77
78
79 static String createMavenVersionString( Properties buildProperties )
80 {
81 String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
82 String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
83 String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
84 String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
85
86 String msg = distributionName + " ";
87 msg += ( version != null ? version : "<version unknown>" );
88 if ( rev != null || timestamp != null )
89 {
90 msg += " (";
91 msg += ( rev != null ? "r" + rev : "" );
92 if ( timestamp != null )
93 {
94 SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" );
95 String ts = fmt.format( new Date( Long.valueOf( timestamp ).longValue() ) );
96 msg += ( rev != null ? "; " : "" ) + ts;
97 }
98 msg += ")";
99 }
100 return msg;
101 }
102
103 private static String reduce( String s )
104 {
105 return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
106 }
107
108
109 private static void stats( Date start, Logger logger )
110 {
111 Date finish = new Date();
112
113 long time = finish.getTime() - start.getTime();
114
115 logger.info( "Total time: " + formatTime( time ) );
116
117 logger.info( "Finished at: " + finish );
118
119
120 System.gc();
121
122 Runtime r = Runtime.getRuntime();
123
124 logger.info( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / MB + "M/" + r.totalMemory() / MB + "M" );
125 }
126
127 private static String formatTime( long ms )
128 {
129 long secs = ms / MS_PER_SEC;
130
131 long min = secs / SEC_PER_MIN;
132
133 secs = secs % SEC_PER_MIN;
134
135 String msg = "";
136
137 if ( min > 1 )
138 {
139 msg = min + " minutes ";
140 }
141 else if ( min == 1 )
142 {
143 msg = "1 minute ";
144 }
145
146 if ( secs > 1 )
147 {
148 msg += secs + " seconds";
149 }
150 else if ( secs == 1 )
151 {
152 msg += "1 second";
153 }
154 else if ( min == 0 )
155 {
156 msg += "< 1 second";
157 }
158 return msg;
159 }
160
161 private static String getFormattedTime( long time )
162 {
163 String pattern = "s.SSS's'";
164 if ( time / 60000L > 0 )
165 {
166 pattern = "m:s" + pattern;
167 if ( time / 3600000L > 0 )
168 {
169 pattern = "H:m" + pattern;
170 }
171 }
172 DateFormat fmt = new SimpleDateFormat( pattern );
173 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
174 return fmt.format( new Date( time ) );
175 }
176
177 static Properties getBuildProperties()
178 {
179 Properties properties = new Properties();
180 InputStream resourceAsStream = null;
181 try
182 {
183 resourceAsStream = MavenCli.class.getResourceAsStream( "/org/apache/maven/messages/build.properties" );
184
185 if ( resourceAsStream != null )
186 {
187 properties.load( resourceAsStream );
188 }
189 }
190 catch ( IOException e )
191 {
192 System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
193 }
194 finally
195 {
196 IOUtil.close( resourceAsStream );
197 }
198
199 return properties;
200 }
201
202 public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
203 {
204 if ( logger == null )
205 {
206 logger = new PrintStreamLogger( System.out );
207 }
208
209 if ( showStackTrace )
210 {
211 logger.error( message, e );
212 }
213 else
214 {
215 logger.error( message );
216
217 if ( e != null )
218 {
219 logger.error( e.getMessage() );
220
221 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
222 {
223 logger.error( "Caused by: " + cause.getMessage() );
224 }
225 }
226 }
227 }
228
229 }