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.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Locale;
27 import java.util.Properties;
28 import java.util.TimeZone;
29
30 import org.codehaus.plexus.util.IOUtil;
31 import org.codehaus.plexus.util.Os;
32 import org.slf4j.Logger;
33
34
35
36
37
38
39
40 public final class CLIReportingUtils
41 {
42
43 public static final long MB = 1024 * 1024;
44
45 private static final long ONE_SECOND = 1000L;
46 private static final long ONE_MINUTE = 60 * ONE_SECOND;
47 private static final long ONE_HOUR = 60 * ONE_MINUTE;
48 private static final long ONE_DAY = 24 * ONE_HOUR;
49
50 public static final String BUILD_VERSION_PROPERTY = "version";
51
52 public static String showVersion()
53 {
54 final String LS = System.getProperty( "line.separator" );
55 Properties properties = getBuildProperties();
56 StringBuilder version = new StringBuilder();
57 version.append( createMavenVersionString( properties ) ).append( LS );
58 version.append( reduce( properties.getProperty( "distributionShortName" ) + " home: "
59 + System.getProperty( "maven.home", "<unknown maven home>" ) ) ).append( LS );
60 version.append( "Java version: " ).append(
61 System.getProperty( "java.version", "<unknown java version>" ) ).append( ", vendor: " ).append(
62 System.getProperty( "java.vendor", "<unknown vendor>" ) ).append( LS );
63 version.append( "Java home: " ).append( System.getProperty( "java.home", "<unknown java home>" ) ).append( LS );
64 version.append( "Default locale: " ).append( Locale.getDefault() ).append( ", platform encoding: " ).append(
65 System.getProperty( "file.encoding", "<unknown encoding>" ) ).append( LS );
66 version.append( "OS name: \"" ).append( Os.OS_NAME ).append( "\", version: \"" ).append( Os.OS_VERSION ).append(
67 "\", arch: \"" ).append( Os.OS_ARCH ).append( "\", family: \"" ).append( Os.OS_FAMILY ).append( "\"" );
68 return version.toString();
69 }
70
71
72
73
74
75
76
77 static String createMavenVersionString( Properties buildProperties )
78 {
79 String timestamp = reduce( buildProperties.getProperty( "timestamp" ) );
80 String version = reduce( buildProperties.getProperty( BUILD_VERSION_PROPERTY ) );
81 String rev = reduce( buildProperties.getProperty( "buildNumber" ) );
82 String distributionName = reduce( buildProperties.getProperty( "distributionName" ) );
83
84 String msg = distributionName + " ";
85 msg += ( version != null ? version : "<version unknown>" );
86 if ( rev != null || timestamp != null )
87 {
88 msg += " (";
89 msg += ( rev != null ? rev : "" );
90 if ( timestamp != null )
91 {
92 String ts = formatTimestamp( Long.valueOf( timestamp ) );
93 msg += ( rev != null ? "; " : "" ) + ts;
94 }
95 msg += ")";
96 }
97 return msg;
98 }
99
100 private static String reduce( String s )
101 {
102 return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null );
103 }
104
105 static Properties getBuildProperties()
106 {
107 Properties properties = new Properties();
108 InputStream resourceAsStream = null;
109 try
110 {
111 resourceAsStream = MavenCli.class.getResourceAsStream( "/org/apache/maven/messages/build.properties" );
112
113 if ( resourceAsStream != null )
114 {
115 properties.load( resourceAsStream );
116 }
117 }
118 catch ( IOException e )
119 {
120 System.err.println( "Unable determine version from JAR file: " + e.getMessage() );
121 }
122 finally
123 {
124 IOUtil.close( resourceAsStream );
125 }
126
127 return properties;
128 }
129
130 public static void showError( Logger logger, String message, Throwable e, boolean showStackTrace )
131 {
132 if ( showStackTrace )
133 {
134 logger.error( message, e );
135 }
136 else
137 {
138 logger.error( message );
139
140 if ( e != null )
141 {
142 logger.error( e.getMessage() );
143
144 for ( Throwable cause = e.getCause(); cause != null; cause = cause.getCause() )
145 {
146 logger.error( "Caused by: " + cause.getMessage() );
147 }
148 }
149 }
150 }
151
152 public static String formatTimestamp( long timestamp )
153 {
154
155 TimeZone tz = TimeZone.getDefault();
156 int offset = tz.getRawOffset();
157
158 long m = Math.abs( ( offset / ONE_MINUTE ) % 60 );
159 long h = Math.abs( ( offset / ONE_HOUR ) % 24 );
160
161 int offsetDir = (int) Math.signum( (float) offset );
162 char offsetSign = offsetDir >= 0 ? '+' : '-';
163 return String.format( "%tFT%<tT%s%02d:%02d", timestamp, offsetSign, h, m );
164 }
165
166 public static String formatDuration( long duration )
167 {
168 long ms = duration % 1000;
169 long s = ( duration / ONE_SECOND ) % 60;
170 long m = ( duration / ONE_MINUTE ) % 60;
171 long h = ( duration / ONE_HOUR ) % 24;
172 long d = duration / ONE_DAY;
173
174 String format;
175 if ( d > 0 )
176 {
177 format = "%d d %02d:%02d h";
178 }
179 else if ( h > 0 )
180 {
181 format = "%2$02d:%3$02d h";
182 }
183 else if ( m > 0 )
184 {
185 format = "%3$02d:%4$02d min";
186 }
187 else
188 {
189 format = "%4$d.%5$03d s";
190 }
191
192 return String.format( format, d, h, m, s, ms );
193 }
194
195 }